Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
%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.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted
+100

See http://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
share|improve this answer
    
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 '13 at 1:31
    
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 '13 at 1:34
1  
You're welcome. I started with Lamport's book LaTeX: A Document Preparation System, which is good. –  John MacFarlane Jun 4 '13 at 3:37
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.