0%

Pandoc 定制模板转换 Markdown 为 PDF

定制模板控制 Markdown 转换细节

模板

Eisvogel

使用 Metadata 控制

Pandoc Filter

扩展 Markdown

  1. pandoc-mermaid-filter
  2. mermaid-filter
  3. Pandoc Ditaa Filter
  4. Imagine

其中 Imagine 支持如下代码块

  • asy
  • blockdiag
  • boxes
  • chartdirector
  • ctioga2
  • ditaa
  • figlet
  • flydraw
  • gle
  • gnuplot
  • graphviz
  • gri
  • inline
  • matplotlib
  • mermaid
  • mscgen
  • octave
  • plantuml
  • ploticus
  • plotutils
  • protocol
  • pygal
  • pyxplot
  • random
  • sample
  • wolfram

Pandoc 控制

  • 模板即为一组语法集合
  • 转换为 PDF --pdf-engine=xelatex
  • 使用模板 --template "eisvogel.latex"

支持中文

-V CJKmainfont="Microsoft YaHei"

高亮

  • --list-highlight-styles
  • --list-highlight-languages
  • --highlight-style zenburn

定制语法高亮,输出默认并编辑

$ pandoc --print-highlight-style=pygments > pygments.theme

章节编号

--number-sections

目录

  • --toc
  • --toc-depth
  • -V toc-title

链接颜色

-V colorlinks -V urlcolor=NavyBlue

PDF margin

-V geometry:"top=2cm, bottom=1.5cm, left=2cm, right=2cm"

设置放入 head.tex

-H --include-in-header=FILE

Chapter breaks

\usepackage{sectsty}
\sectionfont{\clearpage}
$ $ pandoc sample_1.md -f gfm -H chapter_break.tex -o sample_1_chapter_break.pdf

Changing settings via -V option

pandoc "$1" \
    -f gfm \
    --include-in-header chapter_break.tex \
    -V linkcolor:blue \
    -V geometry:a4paper \
    -V geometry:margin=2cm \
    -V mainfont="DejaVu Serif" \
    -V monofont="DejaVu Sans Mono" \
    --pdf-engine=xelatex \
    -o "$2"
  • mainfont is for normal text
  • monofont is for code snippets
  • geometry for page size and margins
  • linkcolor to set hyperlink color

Inline code

\usepackage{fancyvrb,newverbs,xcolor}

\definecolor{Light}{HTML}{F4F4F4}

\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{
  \colorbox{Light}{\oldtexttt{#1}}
}

使用方式--include-in-header inline_code.tex

PDF properties

\usepackage{hyperref}

\hypersetup{
  pdftitle={My awesome book},
  pdfauthor={learnbyexample},
  pdfsubject={pandoc},
  pdfkeywords={pandoc,pdf,xelatex}
}

cover image

\includegraphics{cover.png}
\thispagestyle{empty}

blockquote

\usepackage{tcolorbox}
\newtcolorbox{myquote}{colback=red!5!white, colframe=red!75!black}
\renewenvironment{quote}{\begin{myquote}}{\end{myquote}}