Markdown to Word and PDF: Keeping Formulas, Tables, and CJK Layout Intact
Markdown is fast to write; deliverables are usually Word. The step in between is where things break: formulas become images, tables collapse, CJK text loses its fonts. Here is the chain, opened up.
1. Three paths — pick the right one first
“Convert Markdown to Word” is not one operation but three different ones. Choose wrong and every later fix is patching the first mistake.
| Path | Good for | Cost |
|---|---|---|
| Select all, paste into Word | A few paragraphs, no math, no complex tables | All styles lost: headings become bold body text, formulas become images or garbage |
| pandoc on the command line | Long documents, math and tables, batch work | You must install it, and tune a template once |
| Write in an online editor, export | No installation, short documents | Export fidelity is capped by the editor; complex styling still needs cleanup |
The decision rule is simple: does the document contain formulas? If yes, abandon copy-paste. Math is the one thing you cannot repair by hand afterwards — retyping fifteen equations costs far more than installing pandoc once.
2. What actually gets lost
Markdown describes semantics; Word describes styling. A converter maps one to the other, and anything missing from the mapping table falls through. Four categories account for almost all of it:
- Heading levels map to Word’s Heading 1/2/3 styles — but only if those styles exist in the target document. Edited or customised templates degrade them to body text with bold and a larger size.
- Code blocks usually keep monospace and shading; inline code often loses its background, because that is character-level shading rather than a paragraph style.
- Tables: GFM pipe tables express regular grids only. Merged cells, column-width ratios, and repeating header rows cannot be represented.
- Formulas survive only if the converter emits Word equation objects (OMML). Once rasterised into an image, not a single line can be edited again.
There is a quieter failure too: hard line breaks. Two trailing spaces plus a newline is a line break in Markdown, but many converters turn it into a paragraph break — so tight paragraphs split in two, and CJK first-line indentation fires twice.
3. How formulas survive: OMML is the whole game
A Word equation is not a picture; it is an XML structure called OMML (Office Math Markup Language). If the converter can translate the TeX inside $…$ into OMML, the result is a live equation you can double-click and edit. If it cannot, you get a screenshot. Those are two very different futures for the same document.
pandoc takes the OMML route by default, which is why it is the reliable command-line choice. But it only understands standard notation. These constructs are where it slips:
| Construct | Result | Advice |
|---|---|---|
| $x^2$, $\frac{a}{b}$ | Converts cleanly to a Word equation | Safe |
| \begin{aligned} | Usually converts; alignment points may drop | Spot-check one line |
| \begin{align} | Often fails or arrives as plain text | Rewrite as aligned, or split into separate display equations |
| Custom macros (\newcommand) | Always fails | Expand to full notation before converting |
| \text{…} with CJK | Font or size may be lost | Move the prose outside the formula |
| \begin{cases} | Usually fine; brace sometimes thins out | Check brace height |
A counter-intuitive conclusion
The plainer the math, the better it converts. Macros you built up over three years and triply nested aligned blocks look elegant in an editor and collapse completely on export. For delivery-oriented documents, write the math a little stupidly.
4. Tables, images, and footnotes
Tables are the second disaster zone. A pipe table is a plain-text grid, and it expresses an order of magnitude less than a Word table: no merged cells, no row spans, no percentage column widths, no repeating header row.
- Merged cells are impossible in Markdown. Either merge by hand after conversion, or split the content into two simple tables.
- Column widths are distributed evenly, so a table with one long CJK column and one short numeric column arrives cramped. Fix it once in Word via table properties.
- Repeating header rows: select the header row → Table Properties → enable “Repeat as header row at the top of each page”. Markdown cannot express this.
- Images use relative paths, so the working directory matters. Remote images need network access at conversion time; offline they fail silently into empty placeholders.
- Footnotes: pandoc understands [^1] and emits real Word footnotes. Most online editors do not, and print the brackets literally.
5. Four CJK typography settings the converter will not do for you
An English document usually looks acceptable straight out of conversion. A Chinese document looks readable but unofficial. The reason is that Word’s default body style is configured for Latin text: Calibri, 11 pt, zero indent, single spacing. All four are wrong for Chinese.
| Setting | Word default | What CJK needs |
|---|---|---|
| Font | Calibri / DengXian | Song or Source Han Serif for body, Hei for headings; set Latin and CJK separately |
| Size | 11 pt | 10.5 pt (Chinese size 5); official documents often 16 pt |
| First-line indent | 0 | 2 characters — use the character unit, not centimetres |
| Line spacing | Single | 1.5 lines, or a fixed 22–28 pt depending on the text block |
Do not fix this paragraph by paragraph after converting. Build a reference.docx with those four settings once, then pass it to every conversion and let the styles apply themselves.
6. The standard pandoc commands
Once pandoc is installed, the minimum command is one line. The deliverable version adds a template.
# 1. Export the default template, restyle it, save as reference.docx
pandoc --print-default-data-file reference.docx > reference.docx
# 2. Convert with the template
pandoc input.md -o output.docx --reference-doc=reference.docx
# 3. Add a table of contents
pandoc input.md -o output.docx --reference-doc=reference.docx --toc --toc-depth=3
# 4. Straight to PDF (CJK needs xelatex plus an explicit font)
pandoc input.md -o output.pdf --pdf-engine=xelatex \
-V CJKmainfont="Noto Serif CJK SC" -V mainfont="Noto Serif CJK SC"Two details. --toc writes a Word TOC field, which shows page numbers only after you press F9 inside Word — people routinely assume it failed. And a “font not found” error when making a PDF means the font name is wrong: use the registered family name, not the file name.
7. If you will not install a command line
Installing pandoc is a real barrier for non-technical users. Work down this list instead:
- Write in a Markdown editor that can export, and export to HTML or docx. Prefer one that preserves equation structure — if double-clicking a formula in the result opens an editor, it worked.
- For live preview while writing, the in-site editor renders formulas as you type; it solves writing and previewing, while final Word delivery still benefits from template-based conversion.
- Pasting HTML into Word beats pasting raw Markdown: HTML carries semantic tags, so Word can recover heading levels.
- If all you have is plain text, paste as unformatted text and apply styles inside Word. Do not expect the original appearance to survive.
8. Six failure modes, mapped
| Symptom | Cause | Fix |
|---|---|---|
| Formulas became images | The converter cannot emit OMML | Use pandoc, or an editor that exports equation objects |
| Formulas show backslashes | The math was escaped as plain text | Check whether it sat inside a code block; in Markdown a literal backslash is doubled |
| All table columns are jammed | Widths are split evenly and CJK width is miscalculated | Adjust column widths in Word once, or use fewer columns |
| Headings have no styles | The target document lacks Heading 1/2 styles | Supply a reference.docx that defines them |
| Everything became one font | Only the CJK font was set | Set Latin and CJK fonts separately in the style definition |
| Indentation fires twice | Hard line breaks became paragraph breaks | Remove trailing double spaces and use real paragraph breaks |
9. Seven checks before you deliver
- Formulas open for editing when double-clicked — and matrices and cases are still structurally intact.
- The navigation pane shows the correct heading hierarchy, with no skipped levels.
- No table row is orphaned across a page break, and header rows repeat.
- Latin and CJK fonts are each correct; the whole document is not one font.
- Inline code and code blocks survived with their monospace face.
- Margins, page numbers, headers and footers match the requirement — Markdown handles none of these.
- Save a PDF alongside it. Word layout depends on the fonts installed on the reader’s machine; a PDF is the version that stays fixed.
That last point deserves emphasis: if someone else will read the document, Word is only an intermediate artefact. Convert the final version to PDF and the reader’s fonts, Office version, and printer stop mattering.
Frequently asked
Why did my formulas turn into images after conversion?
The converter did not translate TeX into Word’s OMML equation structure, so it embedded a picture instead. Image formulas cannot be edited, resized consistently, or numbered by Word. A converter that emits OMML by default — pandoc does — avoids this entirely.
Can the heading hierarchy be preserved?
Yes, provided the target Word file defines the Heading 1/2/3 styles. Convert with a reference.docx template and the headings map automatically, which also makes them visible in Word’s navigation pane.
Are merged table cells supported?
No. GFM pipe tables describe regular grids only; merged cells, row spans, and column-width ratios are outside the format. Merge by hand in Word afterwards, or restructure the content into several simple tables.
Why does pandoc produce tofu boxes for Chinese in PDF output?
The default PDF engine has no CJK fonts. Switch to xelatex and pass -V CJKmainfont with a font actually installed on the system, using its registered family name rather than the file name.
The table of contents has no page numbers — is it broken?
No. It is a Word TOC field that has not been refreshed. Select all and press F9, or right-click the TOC and choose Update Field → Update entire table.
After conversion the whole document is one font, including English. Why?
Only the CJK font was set. In Word’s style dialog, Latin and CJK fonts are separate settings; specify both under Modify Style → Format → Font.
Tools mentioned above
- Markdown & LaTeX editorWrite and preview formulas live, with GFM tables and KaTeX math
- LaTeX math cheat sheetSubscripts, matrices, and alignment written correctly before you convert
- Word to PDFFreeze the final layout so the recipient sees exactly what you see
Read next
WebP compatibility map: when it is safe, and when you must fall back to JPG
Two sentences get repeated about WebP: that it is 30% smaller than JPG, and that all modern browsers support it. The first is half true — it depends on the quality you are targeting. The second is true for browsers and completely false for software. This article takes both apart with measured numbers and a layered compatibility table.
PNG or JPG: a table by use case, with measured size and quality data
This question has ten thousand answers, and most of them stop at “photos use JPG, icons use PNG” — correct, but not enough to decide about the file in front of you. Here three test images go through seven format settings, and the argument is made with bytes and pixel-level error: when JPEG saves 80%, and when it saves 18% while damaging the edges of your text.
Typesetting Math Exams and Lesson Plans: Numbers, Formulas, Figures
There is only one standard for a well-typeset exam: can a reader find “question 17, part 2, option C” within three seconds. Every rule below exists to serve that.
How to Write LaTeX Math: From Inline Symbols to Aligned Equations
Six separate problems hide inside “my formula won’t render”: where the notation goes, how scripts bind, how brackets grow, how matrices are written, how lines align, and how to read an error. Each one has a fixed rule.