Pandoc - Inserting pages before generated Table of Contents

Andrew Vink picture Andrew Vink · Aug 31, 2014 · Viewed 10.1k times · Source

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!

Answer

Liyan Chang picture Liyan Chang · Oct 4, 2016

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