Jump to content

Change the size or font of all formulas

From The Document Foundation Wiki


Description

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.

Code

Using a Python script or LibreOffice Basic macro:

# coding: utf-8
from __future__ import unicode_literals

import uno
# from com.sun.star.document import OfficeDocument

FRAME_DESKTOP = 'com.sun.star.frame.Desktop'
FORMULA_PROPERTIES = 'com.sun.star.formula.FormulaProperties'

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
            self.doc.reformat()


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

g_exportedScripts = reformatAllFormulas,
Option Explicit

Sub ChangeFormulasSizeAndFont()
    '''Change the size or font of all Math formulas in document'''
    Dim embeddedObjects As Object ' com.sun.star.text.TextEmbeddedObjects
    Dim elementNames() As String ' Name array
    Dim element As Object ' com.sun.star.text.TextEmbeddedObject.Model
    Dim realElem As Object ' com.sun.star.text.TextEmbeddedObject.(get)EmbeddedObject

    embeddedObjects = ThisComponent.getEmbeddedObjects() ' com.sun.star.text.TextEmbeddedObjects
    elementNames = embeddedObjects.getElementNames() ' name array

    For i = 0 To UBound(elementNames)

        element = embeddedObjects.getByName(elementNames(i)).Model
        If (not isNull(element)) Then
            With element ' com.sun.star.text.TextEmbeddedObject.Model
            If .supportsService("com.sun.star.formula.FormulaProperties") Then
                ' Force the update
                realElem = embeddedObjects.getByName(elementNames(i)).EmbeddedObject
                realElem.updateMode = com.sun.star.embed.EmbedUpdateModes.ALWAYS_UPDATE
                .BaseFontHeight = 14
                .FontNameVariables = "Arial"
                .FontNameFunctions = "Arial"
                .FontNameNumbers = "Arial"
                .FontNameText = "Arial"
            End If
            End With ' element As com.sun.star.formula.FormulaProperties
       End If

     Next i
 
    ThisComponent.reformat()

End Sub ' ChangeFormulasSizeAndFont

ODT files to test macro

Pick up any LibreOffice published Math guide for testing purpose.

Notes

  • Consult API documentation about FormulaProperties to explore other properties that can be altered.
  1. See the API documentation for com.sun.star.text.TextEmbeddedObjects and com.sun.star.text.TextEmbeddedObject services
  2. See the API documentation for com.sun.star.formula.FormulaProperties service