LaTeX to Word Conversion

I have been using LaTeX for writing and have very limited knowledge of Word. However, there are times when a document has to be in Word format, which is extremely inconvenient, especially for scientific writing. To avoid learning how to write in Word, I started searching for ways to convert LaTeX into Word.

Pandoc has so far been a very useful tool for that purpose. To start, you first need to install it. The installation is well documented on the Pandoc home page. For Mac users like myself, it can be installed with Homebrew using the simple command

brew install pandoc

A simple conversion can be done with the command

pandoc your_tex_file_name.tex -o your_docx_file_name.docx

A simple conversion like this may not be satisfactory, especially if you need to give this to your supervisor. A few problems still need to be addressed:

  1. Formatting of the converted file (font size, font style etc)
  2. Proper citations
  3. Numbering and correct referencing of figures, equations, tables, etc.

Conversion file formatting

The command above converts the tex file to docx in the default format. To specify the format of the docx, you can create a docx in which you have customized your format and use it as a reference during pandoc conversion

First, create a reference docx file to customize the format, using the command

pandoc -o custom_reference_file.docx --print-default-data-file reference.docx

This should create a word file named custom_reference_file.docx with content as follows

Here should be a demo figure

Then you can change the format in this docx file accordingly (change the style of the docx), and use it as the template for pandoc conversion

pandoc your_tex_file_name.tex --reference-doc=custom_reference_file.docx -o your_docx_file_name.docx

The converted file should now have the same style as you set up in the reference docx.

Citations

You can provide the BibLaTeX file you used for LaTeX to pandoc for the bibliography using --bibliography. You should provide the relative path of your .bib file just as you would do in latex. An example would be

pandoc your_tex_file_name.tex --bibliography=your_references.bib -o your_docx_file_name.docx

However, the citations in the converted docx file might not show up correctly. This is usually resolved using pandoc-citeproc by specifying command

pandoc your_tex_file_name.tex --citeproc --bibliography=your_reference.bib -o your_docx_file_name.docx

If you have specific citation style preference, you can also specify that in pandoc through citation-style-language file .csl. Usually this will be provided by the publisher of the specific journal you are targeting. You can also find the file from their GitHub repo https://github.com/citation-style-language/styles. After you have the .csl file, specify that in the pandoc command

pandoc your_tex_file_name.tex --citeproc --bibliography=your_reference.bib --csl=your_csl.csl -o your_docx_file_name.docx

Cross reference

Pandoc does a pretty good job converting LaTeX equations to Word equations with no need for equation editors like MathType, which is a major reason why I chose to convert LaTeX to Word with pandoc instead of just writing in docx.

  • Notice that some math commands in LaTeX, especially some provided by packages like amsmath are not supported in pandoc conversion. Pandoc will still do the conversion by simply keeping unknown commands in their original form (for example, \text in align environment is not recognized by pandoc, so whenever \text{} appears in LaTeX, pandoc will simply convert that into ‘\text{what you wrote}’ in the docx file )
  • This problem could be resolved by making a pandoc filter so that unrecognized commands are detected and replaced with something else.

However, in LaTeX, the equations, labels, tables or sections can be numbered, while native pandoc doesn’t support this feature. Also, in LaTeX, referencing these objects is easily done by creating \label{} for the one that you wish to refer and use \ref{label_name} where you wish to refer to it in your article. It will be referenced by the corresponding number.

Example figures

However, this is not supported natively in pandoc, which is quite problematic, especially for scientific writing. The feature can be achieved through pandoc filters. There are many online resources providing filters for different features, and I have found pandoc-crossref and pandoc-tex-numbering quite useful for cross-referencing in my situation.

pandoc-crossref: this extension is one popular cross referencing extension for pandoc. It enables a lot of different features and customization options. However, it does not officially support LaTeX. Some have shown that it still works on LaTeX with restrictions, but in my case it doesn’t work well. I haven’t found a way to fix the issue.

  • It works for simple equation numbering though

pandoc-tex-numbering: this filter does not support as many features as pandoc-crossref and is also not as well documented as pandoc-crossref, but it did a pretty decent job numbering and cross-referencing the objects in LaTeX to Word.

  • It also supports multiline equations and subfigures, quite handy

Equations

For equation numbering, you can use pandoc-crossref specifying

  • autoEqnLabels: automatically number all display equations
  • tableEqns: word doesn’t natively support equation numbering. If this is not specified as true, the number will be added directly into the equation block. If true, a table will be created so that number can be put at the end of line.

To do this, specify

pandoc your_tex_file_name.tex --filter pandoc-crossref -M autoEqnLabels -o your_docx_file_name.docx

this will generate numbering for the equations. Or

pandoc your_tex_file_name.tex --filter pandoc-crossref -M autoEqnLabels -M tableEqns -o your_docx_file_name.docx

and the number will be put at the end of line.

The reference of the equations is not right though.

Pandoc-tex-numbering handles this quite well. You can use it as the filter for pandoc

pandoc your_tex_file_name.tex --filter pandoc-tex-numbering -o your_docx_file_name.docx

The default setting numbers each of your sections in levels, and numbers the equations and figures accordingly (like 1.1.2 for equation 2 in section 1.1). I don’t want section numbering or leveled numbering, which I can turn off by passing the metadata as

pandoc your_tex_file_name.tex --filter pandoc-tex-numbering -M number-sections=false -M number-reset-level=0 -o your_docx_file_name.docx

More metadata commands can be found in their Github repo. The number though is in the math block and there doesn’t seem to be a way to put it at the end of the line.

Figures and Tables

In LaTeX, the numbering of figures or tables will be shown as ‘Figure #: your caption’ in the caption region of the figure. Pandoc-tex-numbering can also do this during the conversion. You can customize the prefix using figure-prefix. The default value is “Figure”

pandoc your_tex_file_name.tex --filter pandoc-tex-numbering -M figure-prefix="Fig" -o your_docx_file_name.docx

This will specify the prefix of figure as “Fig #: your caption”.

Summary

Now, the generated Word file will have pre-specified format, correct citations, cross-reference and numbering for equations and figures. It will match the standard to give to your PI perhaps with a few minor adjustments.

Ref




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • DFT Calculations Using Quantum ESPRESSO
  • Longest Palindromic Substring
  • Negative Binomial Distribution