Change the size or font of all formulas

From The Document Foundation Wiki
< Macros‎ | Math
Jump to navigation Jump to search
Other languages:
English • ‎français


When you want to modify an attribute in all the formulas you are going to write, just follow the procedure described in Faq/Math/008. If you want to change "after the fact", you have to reopen all the formulas and make the modifications. If the number of formulas is large, this can quickly become tedious.

This script selects embedded formula objects[1] in the document, and updates intended formula properties[2]. This macro solves the issue i#5092.


With LibreOffice Basic:

Option Explicit

Sub ChangeFormulasSizeAndFont()
    '''Change the size or font of all Math formulas in document'''
    Dim embeddedObjects As Object '
    Dim elementNames() As String ' Name array
    Dim element As Object '
    Dim realElem As Object '

    embeddedObjects = ThisComponent.getEmbeddedObjects() '
    elementNames = embeddedObjects.getElementNames() ' name array

    For i = 0 To UBound(elementNames)

        element = embeddedObjects.getByName(elementNames(i)).Model
        If (not isNull(element)) Then
            With element '
            If .supportsService("") Then
                ' Force the update
                realElem = embeddedObjects.getByName(elementNames(i)).EmbeddedObject
                realElem.updateMode =
                .BaseFontHeight = 14
                .FontNameVariables = "Arial"
                .FontNameFunctions = "Arial"
                .FontNameNumbers = "Arial"
                .FontNameText = "Arial"
            End If
            End With ' element As
       End If

     Next i

End Sub ' ChangeFormulasSizeAndFont

With Python:

# coding: utf-8
from __future__ import unicode_literals

import uno
# from import OfficeDocument


class UIFormulaCollection():
    """ Current document formulae collection """
    def __init__(self):
        """ Grab current doc in UI by default """
        # ctx = uno.getComponentContext()  # GetDefaultContext
        # smgr = ctx.getServiceManager()  # GetProcessServiceManager
        # desktop =  smgr.createInstanceWithContext(FRAME_DESKTOP, ctx)  # StarDesktop
        # self.doc = desktop.CurrentComponent  # ThisComponent
        self.doc = XSCRIPTCONTEXT.getDocument()
        self.objects = self.doc.EmbeddedObjects
        self.names = self.objects.getElementNames()
    def reformat(self, fontHeight=None, font=None):
        """ add as many properties as desired """
        for name in self.names:
            element = self.objects.getByName(name).Model
            if element.supportsService(FORMULA_PROPERTIES):
                if font:
                    element.FontNameFunctions = font
                    element.FontNameText = font
                    element.FontNameNumbers = font
                    element.FontNameVariables = font
                if fontHeight:
                    element.BaseFontHeight = fontHeight

def reformatAllFormulas():
    ui = UIFormulaCollection()
    ui.reformat(font="Arial", fontHeight=14)

g_exportedScripts = reformatAllFormulas,

ODT files to test macro

Pick up any LibreOffice published Math guide for testing purpose.


  • Consult API documentation about FormulaProperties to explore other properties that can be altered.
  1. See the API documentation for and services
  2. See the API documentation for service