For a project, I would like to use .md files and convert them to a .docx file. I would also like to include some \latex\ commands within the .md files. I just do not want to mess up the system that I am currently using because it works well. Which is contradictory, but hopefully it can be done.

Here is my pandoc setup:

pandoc --reference-doc=pandoc/reference.docx frontmatter1.md fm2.md fm3.md fm4.md f5.md ch1.md ch2.md ch3.md bib.md endmatter.md em.md -o article.docx

I know latex makes much more sense for what I am trying to do above, but working with .docx is a painstaking process. The reference.docx solution works well for me. I just highlight the sections and select the style.

Finally, my question. Can I insert \latex\ commands such as \newpage at the end of my .md files? So far, I have been unsuccessful.

Here is an example of a .md file I would like to get working with latex / pandoc / word:

# heading
Some text here.
## subheading
More text here.


3 Answers 3


I'm not sure if I should be posting an answer on my question, but the real answer seems to be that it just doesn't exist. See the following:

Pandoc Open Issue

Pandoc Google Group
Technical Discussion about the issue

  • Converting among differents document formats, usually is better lost most low level format. For instance, a manual page breaks in LaTeX problebly will end in the wrong place in another format.
    – Fran
    Commented Dec 21, 2022 at 1:41

You can perfectly include LaTeX commands in markdown to export with pandoc to a LaTeX document, then the LaTeX document can be converted to docx and some of these commands will produce the desired effect in the .docx files, as \emph{text}, while others will be ignored, as \newpage.

That is, you cannot export LaTeX commands in .md → .docx conversion as all the latex code will be ignored, but as far as possible you can do it in a .md → . tex. → .docx process like:

 $ pandoc -s test.md -o test.tex
 $ pandoc borra.tex -o borra.docx
  • 1
    Thank you @Fran for the idea to export to .tex as an intermediate step. It solved my problems and saved me a lot of trouble.
    – LunkRat
    Commented Dec 20, 2022 at 16:42

As noted by @hearsay, there is no build-in way of doing this. However, using a few lines of Lua allows to convert all \newpage LaTeX snippets into the respective OpenXML structure:

-- write to file: docx-pagebreaks.lua
function RawBlock (blk)
  if FORMAT == 'docx' and blk.format == 'latex' and blk.text == '\\newpage' then
    return pandoc.RawBlock(
      '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'

Run with

pandoc --reference-doc=pandoc/reference.docx \
       frontmatter1.md fm*.md ch*.md bib.md endmatter.md em.md \
       -o article.docx

There's also a ready-made Lua filter for this purpose: https://github.com/pandoc-ext/pagebreak
See the README in that repo for usage instructions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .