Automatically adjust font size of equations to match their environment?

Is it possible to automatically adjust formatting equations based on where they are inserted?

Let’s say I have a document, where image captions are 9pt, and the main body text is 12 pt, headings up to 18 pt. In the equation editor I can set a default font size, but it means that whenever I create an equations in a caption or heading, I will have to manually change it to fit the environment.

Is it possible to make Libreoffice do this automatically?

Unfortunately, no.

Equations are created in Math which is independent from Writer. Writer Math has not seen significant development or even update to make it abreast with Writer for ages. Its “style” system is palaeolithical. And even if its “styles” were on par with Writer, there is no reason formatting to be driven by Writer styles of the same name when rendered in Writer.

Also when you look at the internal XML encoding of your document, you see that equations are nested inside their own XML elements.

If you have already ideas on how it could be implemented, please share and have a try. Source code is freely available for you to experiment.

It’s worse than that. You insert a formula made with Math, which is then displayed by Math as if it were a genuine OLE object, the same as when you insert part of a Calc sheet in a Writer document. The only “extenuating circumstance” is that it seems to be a functional clone of MathType, which was every bit as bad in that respect. Even inserting a formula from a file saved as .odf is not very intuitive.

A solution would be an update of the ODF format, with a separate formula style, that will override settings made in Math, and contains settings for font face, basic font size (by default taken from the Default paragraph style), with separate fields for size and height of subscripts and superscripts and everything else that goes in formulas. In a way, a Writer document would then more or less work like a master document, with the formulas inserted as sub documents and their formatting overridden by the master document.

Try this macros - for one selected Math Formula, or for all Math Formulas in document. And set the const c as you need.

Sub Selected_MathFormula_TextHeight 'for 1 selected Math formula
	const c=1.5 'multiple for the Formula FontHeight
	dim oDoc as object, o as object, a as single
	oDoc=ThisComponent
	o=oDoc.CurrentController.Selection 'actual selection
	if o.supportsService("com.sun.star.text.TextEmbeddedObject") then 'embedded object is selected
		if o.getEmbeddedObject().ImplementationName="com.sun.star.comp.Math.FormulaDocument" then 'it is Math formula
			a=o.Anchor.CharHeight 'height from Anchor of the Formula
			o.EmbeddedObject.Component.BaseFontHeight=c*a 'set FontHeight to Formula
		end if
	end if
End Sub

Sub All_MathFormula_TextHeight 'for all Math formulas in the document
	const c=1.5 'multiple for the Formula FontHeight
	dim oDoc as object, o as object, a as single
	oDoc=ThisComponent
	for each o in oDoc.EmbeddedObjects 'for each embedded object in document
		if o.getEmbeddedObject().ImplementationName="com.sun.star.comp.Math.FormulaDocument" then 'it is Math formula
			a=o.Anchor.CharHeight 'height from Anchor of the Formula
			o.EmbeddedObject.Component.BaseFontHeight=c*a 'set FontHeight to Formula
		end if
	next o
End Sub

(Not actually a solution.)
I would suggest to define all the format settings manually for one of the many formulas, to select it, and then to harmonize all the formulas with the help of the Basic routine harmonizeFormulas contained in the attached example.
(The code isn’t excactly efficient, because it forces the text document to re-render three times per formula. For documents with hundreds of formulas you may run it unattended.)

The example itself is made of an old (about 25 years) document originally going back to an old MS Word, then converted to StarOffice and saved in old .sxw. format. Just about an hour ago I excavated it from my archeological folders, and inserted the code.

The same example now also contains code only losely related to this question. It can convert correctly shaped plain text pieces to Math formulas.

To understand the history, you may visit these old threads:
disask47622 (Formula to plain text)
disask54403 (Marked plain text to formula)
disask47680 (Harmonizing formulas using one of them as a formatting template)

The Example:
mathFormuilaExampleQuaGl.odt (115.5 KB)

I came back to this thread because I had posted some code being rather complicated on the one hand, but not exactly addressing the question on the other hand.

Reconsidered the answer is “Yes, that can be done by user code in a rather simple way.”.

  • One linitation is that font sizes (height) in Writer can have fractional values while Math only allows for the type Small for which I once more don’t know a clear specification.
  • A second flaw is that it’s complicated to implement a restriction to an arbitrary selection of formulas. I only implemented ALL.
  • A third point is the open question what “their environment” is supposed to mean. I took the CharHeight property of the TextParagraph the hosting frame of the equation is anchored to or in.

An example containing the needed code should show if it is usable for a specific purpose. Call the macro writerHarmonizeAllMathFormulaBaseFontHeightsWithSurroundingText
The example file:
anotherExampleSourceWithCode.odt (47.3 KB)

The code (Basic):

 Sub writerHarmonizeAllMathFormulaBaseFontHeightsWithSurroundingText()
   drawPage = ThisComponent.DrawPage
   writerHarmonizeMathFormulaBaseFontHeightsWithSurroundingTextForElementContainer(drawPage)
 End Sub

 Sub writerHarmonizeMathFormulaBaseFontHeightsWithSurroundingTextForElementContainer(pShapeContainer As Object)
   For Each element In pShapeContainer
      If element.supportsService("com.sun.star.text.TextEmbeddedObject") Then
        tp = element.Anchor.TextParagraph
        newBaseFontHeight = CInt(Int(tp.CharHeight + 0.5))
 REM Type of target property is "Small"
        trySetMathBaseFontHeight(element, newBaseFontHeight)
      EndIf
   Next element
   fail:
 End Sub

 Sub trySetMathBaseFontHeight(pHost, pNewBaseFontHeight)
   'On Local Error Goto fail
   mathComp= pHost.Component
   mathComp.BaseFontHeight = pNewBaseFontHeight
   pHost.ExtendedControlOverEmbeddedObject.Update()
   fail:
 End Sub

Please @Lupp, forgive me for taking the liberty to reformat your code - ajlittoz
There’s a faint chance :wink: if you tell me why.
I had used the tool Preformatted text. The editor showed a closing angle bracket at the start of every line then, and I thought this was OK.

(It’s my personal style to not indent every line inside the definition of a routine, but only to separate any Sub or Function from others by one or two empty lines. .
Internal constructs are emphasized by indenting additional 2 spaces per order of nesting then.)

See also my attached file and the answers in Easy way to change fonts of all mathematical formulas (OLE)