Pandoc - Inserting pages before generated Table of Contents

14,802

Solution 1

I am guessing you want to create a HTML File with Title Page or Header Information. The solution is to do it in more than one Step.

First you compile the Title Page together any other pages to one html.

$ pandoc .\01_HEADER.markdown -o header.html

Then you include that header.html before the body:

$ pandoc -s -S --toc -B .\header.html .\02_Document.markdown -o complete_doc.html

Done.


The pandoc help states it's possibilities to make multi-part documents possible, both -B and -H work, but i think -B is more correct in my case.

  -H FILENAME           --include-in-header=FILENAME                    
  -B FILENAME           --include-before-body=FILENAME                  
  -A FILENAME           --include-after-body=FILENAME                  

In my case i do it with a stylesheet and get a perfect document:

$ pandoc -s -S -c .\res\github-pandoc.css .\01_HEADER.markdown -o header.html
$ pandoc -s -S --toc --toc-depth=2 -c .\res\github-pandoc.css -B .\header.html .\02_Document.markdown -o complete_document.html

Solution 2

For this answer, I'm going to assume you are generating markdown, though the process is the same for other file formats as well.

The key insight is that Pandoc uses templates to determine the final placement. It has default templates and if you modify them, you can change the order of sections.

  1. Find the default template

    > pandoc -D markdown
    $if(titleblock)$
    $titleblock$
    
    $endif$
    $for(header-includes)$
    $header-includes$
    
    $endfor$
    $for(include-before)$
    $include-before$
    
    $endfor$
    $if(toc)$
    $toc$
    
    $endif$
    $body$
    $for(include-after)$
    
    $include-after$
    $endfor$
    

    You won't need this for the next step, but if you are curious where these files live, asking for a nonsense file type works (though I'm sure there is a better way):

    > pandoc -D nonsense
    pandoc: Could not find data file /usr/local/.../templates/default.nonsense
    
  2. Copy and modify the default template

    > pandoc -D markdown > modified.markdown
    

    Using your favorite text editor, you can open modified.markdown to put the $body$ before the $toc$.

    $body$
    $if(toc)$
    $toc$
    $endif$
    
  3. Use the modified template

    > pandoc --template modified.markdown <... rest of your command ...>
    

Solution 3

Use the include-before tag you can observe in the output of pandoc --print-default-template=markdown

---
title: My Way
toc: true
include-before: |
    See these lines
    Come __before__ the [toc](/dev/null)
---

# I've lived a life that's full
# I traveled each and every highway
# But more, much more than this
# I did it my way

Solution 4

None of these solutions worked for me, and one top-level comment suggested using Pandoc templates without details. So here's a simple fix.

  • Download the default Pandoc template from here: https://github.com/jgm/pandoc-templates, e.g. I used default.tex but renamed it template.tex.

  • Modify the template to insert additional content, create new pages, etc. e.g.:

    \input{title.tex}
    \newpage
    \input{acknowledgements.tex}
    \newpage
    
    $if(toc)$
    $if(toc-title)$
    \renewcommand*\contentsname{$toc-title$}
    $endif$
    
    \newpage
    
  • Use this modified template rather than the default template, e.g.

    pandoc --template=template.tex -o document.pdf document.tex`.
    

That's it.

Share:
14,802

Related videos on Youtube

Andrew Vink
Author by

Andrew Vink

Updated on June 18, 2022

Comments

  • Andrew Vink
    Andrew Vink about 2 years

    I've been playing around with Pandoc.

    Is there anyway to insert pages before the generated Table of Contents?

    For example:

    • Title Page
    • Insert Custom Page 001
    • Insert Custom Page 002
    • (Generated) Table of Contents

    Many thanks in advance!

  • scoa
    scoa over 7 years
    I'm not sure this is what the OP is asking for: they want to add some custom pages before the TOC, but not the whole body, which should still appear after.
  • Joshua Muheim
    Joshua Muheim over 7 years
    Yes, this answer doesn't really take into account that the body should still come after the TOC. I'd like to create a title page in Markdown which should come before the TOC. Is this possible?
  • Cris Luengo
    Cris Luengo about 4 years
    Excellent, thanks! You can also add toc: true to the YAML section, instead of adding the --toc argument. For multi-line (and multi-paragraph) text, use include-before: |, and add a set of identically indented lines after.
  • Tinmarino
    Tinmarino about 4 years
    Thank you, @CrisLuengo, I did not know all that. I update the post.
  • Qinsi
    Qinsi over 2 years
    What does -S mean? Can't find it in pandoc doc.
  • muck
    muck over 2 years
    -S, --smart Produce typographically correct output, converting straight quotes to curly quotes, --- to em-dashes, -- to en-dashes, and ... to ellipses. Nonbreaking spaces are inserted after certain abbreviations, such as "Mr." (Note: This option is significant only when the input format is markdown or textile. It is selected automatically when the input format is textile or the output format is latex or context, unless --no-tex-ligatures is used.) linux.die.net/man/1/pandoc This will help you find it in pandoc.org/MANUAL.html