SPB Git

Repository Public Research preview

latex-to-word.md 3.5 KB

# LaTeX to Word

# Why a recipe is needed

pandoc main.tex -o main.docx produces a Word file in which every \ref{…} is empty or shows the label name, floats have no number, and equations have no number. pandoc reads the .tex source only; the numbers exist nowhere but in the .aux file that LaTeX writes when it compiles.

# The recipe

bash
node scripts/latex.mjs compile main.tex          # produces the .aux
node scripts/latex.mjs docx main.tex             # → .latex-build/main.docx
node scripts/latex.mjs docx main.tex --out submission/paper.docx --csl styles/apa.csl

docx does the following, and reports counts for each:

  1. Flattens the document: \input, \include and \subfile are expanded, comments removed.
  2. Reads the .aux: every \newlabel{key}{{number}{page}…}.
  3. Replaces references in the source: \ref{k} → 3; \eqref{k} → (3); \autoref, \cref, \Cref → Figure 3, Table 2, Section 4 (English names, from the kind of the label); \pageref{k} → the page; lists such as \cref{a,b} → Figures 1 and 2.
  4. Numbers captions: "Figure N. " or "Table N. " is written at the start of the caption of each float that has a label.
  5. Numbers equations: the label of each numbered equation or align row is replaced by its number set at the right of the formula.
  6. Calls pandoc with --number-sections and, when a .bib file is declared, --citeproc.

The flattened, resolved source is kept as .latex-build/<name>.pandoc.tex: read it when a passage converts badly.

unresolved_labels lists references with no number in the .aux; they appear as ?? in the Word file. The command exits 1 when there are any.

# What does not survive

Element In the Word file
Cross-references Plain text. They do not update if the reader moves a figure
Equation numbers Typed beside the equation; not Word's own numbering
Section numbers Numbered by pandoc in order. They differ from the PDF when the document has unnumbered sections, appendices lettered A, B …, or custom counters
Citations and reference list Re-rendered by citeproc: author–date by default, whatever the LaTeX style was. Pass --csl with the journal's CSL file to match
TikZ / pgfplots figures Lost. Export them to PNG or PDF first and include those files
PDF and EPS images Linked as they are; Word often cannot display them. Convert to PNG
Macros Simple \newcommand definitions are expanded by pandoc; macros with complex arguments, \def tricks and package-specific commands are dropped or left as text
Tables Simple tabular converts. multirow, multicolumn with rules, longtable options, siunitx columns and coloured cells convert partly or not at all
Theorems, algorithms, listings Text is kept, numbering and styling mostly lost
Layout Margins, fonts, spacing, float placement, headers and footers: all Word defaults
Footnotes Kept. \thanks and affiliation footnotes may be misplaced
Names in other languages \cref names are written in English ("Figure", "Table")

# Before sending the file

Open it. Check, at least: the title block, one reference of each kind (section, figure, table, equation), each table, each figure, the reference list, and search for ?? and [. Say in your report that the file was produced by conversion and which of the items above apply.

When the recipient will edit the text and send it back, tell them the cross-references are static text; changes then have to be carried back into the LaTeX source by hand.

Files main
Menu

Theme