How to remove a word from all formulas in a .odt file

I have a LibreOffice Writer file containing math exercises. I have all solutions hidden using “phantom”.

This is the content of one of the formulas:

b_23= phantom color green {0}

I would like to remove the “phantom” token from all formulas in the file, so solutions are shown. I have tried decompressing the file, executing this script and compressing the file again:

find ./ -type f -readable -writable -exec sed -i “s/phantom/ /g” {} ;

The word “phantom” is removed, but the solution is not shown until I double click each equation.

So, is there a way of removing the “phantom” word from each formula and updating all the equations easily?

Thanks in advance

You need to not only remove the phantom from the annotation inside the <draw:object>, but also remove the whole <draw:image> next to it, which contains the cached render.

Also there’s a way of creating a macro to edit the formula; see e.g. Formatting of All Math Formulas extension as a starting point for such a macro.

Thank you very much for your answer. It did work.

Right now I have a master document where solutions on formulas have the “color green” token and are visible. Then I ran the script below. It creates a copy of the master document where “color green” is replaced with “phantom”, so solutions are hidden. It also removes the ObjectReplacements folder to update all images and then it creates the .odt and .pdf versions of both documents (with solutions/without solutions).

This is the script I run from console:

unzip "DocName.odt" -d ./tempFolder/
cd tempFolder
rm -rf ObjectReplacements
find ./ -type f -readable -writable -exec sed -i "s/color green/phantom/g" {} \;
zip -r "DocName (no sol).odt" ./
cd -
mv ./tempFolder/"DocName (no sol).odt" ./"DocName (no sol).odt"
rm -rf tempFolder
unoconv -f pdf "DocName.odt"
unoconv -f pdf "DocName (no sol).odt"

If the formulas are as you want, but need a refresh after the changes to them without using the editor, you can call this simple piece of code without the lines “commented away”.

If you also have Writer documents with formulas still needing the removal of the unwanted keyword, the mentioned lines need to be put in function. The cose can easily be adapted to similar tasks.

Sub dePhantomizeAndRefreshAllMathOleShapes()
REM The lines commented out by ' are only needed if the removal of "phantom"
REM keyword is not yet done. Remaining the refresh functionality.
'fa    = createUnoService("com.sun.star.sheet.FunctionAccess")
doc   = ThisComponent
cCtrl = doc.CurrentController
sel   = doc.CurrentSelection
dp    = doc.DrawPage
fr    = cCtrl.Frame
dh    = createUnoService("com.sun.star.frame.DispatchHelper")
For Each sh in dp
  If sh.supportsService("com.sun.star.text.TextEmbeddedObject") Then
    oleComponent = sh.Component
    If oleComponent.supportsService("com.sun.star.formula.FormulaProperties") Then
'      formula    = oleComponent.Formula
'      newFormula = fa.callFunction("REGEX", Array(formula, "(?i)\bPHANTOM\b", "", "g"))
'      oleComponent.Formula = newFormula
      cCtrl.select(sh)
      refreshOleObject(sh, fr, dh)
    End If
  End If
Next sh
cCtrl.select(sel)
End Sub

Sub refreshOleObject(pOleShape As Object, pDispatchProvider As Object, pDispatchHelper As Object) REM Refresh rendering
Dim args(0) as new com.sun.star.beans.PropertyValue
args(0).Name  = "VerbID"
args(0).Value = -1
pDispatchHelper.executeDispatch(pDispatchProvider, ".uno:ObjectMenue", "", 0, args())
pDispatchHelper.executeDispatch(pDispatchProvider, ".uno:TerminateInplaceActivation", "", 0, Array())
End Sub
1 Like

Thanks for your answer. This is what I was looking for.
Right now I am using a workaround that does the trick, but your suggestion looks more reasonable.