Pandoc

January 17, 2025 Updated January 18, 2025

Pandoc is a free-software document converter. It can convert between numerous markup and word processing formats, including, but not limited to, various flavors of MarkdownHTMLLaTeX , PDF and Word docx.

安装后不仅可以在终端用命令行操作,也可以借助Obsidian的 Pandoc-Plugin 插件来将markdown文件转换为由 \(\LaTeX\) 生成的PDF。

0.1 命令行

详见 Pandoc User’s Guidepandoc --help

0.2 Pandoc template

下载了 pandoc-latex-template,相关文件位于 /Users/username/.local/share/pandoc/templates

0.2.1 修改字体

对于CJK用户,需要额外修改.latex文件的这个部分以配置字体:

\else % if not pdftex
  $if(mainfont)$
  $else$
  \usepackage[fallback]{xeCJK}
  \setCJKmainfont{Noto Serif SC}[BoldFont=Noto Sans SC, ItalicFont=Kai] %配置中文字体
  \setCJKfallbackfamilyfont{rm}{Noto Serif SC}
  \setmainfont{Source Serif 4}
  \usepackage{sourcecodepro}

可以在\begin{document}之前这样修改,添加首行缩进:

\usepackage{ctex} %调用中文字体宏包
\usepackage{indentfirst} %调用首行缩进宏包
\setlength{\parindent}{2em} %设置首行缩进为2字符

\begin{document}

0.2.2 使用方法

基本的页面属性模版,用于调整header & footer以及titlepage等:

---
title: "Example PDF"
author: [Author]
date: "2017-02-20"
subject: "Markdown"
keywords: [Markdown, Example]
header-left: "\\hspace{1cm}"
header-center: "\\leftmark"
header-right: "Page \\thepage"
footer-left: "\\thetitle"
footer-center: "This is \\LaTeX{}"
footer-right: "\\theauthor"
titlepage: true
titlepage-color: "3C9F53"
titlepage-text-color: "FFFFFF"
titlepage-rule-color: "FFFFFF"
titlepage-rule-height: 2
titlepage-background: "background.pdf"
page-background: "backgrounds/background1.pdf"
toc: true
toc-own-page: true
...

最简单的调用模版的方法:

pandoc example.md -o example.pdf --template eisvogel --listings

包含我常用选项的方法:

pandoc

sourcefile  
-o outputfile.pdf  
--from markdown
--toc
--template eisvogel  
--listings
--number-sections
--pdf-engine=xelatex

其中--toc用来生成目录(也可以直接在属性里面添加toc: true),--number-sections用来给标题添加数字小节。

除此之外,还有其他一些之后可能会用到的选项:

生成 beamer:

pandoc "document.md" -o "document.pdf" --from markdown --to beamer --template "../../dist/eisvogel.beamer" --listings

生成接近书本的样式: (详见typesetting-a-book

pandoc "document.md" -o "document.pdf" --from markdown --template "../../dist/eisvogel.latex" --listings --top-level-division="chapter"

Syntax Highlighting Without Listings:

pandoc example.md -o example.pdf --template eisvogel --highlight-style pygments
pandoc example.md -o example.pdf --template eisvogel --highlight-style kate
pandoc example.md -o example.pdf --template eisvogel --highlight-style espresso
pandoc example.md -o example.pdf --template eisvogel --highlight-style tango

0.3 使用测试

Note

This is an Obsidian Callout.

这是一段quote

这是一个中文段落,带有加粗斜体的内容。测试高亮部分

注意内部链接格式需要为测试

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam aliquet libero quis lectus elementum fermentum.

table a 中文 what
12 20 安静 a
haha 中文 test 209

This is an strange English, sentence.

This is an English sentence mixed with 中文内容。

  • 列表项 1
  • 列表项 2
  1. 有序
  2. 列表

外部链接

用几何方法推导了幂函数求导公式(Power rule):\(\frac{d(x^n)} {dx}=nx^{n-1}\)


啊啊啊啊这是一段中文接下来是英文Laudat ille auditi; vertitur iura tum nepotis causa; motus. Diva virtus! Acrota destruitis vos iubet quo et classis excessere Scyrumve spiro subitusque mente Pirithoi abstulit, lapides.

0.3.1 标题2

啊啊啊哈

<!DOCTYPE html>
<html>
  <head>
    <title>This is the title of the page.</title>
  </head>
  <body>
    <a href="http://example.com">This is a link.</a>
    <img src="./image.jpg" alt="This is an image.">
  </body>
</html>

0.3.1.1 标题3

我是谁who are you

lambda arguments: expression
a = 1 # test 注释
if a > 1: # 测试中文注释
    print("Hi")