Search code examples
markdownpandocpdflatexxelatex

How to put markdown pages onto new PDF pages with Pandoc?


Pandoc allows a user to translate multiple Markdown input files into one PDF file.

pandoc --pdf-engine=xelatex -o test.pdf *.md

With this call, all Markdown pages are continuously listed in the PDF output without any new-page. I would like to start a new PDF page per Markdown file.

How to add a \newpage after or before every inserted Markdown file?


Solution

  • If you use GNU awk, i.e. gawk, with multiple files, it has an extension that triggers at the end of each file so you can use that to insert "newpage" commands like this:

    gawk '1; ENDFILE{print "\\newpage"}' *.md | pandoc --pdf-engine=xelatex -o test.pdf
    

    The 1 in gawk evaluates to "True" which causes gawk to do its default action, which is to print the current line, so that means all lines get printed, in addition to the "newpage" commands at the end of each file.


    You can probably get the same result with GNU sed if you add the -s option to treat each document as separate, so you get a separate "last line" for each document:

    sed -se '$a\\\newpage' doc*.md | pandoc ...