Getting chapters to start on a new page in a pandoc-generated PDF

epublatexpandocpdf

%Title
%Author

#Header 1

Lots of words.

#Header 2

More words.

##Level 2 header

The above text can be turned into an EPUB file for e-readers with pandoc -o output.epub input.mkd, and it can be converted to PDF with pandoc -o output.pdf input.mkd. The latter requires a latex engine to be installed, which may be relevant to answering the question.

With the EPUB, each level 1 header is automatically set at the top of a new page (lesser headers aren't). With the PDF, this isn't the case – and I wouldn't expect it to be by default, since that would be contrary to the main goals of markdown. However, I'm having trouble finding a pandoc option to enable this behaviour.

Does anyone know of a way to enable this behaviour with pandoc? Editing a config file would be an acceptable solution, but if the syntax is latex-based I would appreciate an explanation of it (of the meaning of what is in the config file, not a comprehensive explanation of latex!).

pandoc's --chapters option seems like it should do what I want… but it

  • Makes chapters only appear on odd-numbered pages, inserting blank pages as necessary (interesting, but not what I want — I'm looking to print this off on A4 paper, and it's not going to be layed out book-style, so any aesthetic positive here is offset by a waste of paper)
  • Inserts Chapter x before the actual header, which leads to some ridiculous stuff like:

Chapter 1

Prologue

If it is possible to tame the --chapters option, that would be a suitable answer.

Best Answer

See https://tex.stackexchange.com/questions/9497/start-new-page-with-each-section.

To make this work with pandoc, you'll need to insert the following into the preamble of the LaTeX document pandoc generates on the way to PDF:

\usepackage{titlesec}
\newcommand{\sectionbreak}{\clearpage}

There are several ways to do that. One is to create a custom LaTeX template with these lines in the preamble. You can then use the option --template mytemplate.latex to tell pandoc to use this template. To get the default LaTeX template, which you can modify, do pandoc -D latex > mytemplate.latex.

Another option is to create a small file titlesec.tex with just those two lines. Then call pandoc with the option --include-in-header titlesec.tex to include it in the header, like so:

pandoc --toc --include-in-header titlesec.tex -o output.pdf input1.mkd input2.mkd
Related Question