LaTeX formulas in ODF and Word documents

by levien on ma 01 oktober 2012 // Posted in misc // under

UPDATE (2012-10-01)
OOoLatex is no longer being updated by Geoffroy Piroux. Roland Baudin has created an up-to-date fork for LibreOffice called TexMaths. To install that on a recent version of Ubuntu, first get the required packages:

sudo add-apt-repository ppa:martin-gieseking/ppa
sudo apt-get update
sudo apt-get install texlive dvipng dvisvgm

Then download the TexMaths extension and install it in LibreOffice using the extension manager (which you will find under the Tools menu).


A few weeks ago, I wrote a chapter on Enzyme Kinetics for a first-year Biochemistry reader. Because the text contains a lot of formulas, I would normally have used LaTeX. But the problem is that the Structural Biology group who organise the Biochemistry course have to be able to edit the document, and they only use Microsoft Word for Mac. I work mostly on Ubuntu Linux these days, and although I still have a laptop that can run Windows, I neither use nor have a running version of MS Word. And besides that, anyone who has ever tried to use the formula editor of MS Office will have noticed that it, well, sucks. :) I tried using the OpenOffice.org Math editor, but found the version in Ubuntu Feisty to be very unstable and therefore unusable. (Note: The stability problems were with version 2.2. Version 2.3 of the Math editor, which is in Ubuntu Gutsy, seems to be quite usable. However, formulas do not display correctly with Word for Mac.)

So I investigated the possibility of using OpenOffice.org Writer in combination with OOoLatex to create a Word document with LaTeX formulas.

OOoLatex was very easy to install on Ubuntu, and it works like a charm. However, getting MS Word to display the formulas proved not as easy.

First of all, you have to choose the right format for the formula pictures in OOoLatex. WMF gives the best results, but requires the Bakoma TTF fonts to be installed on the machine running OpenOffice.org or Word. As I cannot count on other people having these fonts installed, I chose to create the formula pictures as transparant PNG images. The PNG antialiasing did create some rastering artifacts when printing, but these were minor.

A second problem is that the MS Word .doc format is not quite as flexible as OpenOffice.org ODF (Open Document Format) when it comes to positioning pictures relative to the text baseline. Basically, your only choice for inline pictures in .doc files is to align them top, bottom or centre, relative to the baseline. The result is that inline formulas will tend to float either above or below the rest of the line a little bit. Changing the picture type from inline to floating will give you the positioning flexibility to fix this, but requires a lot of manual work repositioning the formulas.

Finally, the various versions of MS Word are very picky as to what they'll accept as pictures. Word 2003 for Windows would not accept the formula pictures in Word documents saved directly from OpenOffice.org Writer 2.2. The documents have to first be reloaded in OpenOffice.org Writer, and then saved again as Word document. Word for Windows then loads these files correctly. Word for Mac however will still stubbornly refuse to display the pictures. Apparently there are several interoperability problems between various Word versions, and this seems to be one of them. However, after some experimenting I found out that the pictures are there, but they have transparency set to 100%. It turns out that Word for Mac does not support partly-transparent objects, it simply makes them fully transparent. Setting transparency to 0% manually is a bit of a drag, but does at least "fix" the problem (double-click on the picture to open the options dialog). If you have inserted any special characters, be sure to check those as well, as MS Word for Mac also seems to have rather limited Unicode-support.

Last week I upgraded my Unbuntu to 7.10 ("Gutsy Gibbon"), which has a newer version of OpenOffice.org (2.3.0). I saved some ODF documents with OOoLatex formulas as Word documents from this version of OpenOffice.org. And guess what, the formulas were accepted without problems on MS Word, both on Windows and on the Mac. Go figure. Of course the baseline offset and Unicode problems are still there though. To wrap things up, some tips for for people who want to create a Word document with LaTeX formulas using OpenOffice.org and OOoLatex:

  • Always keep an ODF version of the document for reference, as you'll loose the correct baseline offset for the formula's when converting to MS Word .doc.
  • Distribute a PDF version of the ODF document for printing, if editing by third parties is not required.
  • If editing (of the text, not the fomulas) by third parties is required, make sure your formulas are created as PNG files, or include the Bakoma TTF fonts with your document.
  • If you are using an OpenOffice.org version before 2.3.0: Before you distribute your Word document, open it in OpenOffice.org Writer, and save it again.
  • If the doument is intended for MS Word users running Windows, consider reccomending the Sun ODF plugin for MS Office, which will enable users of MS Word 2000 and up to work directly with ODF documents. Users of MS Office 2007 can use the open-source OpenXML/ODF translator add-in (or Microsoft's own ODF plugin if it becomes available).
  • If you are using an OpenOffice.org version before 2.3.0 and the document is intended for Word on a Mac, don't create your formulas with a transparent background. If you already have, you'll probably have to manually set the transparency of all the formula pictures to 0% in MS Word for Mac. Alternatively, recommend the Mac user to install NeoOffice, which is a free MacOS X version of OpenOffice.org. This will allow them to work directly with ODF files, handles unicode characters and transparency in a proper way and generally seems to provide better compatibility with MS Word than MS Word for Mac does. ;-)

If you have a Word document with transparent pictures which won't display on Word for Mac, try saving it as RTF and remove all instances of the following RTF-tag from the file (using a text editor or a bit of Perl magic):

{\sp{\sn fillOpacity}{\sv 0}}

I haven't tried it yet, but a comparison of several RTF files produced by Word for Mac and Word for Windows suggests that it might work.

UPDATE 2010-08-05:

Removing the ooolatex package on Ubuntu Hardy 8.04 proved to be a bit of a challenge, as the post-removal script turned out to be broken. The solution was to manually remove the package info and then deinstall the package:

sudo mkdir /tmp/ooolatex
sudo mv ooolatex.* /tmp/ooolatex
sudo dpkg --purge --force-remove-reinstreq ooolatex

If that works, you can delete the temporary directory using sudo rm -r /tmp/ooolatex

See also:
https://bugs.launchpad.net/ubuntu/+source/aptitude/+bug/378545