Changing fonts in multiple embedded formulas, 24.8.1.2

=== Converted from a “comment” to a “suggested solution”.

(I actually read the already given information. I widely ignored the pieces of code, however.)

I wouldn’t write highly specialised code to apply one or two attributes to existing formulas. As I already told, there are about 70, and another user may want to change/harmonise the relative spacing - or whatever,.

Explaining my point of view

Direct (hard) formatting is generally discouraged for good reasons, and the extensive support of styles is an important advantage of the LibreOffice family over commercial competitors. If you want to insert an additional style into the hierarchy in Writer or Calc, you will often use the option of deriving it from an example (a template).
Unfortunately, however, styles are not supported in Draw.
But you can create formulas as templates for other formulas, which then shall use the same settings.
A remaining problem is that you need user code to retroactively set the attributes of such a template for existing formulas. Another one is how to get all the formulas even if nested in groups
===
General example (may need LibreOffice V7 or higher):
harmonizeFormulaShapesInDrawingsDiving.odg (75.1 KB)
Similar example for older versions of LibO and for OO.o, AOO:
harmonizeFormulaShapesInDrawingsDivingLegacy.odg (63.9 KB)

My example documents show how this can be done for all formulas in a Draw document, even for deeply nested ones.
The question of how best to select a subset of existing formulas for reformatting is not covered.

Very unrealistic (joking example):
harmonizeFormulaShapesInDrawingsDivingEE.odg (115.6 KB)

Alright, I don’t get a few things. It seems like a HUGE disadvantage to LO Math that you cannot edit multiple objects at once and it is supposed to be discouraged? Imagine someone asked for LO Impress on how to change the color of two objects. It would be rather simple, wouldn’t it? Why isn’t it the same for LO Math? I was simply looking to automate changing a few known things in formulas. If it is safe to do it manually, why would automating it be somehow discouraged?

What is visually offered as a formula in Draw isn’t the formula itself, but a shape hosting an embedded component.
Also when using the UI you can’t edit it or change internal atributes of it without opening that component which causes a lot of changes in the range of UI elements (menus, toolbars, icons), and also opens a special window for the editing of the content. No component (Writer, Calc, …) can do this for more than one formula at a time.
BTW: I would be interested in a small (but multipage) example where you see a relevant use case for the feature you dream of.

Well, imagine you have 100s of embedded formulas. Say, you decide that you need to change all variables into another font. You can do that one by one, or you can use a simple macro?
I don’t understand what I am possibly losing by automating this procedure. The script I provided already does it. It changes all font sizes and offers significant time saving. I haven’t had any side effects that would discourage me from using it. I have re-checked the formulas after I ran the macros and all they did was exactly what I expected them to do.
I only need to know the names of a few attributes to be able to modify that script to be able to change font types.
I don’t understand why anyone would be against it. The sentiment I am getting is that Math is unreliable and cannot be trusted to achieve basic tasks (change formulas). In my experience, it is a very reliable tool, although it does miss a few features, macros to some extent make up for that.

Simply select one (any) formula shape in the drawing document.
Go to the Basic IDE and create the Sub

Sub Main()
formulamodel = ThisComponent.CurrentSelection(0).Model
End Sub

Execute the one command without leaving the Sub.
Then inspect the properties of the formulamodel in the Watch window clicking the lttle “+”.

Many different users may want to change many different attributes. Therefore I didn’t look for code working on some specific attributes but developed a solution which should be applicable in all the different cases based on one formula manually reformatted.
I also wanted to include formulas contained in possibly nested groups, and made the algorithm therefore recursive.

I’m not “against it”, but think I offered a solution usable in many similar, but not identical cases.
Well, we have mutual problems with understanding one another.

1 Like

This may help. Use and application are at your own risk

**Attribute                Type                            Example**
CustomFontNameFixed       string                       "Liberation Mono"   
CustomFontNameSans        string                       "Liberation Sans"   
CustomFontNameSerif       string                      "Liberation Serif"   
FontFixedIsBold           boolean                                  False   
FontFixedIsItalic         boolean                                  False   
FontFunctionsIsBold       boolean                                  False   
FontFunctionsIsItalic     boolean                                  False   
FontMathIsBold            boolean                                  False   
FontMathIsItalic          boolean                                  False   
FontNameFunctions         string                      "Liberation Serif"   
FontNameMath              string                            "OpenSymbol"   
FontNameNumbers           string                      "Liberation Serif"   
FontNameText              string                      "Liberation Serif"   
FontNameVariables         string                      "Liberation Serif"   
FontNumbersIsBold         boolean                                  False   
FontNumbersIsItalic       boolean                                  False   
FontSansIsBold            boolean                                  False   
FontSansIsItalic          boolean                                  False   
FontSerifIsBold           boolean                                  False   
FontSerifIsItalic         boolean                                  False   
FontTextIsBold            boolean                                  False   
FontTextIsItalic          boolean                                  False   
FontVariablesIsBold       boolean                                  False   
FontVariablesIsItalic     boolean                                   True

The macro by @Lupp give me the key

Thank you, I will probably try this tomorrow and post the result if it is successful, in case others want to try it in the future.
In the meantime, is there a more detailed version of this? I would like to know other values (such as spacing, etc) in case I need to mass edit them in the future.

Sorry. I didn’t ask for your advice what to imagine, but for a “relevant use case”. You may replace “relevant” with “realistic”. Anyway, I expected an attached example file (.odg or possibly .odp).

This is not the same thing. Changing the colour of some Impress objects is operating in the same workspace: objects are created in Impress, they have a style associated with them and you can change the style to act simultaneously on all of them.


Formulas are “external” to the component (Writer, Calc, Draw, Impress) into which they are inserted. They are described by a different “language”. The component understands enough this other language to render the formula but the formatting directive are fundamentally different.

I understand your request and I need it too. It could work if the formatting directives were the same as those in the component. Taking the example of Writer, these would be character styles. Then if these character styles would be made accessible, you’d just customise them “as usual” to operate on all formulas at once.

Unfortunately, Math formulas follow some standard which is not the same as the overall ODF used by LO. Anyway, this creates the problem of driving “external objects” formatting in two different locations: inside Math according to Math procedure and in Writer according to “compatible (?)” character styles.

Did you try the code contained in my example?
It uses a template formula as a kind of surrogate for formula styles.

No. I am presently busy with another time-eating project, but I’ll make a try as soon as possible.

UPDATE:
I used the following script to get what I wanted done:

Sub changeFonts_14pt()
	doc = ThisComponent
	sNames = Array("BaseFontHeight", "FontNameMath", "FontNameVariables", "FontNameFunctions", "FontNameNumbers", "FontNameText", "CustomFontNameFixed", "CustomFontNameSans", "CustomFontNameSerif")
	values = Array(14, "OpenSymbol", "Liberation Serif", "Liberation Serif", "Liberation Serif", "Liberation Serif", "Liberation Mono", "Liberation Sans", "Liberation Serif")
	changeFormulaProperties doc, sNames, values
End Sub

It works and does exactly what I wanted by changing all the fonts at once to the desired ones.