17

I know that I can use the -H or --include-in-header commands to include a custom preamble in the generated output. The problem is now that Pandoc includes my preamble but puts another preamble in front of it:

\documentclass{article}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifxetex
  \usepackage{fontspec,xltxtra,xunicode}
  \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
\else
  \ifluatex
    \usepackage{fontspec}
    \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
  \else
    \usepackage[utf8]{inputenc}
  \fi
\fi
\ifxetex
  \usepackage[setpagesize=false, % page size defined by xetex
              unicode=false, % unicode breaks when used with xetex
              xetex]{hyperref}
\else
  \usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true, pdfborder={0 0 0}}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}

[... my preamble ...]

I know that this is the default LaTeX preamble (default.latex) that Pandoc loads from its templates folder. The same thing happens when I'm using --include-after-body with \end{document}.

This is the command I'm using:

pandoc -o output.tex input.txt --include-before-body=header.tex --include-after-body=footer.tex

2 Answers 2

24

As you have discovered, --include-in-header adds text into the preamble specified in Pandoc's LaTeX template. There are a few ways to do what you are trying to do.

  1. If you would like a completely custom preamble, you need to specify a template file using

    pandoc -o output.tex --template=FILE input.txt
    

    The template can have variables (such as $title$ and, more importantly, $body$) and conditionals. If you would like some inspiration, you could check out the default template using the command

    pandoc -D latex
    
  2. If you want to use a new template once and for all, you can make one, call it default.latex, and put it in the templates directory (~/.pandoc/templates/ on a unix machine). In this case, you need to specify that you want to use a template by calling

    pandoc -o output.tex --standalone input.txt
    
  3. If you would rather not deal with templates at all, you can just run

    pandoc -o output.tex input.txt
    

    and the result will be a bare LaTeX document, that is, without a preamble, \begin{document} or \end{document}. Then you can add a preamble yourself. Note that any metadata (title, author) will be lost when using this method.

Full details on how to make and use templates can be found in Pandoc's excellent man page.

3

See also this answer on tex.stackexchange Adding headers and footers using Pandoc

With pandoc 1.12.x and it’s new YAML metadata capabilities you could add all the information and all LaTeX-code you need in your markdown document like this:

---
title: Test
author: Author Name
header-includes: |
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    \fancyhead[CO,CE]{This is fancy}
    \fancyfoot[CO,CE]{So is this}
    \fancyfoot[LE,RO]{\thepage}
abstract: This is a pandoc test . . . 
---

# This is a test

Lorem ipsum....

That way you don't have to modify the template, simply calling pandoc doc.md -o doc.pdf will suffice.

You must log in to answer this question.

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