2

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?

Paebbels
  • 15,573
  • 13
  • 70
  • 139

1 Answers1

2

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 ...
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • While this answer is correct in adding a raw LaTeX command like `\newpage` into the Markdown file to solve the problem, I would like to see a more neutral answer. Using `awk` or `sed` is a Linux-biased answer not very suitable for Pandoc users on Windows platforms. Otherwise it's a good answer. – Paebbels May 25 '19 at 22:02
  • Sorry, I wasn't aware of that from your question. I'll have a think about Windows-specific solutions, but for the moment, I do know you can install `gawk` on Windows easily enough. – Mark Setchell May 26 '19 at 09:03
  • You can add multiple sections to your answer. 1) a gneric answer about `\newpage` and Pandoc accepting embedded raw LaTeX commands. 2) a linux-based answer using `gawk` or `sed`. 3) a Windows suitable answer using e.g. PowerShell commands. – Paebbels May 26 '19 at 14:02