3

Please consider the very simple latex file below

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}

\begin{equation} \label{eq:somelabel} e=mc^2 \end{equation}

The equation \ref{eq:somelabel} is a typical formula.

\end{document}

which is saved as test_latex.tex. When I try to convert this to a word document with

pandoc test_latex.tex -o -s test_word.docx

in the test_word.docx file the reference to the equation is not correct (instead of a number I see the label).

How can I fix this? Many thanks!

larry77
  • 255
  • 2
  • 6
  • 1
    You might be out of luck: https://github.com/jgm/pandoc/issues/2851 – DG' Jan 26 '21 at 21:11
  • 3
    make4ht -f odt test_latex compiles your file correctly. It creates ODT, not DOCX, but Word can open it too. – michal.h21 Jan 26 '21 at 21:28
  • I wonder if this https://github.com/tomduck/pandoc-eqnos can help or it requires me to start from a markdown file.... – larry77 Jan 26 '21 at 21:57
  • pandoc-eqnos will need markdown input. Give make4ht a try! – DG' Jan 26 '21 at 22:07
  • Yes, I will give make4ht a try, but I need to master also pandoc because it has a much wider scope in the type of docs it can handle. The cross reference section here https://mywindowshub.com/how-to-convert-files-from-latex-to-ms-word-using-pandoc/ may be what I need. – larry77 Jan 26 '21 at 22:09
  • 1
    Pandoc is great and you can do a lot with pandoc-crossref and other filters, but you need to write in markdown to make the most of it – DG' Jan 27 '21 at 08:24

1 Answers1

2

This is my work around:

  1. Convert LaTeX document to epub3 document with MathML by using tex4ebook utility: tex4ebook -f epub3 test_latex.tex "mathml"
  2. Convert this output to MS Word document by using Pandoc: pandoc test_latex.epub -o test_latex.docx

Note that you might need to revise your LaTeX source file before feeding it to tex4ebook, this utility is very fragile and expects a very simple LaTeX document (i.e. documents that use only basic packages). If you get some error messages, you should try with a small fraction of your LaTeX document and detect the reason of the error (error messages produced by tex4ebook does not help at all). In my case I removed some packages like enumitem, tikz, fancyhdr, etc.. I have also figured out that labels and references including colons like \label{eq:chap1-eq1} cause error, so i changed them as \label{eq-chap1-eq1} you should detect all issues like this by trying small fractions of your document.

I have managed to convert large documents like a whole book with this method.