We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question

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

asked 2019-09-16 00:18:04 +0200

juanjo_ng gravatar image

updated 2021-06-05 01:02:36 +0200

Alex Kemp gravatar image

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

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2021-06-05 01:02:45.969851

2 Answers

Sort by » oldest newest most voted

answered 2019-09-16 08:43:28 +0200

updated 2019-09-16 08:46:10 +0200

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.

edit flag offensive delete link more


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"
juanjo_ng gravatar imagejuanjo_ng ( 2019-12-31 12:53:35 +0200 )edit

answered 2019-12-29 19:01:23 +0200

Lupp gravatar image

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
      refreshOleObject(sh, fr, dh)
    End If
  End If
Next sh
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
edit flag offensive delete link more


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.

juanjo_ng gravatar imagejuanjo_ng ( 2019-12-31 13:12:28 +0200 )edit

Question Tools

1 follower


Asked: 2019-09-16 00:18:04 +0200

Seen: 144 times

Last updated: Dec 31 '19