during my thesis I have multiple md (markdown) files in a git repository. All these md files are chapters of a final book pdf that I want to generate. I would like to use XeLatex so I am thinking of converting all md files to tex files (using pandoc) and then include them in final book.tex file that will export the pdf. (An alternative is to hard copy paste the content of 10 md files in a Latex book template but I think there is a more generic way). I wrote a Makefile in order to convert md files to tex like below:

pandoc file1.md -f markdown -t latex -s -o file1.tex
pandoc file2.md -f markdown -t latex -s -o file2.tex
pandoc file10.md -f markdown -t latex -s -o file10.tex

I run the Makefile and as a result I took 10 tex files. After that, I tried to include them in the final book.tex file


Each one tex file,as generated from pandoc, has it's own documentclass and i as a result i am getting error during compilation (Latex error: file1 can be used only in preamble). I tried to hard copy-paste the text of the first file in another tex file without documentclass and works fine, without errors. Is there a way to get the final tex file without copy-paste the content of each sub file? I believe that i need to add something in my Makefile in order to give only one tex file as a result.

Thanks for your attention. I’m looking forward to your reply.

2 Answers 2


Remove the -s (standalone) option. By default the LaTeX output is not a complete document but only the contents of the document environment, ready to be included.

Alternatively, you can also complete documents (i.e., using -s) and include them in a main document without manual modifications. There are several options here. Take a look to the packages standalone, docmute, import and combine and the questions about these packages in this site.

  • Thanks for your answer. I tried removing -s from Makefile and works fine. I am getting back only the content. I also tried docmute package using \input instead of \include in main tex file and works fine too. Thank you again.
    – ziselos
    Commented Apr 11, 2018 at 5:41

Pandoc allows passing multiple input files at once; it will concatenate the files, treating them as if they were one single large file:

pandoc -s -f markdown -t latex file1.md file2.md file3.md -o combined.tex

Note that you can also produce the output PDF directly via pandoc by passing --pdf-engine=xelatex (or, for pandoc v1.x, --latex-engine=xelatex). Pandoc will call XeLaTeX for you.

pandoc -s -f markdown --pdf-engine=xelatex file1.md file2.md \
       -M documentclass=scrbook -o result.pdf

There are multiple options to modify the LaTeX output produced by pandoc, e.g. setting a classoptions metavalue, or passing a LaTeX file to be included verbatim in the header via the --include-in-header parameter.

You must log in to answer this question.

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