22
%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.

4 Answers 4

33
+100

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
5
  • Thank you, this is awesome. This does have the downside of breaking the links within the PDF and setting all the bookmarks (not sure if this is the right word) to page 1, but since I'm using this primarily for actual printing that's not a big deal. The only thing it doesn't do is put the table of contents on the same page as the title, but that's not really important at all.
    – evilsoup
    Jun 4, 2013 at 1:31
  • 2
    BTW, if you're the same John MacFarlane who made pandoc, I'd like to take this chance to thank you - since I discovered it, your program has become a vital part of my writing workflow. Also, are there any resources you'd recommend for learning about LaTeX?
    – evilsoup
    Jun 4, 2013 at 1:34
  • 4
    You're welcome. I started with Lamport's book LaTeX: A Document Preparation System, which is good. Jun 4, 2013 at 3:37
  • 3
    titlesec seems to be incompatible with the current pandoc latex template, the proposed answer generates a latex error. You can use sectsty: \usepackage{sectsty} \sectionfont{\clearpage}. May 20, 2018 at 12:59
  • There are errors when I use the above method. The solution is given here.
    – jdhao
    Jun 17, 2019 at 10:11
13

What I ended up doing was this:

pandoc --toc -V documentclass=report -o output.pdf inputs*.mkd

This causes the LaTeX 'report' document class to be used (by default, with --chapters, pandoc seems to use the 'book' or 'memoir' class, which are designed for books - different-sized margins one each side to facilitate book-binding, chapters only starting on odd pages, etc.), which puts level 1 headers at the top of pages, but doesn't only put them at the top of odd-numbered pages.

This still inserts 'Chapter X' before each chapter, which is still annoying. I would also quite like to have the Table of Contents start on the same page as the title (this happens with the 'article' document class, but then chapters aren't shunted to the beginning of new pages - they just start wherever). However, I think that getting those would require more knowledge of LaTeX than I have: I'm not going to mark this as the accepted answer, since the ideal answer for this would explain how to work around these two issues.

1
  • This is the answer for the question, "How to insert page breaks between HTML input files." Thanks!!
    – blakev
    Apr 14, 2021 at 7:01
1

Also more than two years later, and I kept running into this issue as well; so for anyone else who runs into this issue who finds this...

Thanks to the answers of John and the other link included, I use the following in a file with the pandoc -H option and it seems to work fine:

% https://superuser.com/questions/601469/getting-chapters-to-start-on-a-new-page-in-a-pandoc-generated-pdf
% http://tex.stackexchange.com/questions/120740/how-do-i-remove-chapter-n-from-the-chapter-titles-of-a-book
\usepackage{titlesec}
\usepackage{fancyhdr}

\newcommand{\sectionbreak}{\clearpage}
\titleformat{\chapter}[display]{\normalfont\bfseries}{}{0pt}{\Large}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
\fancyhead[LE,RO]{\thepage}
\renewcommand\headrulewidth{0pt}
\renewcommand\chaptermark[1]{\markboth{#1}{}} 
\renewcommand\sectionmark[1]{\markright{\thesection.\ #1}}

With this, I used the --chapters option to pandoc and it generates PDF chapters on new pages with no Chapter XX: page headings. Using -V documentclass=report also produced the desired result, without the page-skipping-and-new-chapter-on-odd-page-number phenomenon produced by the book document class.

1
  • 1
    --chapters is now OBE. Pandoc says --chapters has been removed. Use --top-level-division=chapter instead. Apr 3, 2020 at 11:55
0

Had the same problem and figured out I'd post my solution using only YAML metadata fields.

documentclass: report
header-includes:
- \renewcommand{\chaptername}{}
- \renewcommand{\thechapter}{}

You must log in to answer this question.

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