pandoc
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePandoc Universal Document Converter Skill
Pandoc 通用文档转换器技能
Convert documents between 40+ formats with Pandoc. This skill covers Markdown to PDF/DOCX/HTML conversions, custom templates, citation management, and batch processing automation.
使用Pandoc在40余种格式之间转换文档。本技能涵盖Markdown转PDF/DOCX/HTML、自定义模板、引用管理以及批处理自动化等内容。
When to Use This Skill
何时使用本技能
USE When
适用场景
- Converting Markdown to PDF with professional formatting
- Creating Word documents from Markdown sources
- Need reproducible document builds from plain text
- Managing academic papers with citations (BibTeX/CSL)
- Batch converting multiple documents
- Need custom templates for consistent branding
- Converting between multiple documentation formats
- Creating LaTeX documents from Markdown
- Need cross-references (figures, tables, equations)
- Building automated document pipelines
- 将Markdown转换为具有专业格式的PDF
- 从Markdown源文件创建Word文档
- 需要从纯文本生成可重复构建的文档
- 使用BibTeX/CSL管理学术论文引用
- 批量转换多个文档
- 需要自定义模板以保持品牌一致性
- 在多种文档格式之间进行转换
- 从Markdown创建LaTeX文档
- 需要交叉引用(图片、表格、公式)
- 构建自动化文档流水线
DON'T USE When
不适用场景
- Building documentation websites (use MkDocs or Sphinx)
- Need interactive documentation (use web frameworks)
- Require real-time collaborative editing (use Google Docs)
- Building slide presentations (use Marp)
- Need WYSIWYG editing (use Word directly)
- Converting complex nested HTML (may lose formatting)
- 构建文档网站(使用MkDocs或Sphinx)
- 需要交互式文档(使用Web框架)
- 要求实时协作编辑(使用Google Docs)
- 构建幻灯片演示文稿(使用Marp)
- 需要所见即所得编辑(直接使用Word)
- 转换复杂嵌套HTML(可能丢失格式)
Prerequisites
前置条件
Installation
安装
bash
undefinedbash
undefinedmacOS (Homebrew)
macOS (Homebrew)
brew install pandoc
brew install pandoc-crossref # For cross-references
brew install basictex # Minimal LaTeX for PDF
brew install pandoc
brew install pandoc-crossref # 用于交叉引用
brew install basictex # 生成PDF所需的最小LaTeX环境
Or full LaTeX: brew install --cask mactex
或完整LaTeX环境:brew install --cask mactex
Ubuntu/Debian
Ubuntu/Debian
sudo apt-get update
sudo apt-get install pandoc pandoc-citeproc
sudo apt-get install texlive-xetex texlive-fonts-recommended
sudo apt-get install texlive-latex-extra # For additional packages
sudo apt-get update
sudo apt-get install pandoc pandoc-citeproc
sudo apt-get install texlive-xetex texlive-fonts-recommended
sudo apt-get install texlive-latex-extra # 用于额外包
Fedora/RHEL
Fedora/RHEL
sudo dnf install pandoc pandoc-citeproc
sudo dnf install texlive-xetex texlive-collection-fontsrecommended
sudo dnf install pandoc pandoc-citeproc
sudo dnf install texlive-xetex texlive-collection-fontsrecommended
Windows (Chocolatey)
Windows (Chocolatey)
choco install pandoc
choco install miktex # LaTeX distribution
choco install pandoc
choco install miktex # LaTeX发行版
Windows (Scoop)
Windows (Scoop)
scoop install pandoc
scoop install latex
scoop install pandoc
scoop install latex
Verify installation
验证安装
pandoc --version
undefinedpandoc --version
undefinedSystem Requirements
系统要求
- Pandoc 2.19 or higher (3.x recommended)
- LaTeX distribution (for PDF output)
- Python 3.8+ (for pandoc-filters)
- Pandoc 2.19或更高版本(推荐3.x)
- LaTeX发行版(用于PDF输出)
- Python 3.8+(用于pandoc-filters)
Core Capabilities
核心功能
1. Basic Format Conversion
1. 基础格式转换
bash
undefinedbash
undefinedMarkdown to PDF
Markdown转PDF
pandoc document.md -o document.pdf
pandoc document.md -o document.pdf
Markdown to DOCX
Markdown转DOCX
pandoc document.md -o document.docx
pandoc document.md -o document.docx
Markdown to HTML
Markdown转HTML
pandoc document.md -o document.html --standalone
pandoc document.md -o document.html --standalone
Markdown to LaTeX
Markdown转LaTeX
pandoc document.md -o document.tex
pandoc document.md -o document.tex
HTML to Markdown
HTML转Markdown
pandoc page.html -o page.md
pandoc page.html -o page.md
DOCX to Markdown
DOCX转Markdown
pandoc document.docx -o document.md
pandoc document.docx -o document.md
Multiple input files
多输入文件
pandoc chapter1.md chapter2.md chapter3.md -o book.pdf
pandoc chapter1.md chapter2.md chapter3.md -o book.pdf
Specify input format explicitly
显式指定输入格式
pandoc -f markdown -t pdf document.md -o document.pdf
undefinedpandoc -f markdown -t pdf document.md -o document.pdf
undefined2. PDF Generation with Options
2. 带选项的PDF生成
bash
undefinedbash
undefinedBasic PDF with table of contents
带目录的基础PDF
pandoc document.md -o document.pdf --toc
pandoc document.md -o document.pdf --toc
PDF with XeLaTeX engine (better font support)
使用XeLaTeX引擎的PDF(更好的字体支持)
pandoc document.md -o document.pdf
--pdf-engine=xelatex
--toc
--toc-depth=3
--pdf-engine=xelatex
--toc
--toc-depth=3
pandoc document.md -o document.pdf
--pdf-engine=xelatex
--toc
--toc-depth=3
--pdf-engine=xelatex
--toc
--toc-depth=3
PDF with custom margins
自定义边距的PDF
pandoc document.md -o document.pdf
--pdf-engine=xelatex
-V geometry:margin=1in
--pdf-engine=xelatex
-V geometry:margin=1in
pandoc document.md -o document.pdf
--pdf-engine=xelatex
-V geometry:margin=1in
--pdf-engine=xelatex
-V geometry:margin=1in
PDF with custom fonts
自定义字体的PDF
pandoc document.md -o document.pdf
--pdf-engine=xelatex
-V mainfont="Georgia"
-V sansfont="Helvetica"
-V monofont="Menlo"
--pdf-engine=xelatex
-V mainfont="Georgia"
-V sansfont="Helvetica"
-V monofont="Menlo"
pandoc document.md -o document.pdf
--pdf-engine=xelatex
-V mainfont="Georgia"
-V sansfont="Helvetica"
-V monofont="Menlo"
--pdf-engine=xelatex
-V mainfont="Georgia"
-V sansfont="Helvetica"
-V monofont="Menlo"
PDF with paper size and font size
指定纸张大小和字体大小的PDF
pandoc document.md -o document.pdf
--pdf-engine=xelatex
-V papersize=a4
-V fontsize=11pt
--pdf-engine=xelatex
-V papersize=a4
-V fontsize=11pt
pandoc document.md -o document.pdf
--pdf-engine=xelatex
-V papersize=a4
-V fontsize=11pt
--pdf-engine=xelatex
-V papersize=a4
-V fontsize=11pt
PDF with numbered sections
带编号章节的PDF
pandoc document.md -o document.pdf
--number-sections
--toc
--number-sections
--toc
pandoc document.md -o document.pdf
--number-sections
--toc
--number-sections
--toc
PDF with syntax highlighting style
带语法高亮样式的PDF
pandoc document.md -o document.pdf
--highlight-style=tango
--highlight-style=tango
pandoc document.md -o document.pdf
--highlight-style=tango
--highlight-style=tango
List available highlighting styles
列出可用的高亮样式
pandoc --list-highlight-styles
undefinedpandoc --list-highlight-styles
undefined3. Word Document (DOCX) Generation
3. Word文档(DOCX)生成
bash
undefinedbash
undefinedBasic DOCX
基础DOCX
pandoc document.md -o document.docx
pandoc document.md -o document.docx
DOCX with table of contents
带目录的DOCX
pandoc document.md -o document.docx --toc
pandoc document.md -o document.docx --toc
DOCX with reference document (template)
使用参考文档(模板)的DOCX
pandoc document.md -o document.docx
--reference-doc=template.docx
--reference-doc=template.docx
pandoc document.md -o document.docx
--reference-doc=template.docx
--reference-doc=template.docx
DOCX with syntax highlighting
带语法高亮的DOCX
pandoc document.md -o document.docx
--highlight-style=kate
--highlight-style=kate
pandoc document.md -o document.docx
--highlight-style=kate
--highlight-style=kate
Creating a reference document template
创建参考文档模板
pandoc --print-default-data-file reference.docx > template.docx
pandoc --print-default-data-file reference.docx > template.docx
Edit template.docx in Word to customize styles
在Word中编辑template.docx以自定义样式
undefinedundefined4. HTML Generation
4. HTML生成
bash
undefinedbash
undefinedStandalone HTML (includes head, body)
独立HTML(包含head、body)
pandoc document.md -o document.html --standalone
pandoc document.md -o document.html --standalone
HTML with custom CSS
带自定义CSS的HTML
pandoc document.md -o document.html
--standalone
--css=styles.css
--standalone
--css=styles.css
pandoc document.md -o document.html
--standalone
--css=styles.css
--standalone
--css=styles.css
HTML with embedded CSS
嵌入CSS的HTML
pandoc document.md -o document.html
--standalone
--css=styles.css
--embed-resources
--self-contained
--standalone
--css=styles.css
--embed-resources
--self-contained
pandoc document.md -o document.html
--standalone
--css=styles.css
--embed-resources
--self-contained
--standalone
--css=styles.css
--embed-resources
--self-contained
HTML with syntax highlighting
带语法高亮的HTML
pandoc document.md -o document.html
--standalone
--highlight-style=pygments
--standalone
--highlight-style=pygments
pandoc document.md -o document.html
--standalone
--highlight-style=pygments
--standalone
--highlight-style=pygments
HTML with table of contents
带目录的HTML
pandoc document.md -o document.html
--standalone
--toc
--toc-depth=2
--standalone
--toc
--toc-depth=2
pandoc document.md -o document.html
--standalone
--toc
--toc-depth=2
--standalone
--toc
--toc-depth=2
HTML with math rendering (MathJax)
带数学公式渲染的HTML(MathJax)
pandoc document.md -o document.html
--standalone
--mathjax
--standalone
--mathjax
pandoc document.md -o document.html
--standalone
--mathjax
--standalone
--mathjax
HTML5 output
HTML5输出
pandoc document.md -o document.html
--standalone
-t html5
--standalone
-t html5
undefinedpandoc document.md -o document.html
--standalone
-t html5
--standalone
-t html5
undefined5. Custom LaTeX Templates
5. 自定义LaTeX模板
latex
%% template.tex - Custom Pandoc LaTeX template
\documentclass[$if(fontsize)$$fontsize$,$endif$$if(papersize)$$papersize$paper,$endif$]{article}
%% Packages
\usepackage{geometry}
\geometry{margin=1in}
\usepackage{fontspec}
\usepackage{hyperref}
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{listings}
%% Fonts
$if(mainfont)$
\setmainfont{$mainfont$}
$endif$
$if(sansfont)$
\setsansfont{$sansfont$}
$endif$
$if(monofont)$
\setmonofont{$monofont$}
$endif$
%% Colors
\definecolor{linkcolor}{RGB}{0, 102, 204}
\definecolor{codebackground}{RGB}{248, 248, 248}
%% Hyperlinks
\hypersetup{
colorlinks=true,
linkcolor=linkcolor,
urlcolor=linkcolor,
pdfauthor={$author$},
pdftitle={$title$}
}
%% Headers and footers
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{$title$}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
%% Code blocks
\lstset{
backgroundcolor=\color{codebackground},
basicstyle=\ttfamily\small,
breaklines=true,
frame=single,
numbers=left,
numberstyle=\tiny\color{gray}
}
%% Section formatting
\titleformat{\section}
{\Large\bfseries\color{linkcolor}}
{\thesection}{1em}{}
\titleformat{\subsection}
{\large\bfseries}
{\thesubsection}{1em}{}
%% Title
$if(title)$
\title{$title$}
$endif$
$if(author)$
\author{$author$}
$endif$
$if(date)$
\date{$date$}
$endif$
\begin{document}
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$
$if(toc)$
\tableofcontents
\newpage
$endif$
$body$
\end{document}bash
undefinedlatex
%% template.tex - Custom Pandoc LaTeX template
\documentclass[$if(fontsize)$$fontsize$,$endif$$if(papersize)$$papersize$paper,$endif$]{article}
%% Packages
\usepackage{geometry}
\geometry{margin=1in}
\usepackage{fontspec}
\usepackage{hyperref}
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{listings}
%% Fonts
$if(mainfont)$
\setmainfont{$mainfont$}
$endif$
$if(sansfont)$
\setsansfont{$sansfont$}
$endif$
$if(monofont)$
\setmonofont{$monofont$}
$endif$
%% Colors
\definecolor{linkcolor}{RGB}{0, 102, 204}
\definecolor{codebackground}{RGB}{248, 248, 248}
%% Hyperlinks
\hypersetup{
colorlinks=true,
linkcolor=linkcolor,
urlcolor=linkcolor,
pdfauthor={$author$},
pdftitle={$title$}
}
%% Headers and footers
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{$title$}
\fancyhead[R]{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
%% Code blocks
\lstset{
backgroundcolor=\color{codebackground},
basicstyle=\ttfamily\small,
breaklines=true,
frame=single,
numbers=left,
numberstyle=\tiny\color{gray}
}
%% Section formatting
\titleformat{\section}
{\Large\bfseries\color{linkcolor}}
{\thesection}{1em}{}
\titleformat{\subsection}
{\large\bfseries}
{\thesubsection}{1em}{}
%% Title
$if(title)$
\title{$title$}
$endif$
$if(author)$
\author{$author$}
$endif$
$if(date)$
\date{$date$}
$endif$
\begin{document}
$if(title)$
\maketitle
$endif$
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$
$if(toc)$
\tableofcontents
\newpage
$endif$
$body$
\end{document}bash
undefinedUse custom template
使用自定义模板
pandoc document.md -o document.pdf
--template=template.tex
--pdf-engine=xelatex
-V title="My Document"
-V author="Your Name"
-V date="2026-01-17"
--toc
--template=template.tex
--pdf-engine=xelatex
-V title="My Document"
-V author="Your Name"
-V date="2026-01-17"
--toc
undefinedpandoc document.md -o document.pdf
--template=template.tex
--pdf-engine=xelatex
-V title="My Document"
-V author="Your Name"
-V date="2026-01-17"
--toc
--template=template.tex
--pdf-engine=xelatex
-V title="My Document"
-V author="Your Name"
-V date="2026-01-17"
--toc
undefined6. YAML Metadata in Documents
6. 文档中的YAML元数据
markdown
---
title: "Technical Report"
author:
- name: "John Smith"
affiliation: "University of Example"
email: "john@example.edu"
- name: "Jane Doe"
affiliation: "Tech Corp"
date: "January 17, 2026"
abstract: |
This document demonstrates advanced Pandoc features
including custom metadata, citations, and formatting.
keywords:
- documentation
- pandoc
- markdown
lang: en-US
toc: true
toc-depth: 3
numbersections: true
geometry: margin=1in
fontsize: 11pt
mainfont: "Georgia"
monofont: "Fira Code"
linkcolor: blue
bibliography: references.bib
csl: ieee.csl
---markdown
---
title: "技术报告"
author:
- name: "John Smith"
affiliation: "示例大学"
email: "john@example.edu"
- name: "Jane Doe"
affiliation: "科技公司"
date: "2026年1月17日"
abstract: |
本文档展示了Pandoc的高级功能,
包括自定义元数据、引用和格式设置。
keywords:
- 文档编制
- pandoc
- markdown
lang: en-US
toc: true
toc-depth: 3
numbersections: true
geometry: margin=1in
fontsize: 11pt
mainfont: "Georgia"
monofont: "Fira Code"
linkcolor: blue
bibliography: references.bib
csl: ieee.csl
---Introduction
引言
Your document content starts here...
undefined文档内容从此处开始...
undefined7. Citations and Bibliography
7. 引用与参考文献
bibtex
%% references.bib
@article{smith2024,
author = {Smith, John and Doe, Jane},
title = {Advanced Documentation Techniques},
journal = {Journal of Technical Writing},
year = {2024},
volume = {15},
number = {3},
pages = {42--58},
doi = {10.1234/jtw.2024.001}
}
@book{johnson2023,
author = {Johnson, Robert},
title = {The Complete Guide to Markdown},
publisher = {Tech Press},
year = {2023},
address = {New York},
isbn = {978-0-123456-78-9}
}
@inproceedings{williams2025,
author = {Williams, Sarah},
title = {Document Automation Best Practices},
booktitle = {Proceedings of DocCon 2025},
year = {2025},
pages = {100--115},
organization = {Documentation Society}
}
@online{pandocmanual,
author = {{Pandoc Contributors}},
title = {Pandoc User's Guide},
year = {2024},
url = {https://pandoc.org/MANUAL.html},
urldate = {2024-01-15}
}markdown
<!-- document.md with citations -->
---
title: "Research Paper"
bibliography: references.bib
csl: apa.csl
---bibtex
%% references.bib
@article{smith2024,
author = {Smith, John and Doe, Jane},
title = {Advanced Documentation Techniques},
journal = {Journal of Technical Writing},
year = {2024},
volume = {15},
number = {3},
pages = {42--58},
doi = {10.1234/jtw.2024.001}
}
@book{johnson2023,
author = {Johnson, Robert},
title = {The Complete Guide to Markdown},
publisher = {Tech Press},
year = {2023},
address = {New York},
isbn = {978-0-123456-78-9}
}
@inproceedings{williams2025,
author = {Williams, Sarah},
title = {Document Automation Best Practices},
booktitle = {Proceedings of DocCon 2025},
year = {2025},
pages = {100--115},
organization = {Documentation Society}
}
@online{pandocmanual,
author = {{Pandoc Contributors}},
title = {Pandoc User's Guide},
year = {2024},
url = {https://pandoc.org/MANUAL.html},
urldate = {2024-01-15}
}markdown
<!-- 带引用的document.md -->
---
title: "研究论文"
bibliography: references.bib
csl: apa.csl
---Literature Review
文献综述
According to @smith2024, documentation is essential for
project success. This aligns with earlier findings
[@johnson2023; @williams2025].
The standard approach uses markdown formatting
[see @pandocmanual, chapter 3].
Multiple citations can be grouped together
[@smith2024; @johnson2023, pp. 15-20].
根据@smith2024的研究,文档编制对项目成功至关重要。这与早期的研究结果一致
[@johnson2023; @williams2025]。
标准方法使用Markdown格式
[参见@pandocmanual,第3章]。
多个引用可以组合在一起
[@smith2024; @johnson2023, pp. 15-20]。
References
参考文献
::: {#refs}
:::
```bash::: {#refs}
:::
```bashGenerate PDF with citations
生成带引用的PDF
pandoc document.md -o document.pdf
--citeproc
--bibliography=references.bib
--csl=apa.csl
--pdf-engine=xelatex
--citeproc
--bibliography=references.bib
--csl=apa.csl
--pdf-engine=xelatex
pandoc document.md -o document.pdf
--citeproc
--bibliography=references.bib
--csl=apa.csl
--pdf-engine=xelatex
--citeproc
--bibliography=references.bib
--csl=apa.csl
--pdf-engine=xelatex
Download CSL styles
下载CSL样式
8. Cross-References with pandoc-crossref
8. 使用pandoc-crossref进行交叉引用
bash
undefinedbash
undefinedInstall pandoc-crossref
安装pandoc-crossref
macOS
macOS
brew install pandoc-crossref
brew install pandoc-crossref
Or download from releases
或从发行版下载
```markdown
<!-- document.md with cross-references -->
---
title: "Document with Cross-References"
---
```markdown
<!-- 带交叉引用的document.md -->
---
title: "带交叉引用的文档"
---Introduction
引言
See @fig:architecture for the system overview.
The data flow is described in @sec:dataflow.
Results are shown in @tbl:results.
The equation @eq:formula describes the relationship.
系统概述请参见@fig:architecture。
数据流在@sec:dataflow中描述。
结果显示在@tbl:results中。
公式@eq:formula描述了相关关系。
System Architecture {#sec:architecture}
系统架构 {#sec:architecture}
{#fig:architecture}
{#fig:architecture}Data Flow {#sec:dataflow}
数据流 {#sec:dataflow}
The process follows these steps...
流程遵循以下步骤...
Results
结果
| Metric | Value | Unit |
|---|---|---|
| Speed | 100 | ms |
| Memory | 256 | MB |
: Performance metrics {#tbl:results}
| 指标 | 数值 | 单位 |
|---|---|---|
| 速度 | 100 | ms |
| 内存 | 256 | MB |
: 性能指标 {#tbl:results}
Mathematical Model
数学模型
The core formula is:
$$ E = mc^2 $$ {#eq:formula}
Equation @eq:formula shows Einstein's famous equation.
```bash核心公式为:
$$ E = mc^2 $$ {#eq:formula}
公式@eq:formula展示了爱因斯坦的著名方程。
```bashGenerate PDF with cross-references
生成带交叉引用的PDF
pandoc document.md -o document.pdf
--filter pandoc-crossref
--citeproc
--pdf-engine=xelatex
--number-sections
--filter pandoc-crossref
--citeproc
--pdf-engine=xelatex
--number-sections
pandoc document.md -o document.pdf
--filter pandoc-crossref
--citeproc
--pdf-engine=xelatex
--number-sections
--filter pandoc-crossref
--citeproc
--pdf-engine=xelatex
--number-sections
pandoc-crossref options in YAML
YAML中的pandoc-crossref选项
---
---
figureTitle: "Figure"
figureTitle: "Figure"
tableTitle: "Table"
tableTitle: "Table"
listingTitle: "Listing"
listingTitle: "Listing"
figPrefix: "Fig."
figPrefix: "Fig."
tblPrefix: "Table"
tblPrefix: "Table"
eqnPrefix: "Eq."
eqnPrefix: "Eq."
secPrefix: "Section"
secPrefix: "Section"
---
---
undefinedundefined9. Lua Filters
9. Lua过滤器
lua
-- filters/word-count.lua
-- Count words in document
local word_count = 0
function Str(el)
word_count = word_count + 1
return el
end
function Pandoc(doc)
print("Word count: " .. word_count)
return doc
endlua
-- filters/uppercase-headers.lua
-- Convert all headers to uppercase
function Header(el)
return pandoc.walk_block(el, {
Str = function(s)
return pandoc.Str(string.upper(s.text))
end
})
endlua
-- filters/remove-links.lua
-- Remove all hyperlinks, keeping text
function Link(el)
return el.content
endlua
-- filters/custom-blocks.lua
-- Convert custom div blocks to styled output
function Div(el)
if el.classes:includes("warning") then
-- For LaTeX output
local latex_begin = pandoc.RawBlock('latex',
'\\begin{tcolorbox}[colback=yellow!10,colframe=orange]')
local latex_end = pandoc.RawBlock('latex', '\\end{tcolorbox}')
table.insert(el.content, 1, latex_begin)
table.insert(el.content, latex_end)
return el.content
end
if el.classes:includes("info") then
local latex_begin = pandoc.RawBlock('latex',
'\\begin{tcolorbox}[colback=blue!5,colframe=blue!50]')
local latex_end = pandoc.RawBlock('latex', '\\end{tcolorbox}')
table.insert(el.content, 1, latex_begin)
table.insert(el.content, latex_end)
return el.content
end
endlua
-- filters/include-files.lua
-- Include content from external files
function CodeBlock(el)
if el.classes:includes("include") then
local file = io.open(el.text, "r")
if file then
local content = file:read("*all")
file:close()
-- Get file extension for syntax highlighting
local ext = el.text:match("%.(%w+)$")
local lang = ext or ""
return pandoc.CodeBlock(content, {class = lang})
end
end
endbash
undefinedlua
-- filters/word-count.lua
-- 统计文档字数
local word_count = 0
function Str(el)
word_count = word_count + 1
return el
end
function Pandoc(doc)
print("Word count: " .. word_count)
return doc
endlua
-- filters/uppercase-headers.lua
-- 将所有标题转换为大写
function Header(el)
return pandoc.walk_block(el, {
Str = function(s)
return pandoc.Str(string.upper(s.text))
end
})
endlua
-- filters/remove-links.lua
-- 移除所有超链接,保留文本
function Link(el)
return el.content
endlua
-- filters/custom-blocks.lua
-- 将自定义div块转换为样式化输出
function Div(el)
if el.classes:includes("warning") then
-- 针对LaTeX输出
local latex_begin = pandoc.RawBlock('latex',
'\\begin{tcolorbox}[colback=yellow!10,colframe=orange]')
local latex_end = pandoc.RawBlock('latex', '\\end{tcolorbox}')
table.insert(el.content, 1, latex_begin)
table.insert(el.content, latex_end)
return el.content
end
if el.classes:includes("info") then
local latex_begin = pandoc.RawBlock('latex',
'\\begin{tcolorbox}[colback=blue!5,colframe=blue!50]')
local latex_end = pandoc.RawBlock('latex', '\\end{tcolorbox}')
table.insert(el.content, 1, latex_begin)
table.insert(el.content, latex_end)
return el.content
end
endlua
-- filters/include-files.lua
-- 包含外部文件内容
function CodeBlock(el)
if el.classes:includes("include") then
local file = io.open(el.text, "r")
if file then
local content = file:read("*all")
file:close()
-- 获取文件扩展名用于语法高亮
local ext = el.text:match("%.(%w+)$")
local lang = ext or ""
return pandoc.CodeBlock(content, {class = lang})
end
end
endbash
undefinedUse Lua filters
使用Lua过滤器
pandoc document.md -o document.pdf
--lua-filter=filters/uppercase-headers.lua
--lua-filter=filters/custom-blocks.lua
--lua-filter=filters/uppercase-headers.lua
--lua-filter=filters/custom-blocks.lua
pandoc document.md -o document.pdf
--lua-filter=filters/uppercase-headers.lua
--lua-filter=filters/custom-blocks.lua
--lua-filter=filters/uppercase-headers.lua
--lua-filter=filters/custom-blocks.lua
Chain multiple filters
链式使用多个过滤器
pandoc document.md -o document.pdf
--filter pandoc-crossref
--lua-filter=filters/custom-blocks.lua
--citeproc
--filter pandoc-crossref
--lua-filter=filters/custom-blocks.lua
--citeproc
undefinedpandoc document.md -o document.pdf
--filter pandoc-crossref
--lua-filter=filters/custom-blocks.lua
--citeproc
--filter pandoc-crossref
--lua-filter=filters/custom-blocks.lua
--citeproc
undefined10. Batch Conversion Scripts
10. 批量转换脚本
bash
#!/bin/bashbash
#!/bin/bashscripts/batch-convert.sh
scripts/batch-convert.sh
Convert all Markdown files to PDF
将所有Markdown文件转换为PDF
set -euo pipefail
set -euo pipefail
Configuration
配置
INPUT_DIR="${1:-./docs}"
OUTPUT_DIR="${2:-./output}"
TEMPLATE="${3:-}"
INPUT_DIR="${1:-./docs}"
OUTPUT_DIR="${2:-./output}"
TEMPLATE="${3:-}"
Create output directory
创建输出目录
mkdir -p "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"
Find and convert all markdown files
查找并转换所有Markdown文件
find "$INPUT_DIR" -name "*.md" -type f | while read -r file; do
# Get relative path and create output path
relative="${file#$INPUT_DIR/}"
output_file="$OUTPUT_DIR/${relative%.md}.pdf"
output_dir=$(dirname "$output_file")
# Create output subdirectory
mkdir -p "$output_dir"
echo "Converting: $file -> $output_file"
# Build pandoc command
cmd=(pandoc "$file" -o "$output_file"
--pdf-engine=xelatex
--toc
--number-sections
--highlight-style=tango)
# Add template if specified
if [[ -n "$TEMPLATE" ]]; then
cmd+=(--template="$TEMPLATE")
fi
# Execute conversion
"${cmd[@]}"done
echo "Batch conversion complete!"
echo "Output: $OUTPUT_DIR"
```bash
#!/bin/bashfind "$INPUT_DIR" -name "*.md" -type f | while read -r file; do
# 获取相对路径并创建输出路径
relative="${file#$INPUT_DIR/}"
output_file="$OUTPUT_DIR/${relative%.md}.pdf"
output_dir=$(dirname "$output_file")
# 创建输出子目录
mkdir -p "$output_dir"
echo "正在转换: $file -> $output_file"
# 构建pandoc命令
cmd=(pandoc "$file" -o "$output_file"
--pdf-engine=xelatex
--toc
--number-sections
--highlight-style=tango)
# 如果指定了模板则添加
if [[ -n "$TEMPLATE" ]]; then
cmd+=(--template="$TEMPLATE")
fi
# 执行转换
"${cmd[@]}"done
echo "批量转换完成!"
echo "输出目录: $OUTPUT_DIR"
```bash
#!/bin/bashscripts/convert-to-all-formats.sh
scripts/convert-to-all-formats.sh
Convert a document to multiple formats
将文档转换为多种格式
set -euo pipefail
INPUT_FILE="${1:?Usage: $0 <input.md>}"
BASE_NAME="${INPUT_FILE%.md}"
echo "Converting $INPUT_FILE to multiple formats..."
set -euo pipefail
INPUT_FILE="${1:?用法: $0 <input.md>}"
BASE_NAME="${INPUT_FILE%.md}"
echo "正在将$INPUT_FILE转换为多种格式..."
echo " -> PDF"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.pdf"
--pdf-engine=xelatex
--toc
--number-sections
--pdf-engine=xelatex
--toc
--number-sections
echo " -> PDF"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.pdf"
--pdf-engine=xelatex
--toc
--number-sections
--pdf-engine=xelatex
--toc
--number-sections
DOCX
DOCX
echo " -> DOCX"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.docx"
--toc
--toc
echo " -> DOCX"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.docx"
--toc
--toc
HTML
HTML
echo " -> HTML"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.html"
--standalone
--toc
--embed-resources
--standalone
--toc
--embed-resources
echo " -> HTML"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.html"
--standalone
--toc
--embed-resources
--standalone
--toc
--embed-resources
LaTeX
LaTeX
echo " -> LaTeX"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.tex"
echo " -> LaTeX"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.tex"
EPUB
EPUB
echo " -> EPUB"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.epub"
--toc
--toc
echo "Done! Created:"
ls -la "${BASE_NAME}".*
```python
#!/usr/bin/env python3
"""
scripts/smart_convert.py
Smart document converter with configuration file support.
"""
import subprocess
import sys
from pathlib import Path
import yaml
def load_config(config_path: Path) -> dict:
"""Load conversion configuration from YAML."""
with open(config_path) as f:
return yaml.safe_load(f)
def convert_document(
input_file: Path,
output_file: Path,
config: dict
) -> bool:
"""Convert a single document using pandoc."""
cmd = ['pandoc', str(input_file), '-o', str(output_file)]
# Add common options
if config.get('toc'):
cmd.append('--toc')
if toc_depth := config.get('toc_depth'):
cmd.extend(['--toc-depth', str(toc_depth)])
if config.get('number_sections'):
cmd.append('--number-sections')
if template := config.get('template'):
cmd.extend(['--template', template])
if pdf_engine := config.get('pdf_engine'):
cmd.extend(['--pdf-engine', pdf_engine])
if highlight := config.get('highlight_style'):
cmd.extend(['--highlight-style', highlight])
if bibliography := config.get('bibliography'):
cmd.append('--citeproc')
cmd.extend(['--bibliography', bibliography])
if csl := config.get('csl'):
cmd.extend(['--csl', csl])
# Add variables
for key, value in config.get('variables', {}).items():
cmd.extend(['-V', f'{key}={value}'])
# Add filters
for filter_name in config.get('filters', []):
if filter_name.endswith('.lua'):
cmd.extend(['--lua-filter', filter_name])
else:
cmd.extend(['--filter', filter_name])
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"Error: {result.stderr}", file=sys.stderr)
return False
return True
def main():
if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} <input.md> <output.pdf> [config.yaml]")
sys.exit(1)
input_file = Path(sys.argv[1])
output_file = Path(sys.argv[2])
config_file = Path(sys.argv[3]) if len(sys.argv) > 3 else None
config = {}
if config_file and config_file.exists():
config = load_config(config_file)
success = convert_document(input_file, output_file, config)
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()yaml
undefinedecho " -> EPUB"
pandoc "$INPUT_FILE" -o "${BASE_NAME}.epub"
--toc
--toc
echo "完成!已创建:"
ls -la "${BASE_NAME}".*
```python
#!/usr/bin/env python3
"""
scripts/smart_convert.py
支持配置文件的智能文档转换器。
"""
import subprocess
import sys
from pathlib import Path
import yaml
def load_config(config_path: Path) -> dict:
"""从YAML加载转换配置。"""
with open(config_path) as f:
return yaml.safe_load(f)
def convert_document(
input_file: Path,
output_file: Path,
config: dict
) -> bool:
"""使用pandoc转换单个文档。"""
cmd = ['pandoc', str(input_file), '-o', str(output_file)]
# 添加通用选项
if config.get('toc'):
cmd.append('--toc')
if toc_depth := config.get('toc_depth'):
cmd.extend(['--toc-depth', str(toc_depth)])
if config.get('number_sections'):
cmd.append('--number-sections')
if template := config.get('template'):
cmd.extend(['--template', template])
if pdf_engine := config.get('pdf_engine'):
cmd.extend(['--pdf-engine', pdf_engine])
if highlight := config.get('highlight_style'):
cmd.extend(['--highlight-style', highlight])
if bibliography := config.get('bibliography'):
cmd.append('--citeproc')
cmd.extend(['--bibliography', bibliography])
if csl := config.get('csl'):
cmd.extend(['--csl', csl])
# 添加变量
for key, value in config.get('variables', {}).items():
cmd.extend(['-V', f'{key}={value}'])
# 添加过滤器
for filter_name in config.get('filters', []):
if filter_name.endswith('.lua'):
cmd.extend(['--lua-filter', filter_name])
else:
cmd.extend(['--filter', filter_name])
print(f"正在执行: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"错误: {result.stderr}", file=sys.stderr)
return False
return True
def main():
if len(sys.argv) < 3:
print(f"用法: {sys.argv[0]} <input.md> <output.pdf> [config.yaml]")
sys.exit(1)
input_file = Path(sys.argv[1])
output_file = Path(sys.argv[2])
config_file = Path(sys.argv[3]) if len(sys.argv) > 3 else None
config = {}
if config_file and config_file.exists():
config = load_config(config_file)
success = convert_document(input_file, output_file, config)
sys.exit(0 if success else 1)
if __name__ == '__main__':
main()yaml
undefinedconfig/pandoc-config.yaml
config/pandoc-config.yaml
Configuration for smart_convert.py
smart_convert.py的配置文件
toc: true
toc_depth: 3
number_sections: true
pdf_engine: xelatex
highlight_style: tango
template: templates/report.tex
bibliography: references/main.bib
csl: styles/ieee.csl
variables:
geometry: margin=1in
fontsize: 11pt
mainfont: Georgia
monofont: Fira Code
linkcolor: blue
filters:
- pandoc-crossref
- filters/custom-blocks.lua
undefinedtoc: true
toc_depth: 3
number_sections: true
pdf_engine: xelatex
highlight_style: tango
template: templates/report.tex
bibliography: references/main.bib
csl: styles/ieee.csl
variables:
geometry: margin=1in
fontsize: 11pt
mainfont: Georgia
monofont: Fira Code
linkcolor: blue
filters:
- pandoc-crossref
- filters/custom-blocks.lua
undefined11. Makefile for Document Projects
11. 文档项目的Makefile
makefile
undefinedmakefile
undefinedMakefile for document conversion project
文档转换项目的Makefile
Configuration
配置
PANDOC = pandoc
PDF_ENGINE = xelatex
TEMPLATE = templates/report.tex
BIBLIOGRAPHY = references/main.bib
CSL = styles/ieee.csl
PANDOC = pandoc
PDF_ENGINE = xelatex
TEMPLATE = templates/report.tex
BIBLIOGRAPHY = references/main.bib
CSL = styles/ieee.csl
Directories
目录
SRC_DIR = src
OUT_DIR = output
BUILD_DIR = build
SRC_DIR = src
OUT_DIR = output
BUILD_DIR = build
Source files
源文件
MD_FILES := $(wildcard $(SRC_DIR)/*.md)
PDF_FILES := $(patsubst $(SRC_DIR)/%.md,$(OUT_DIR)/%.pdf,$(MD_FILES))
DOCX_FILES := $(patsubst $(SRC_DIR)/%.md,$(OUT_DIR)/%.docx,$(MD_FILES))
HTML_FILES := $(patsubst $(SRC_DIR)/%.md,$(OUT_DIR)/%.html,$(MD_FILES))
MD_FILES := $(wildcard $(SRC_DIR)/*.md)
PDF_FILES := $(patsubst $(SRC_DIR)/%.md,$(OUT_DIR)/%.pdf,$(MD_FILES))
DOCX_FILES := $(patsubst $(SRC_DIR)/%.md,$(OUT_DIR)/%.docx,$(MD_FILES))
HTML_FILES := $(patsubst $(SRC_DIR)/%.md,$(OUT_DIR)/%.html,$(MD_FILES))
Common options
通用选项
PANDOC_OPTS = --toc --number-sections --highlight-style=tango
PDF_OPTS = --pdf-engine=$(PDF_ENGINE) --template=$(TEMPLATE)
CITE_OPTS = --citeproc --bibliography=$(BIBLIOGRAPHY) --csl=$(CSL)
PANDOC_OPTS = --toc --number-sections --highlight-style=tango
PDF_OPTS = --pdf-engine=$(PDF_ENGINE) --template=$(TEMPLATE)
CITE_OPTS = --citeproc --bibliography=$(BIBLIOGRAPHY) --csl=$(CSL)
Phony targets
伪目标
.PHONY: all pdf docx html clean help
.PHONY: all pdf docx html clean help
Default target
默认目标
all: pdf
all: pdf
Build all PDFs
构建所有PDF
pdf: $(PDF_FILES)
pdf: $(PDF_FILES)
Build all DOCX
构建所有DOCX
docx: $(DOCX_FILES)
docx: $(DOCX_FILES)
Build all HTML
构建所有HTML
html: $(HTML_FILES)
html: $(HTML_FILES)
Pattern rules
模式规则
$(OUT_DIR)/%.pdf: $(SRC_DIR)/%.md $(TEMPLATE) $(BIBLIOGRAPHY) | $(OUT_DIR)
@echo "Building PDF: $@"
$(PANDOC) $< -o $@ $(PANDOC_OPTS) $(PDF_OPTS) $(CITE_OPTS)
$(OUT_DIR)/%.docx: $(SRC_DIR)/%.md $(BIBLIOGRAPHY) | $(OUT_DIR)
@echo "Building DOCX: $@"
$(PANDOC) $< -o $@ $(PANDOC_OPTS) $(CITE_OPTS)
$(OUT_DIR)/%.html: $(SRC_DIR)/%.md $(BIBLIOGRAPHY) | $(OUT_DIR)
@echo "Building HTML: $@"
$(PANDOC) $< -o $@ --standalone $(PANDOC_OPTS) $(CITE_OPTS) --embed-resources
$(OUT_DIR)/%.pdf: $(SRC_DIR)/%.md $(TEMPLATE) $(BIBLIOGRAPHY) | $(OUT_DIR)
@echo "正在构建PDF: $@"
$(PANDOC) $< -o $@ $(PANDOC_OPTS) $(PDF_OPTS) $(CITE_OPTS)
$(OUT_DIR)/%.docx: $(SRC_DIR)/%.md $(BIBLIOGRAPHY) | $(OUT_DIR)
@echo "正在构建DOCX: $@"
$(PANDOC) $< -o $@ $(PANDOC_OPTS) $(CITE_OPTS)
$(OUT_DIR)/%.html: $(SRC_DIR)/%.md $(BIBLIOGRAPHY) | $(OUT_DIR)
@echo "正在构建HTML: $@"
$(PANDOC) $< -o $@ --standalone $(PANDOC_OPTS) $(CITE_OPTS) --embed-resources
Create output directory
创建输出目录
$(OUT_DIR):
mkdir -p $(OUT_DIR)
$(OUT_DIR):
mkdir -p $(OUT_DIR)
Clean build artifacts
清理构建产物
clean:
rm -rf $(OUT_DIR)/*
rm -rf $(BUILD_DIR)/*
clean:
rm -rf $(OUT_DIR)/*
rm -rf $(BUILD_DIR)/*
Watch for changes (requires entr)
监听文件变化(需要entr)
watch:
find $(SRC_DIR) -name "*.md" | entr -c make pdf
watch:
find $(SRC_DIR) -name "*.md" | entr -c make pdf
Help
帮助信息
help:
@echo "Available targets:"
@echo " all - Build all PDFs (default)"
@echo " pdf - Build PDF files"
@echo " docx - Build DOCX files"
@echo " html - Build HTML files"
@echo " clean - Remove built files"
@echo " watch - Watch for changes and rebuild"
@echo ""
@echo "Source files: $(MD_FILES)"
undefinedhelp:
@echo "可用目标:"
@echo " all - 构建所有PDF(默认)"
@echo " pdf - 构建PDF文件"
@echo " docx - 构建DOCX文件"
@echo " html - 构建HTML文件"
@echo " clean - 删除已构建文件"
@echo " watch - 监听文件变化并重新构建"
@echo ""
@echo "源文件: $(MD_FILES)"
undefined12. GitHub Actions Workflow
12. GitHub Actions 工作流
yaml
undefinedyaml
undefined.github/workflows/build-docs.yml
.github/workflows/build-docs.yml
name: Build Documents
on:
push:
branches: [main]
paths:
- 'docs/'
- 'templates/'
- '.github/workflows/build-docs.yml'
pull_request:
paths:
- 'docs/'
- 'templates/'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Pandoc
run: |
wget https://github.com/jgm/pandoc/releases/download/3.1.11/pandoc-3.1.11-linux-amd64.tar.gz
tar xzf pandoc-3.1.11-linux-amd64.tar.gz
sudo mv pandoc-3.1.11/bin/* /usr/local/bin/
- name: Install pandoc-crossref
run: |
wget https://github.com/lierdakil/pandoc-crossref/releases/download/v0.3.17.0/pandoc-crossref-Linux.tar.xz
tar xf pandoc-crossref-Linux.tar.xz
sudo mv pandoc-crossref /usr/local/bin/
- name: Install LaTeX
run: |
sudo apt-get update
sudo apt-get install -y texlive-xetex texlive-fonts-recommended \
texlive-latex-extra texlive-fonts-extra
- name: Build PDF documents
run: |
mkdir -p output
for file in docs/*.md; do
output="output/$(basename "${file%.md}.pdf")"
echo "Building: $file -> $output"
pandoc "$file" -o "$output" \
--pdf-engine=xelatex \
--toc \
--number-sections \
--filter pandoc-crossref \
--template=templates/report.tex
done
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: documents
path: output/*.pdfrelease:
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: documents
path: output
- name: Create release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: output/*.pdfundefinedname: Build Documents
on:
push:
branches: [main]
paths:
- 'docs/'
- 'templates/'
- '.github/workflows/build-docs.yml'
pull_request:
paths:
- 'docs/'
- 'templates/'
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 检出代码库
uses: actions/checkout@v4
- name: 安装Pandoc
run: |
wget https://github.com/jgm/pandoc/releases/download/3.1.11/pandoc-3.1.11-linux-amd64.tar.gz
tar xzf pandoc-3.1.11-linux-amd64.tar.gz
sudo mv pandoc-3.1.11/bin/* /usr/local/bin/
- name: 安装pandoc-crossref
run: |
wget https://github.com/lierdakil/pandoc-crossref/releases/download/v0.3.17.0/pandoc-crossref-Linux.tar.xz
tar xf pandoc-crossref-Linux.tar.xz
sudo mv pandoc-crossref /usr/local/bin/
- name: 安装LaTeX
run: |
sudo apt-get update
sudo apt-get install -y texlive-xetex texlive-fonts-recommended \
texlive-latex-extra texlive-fonts-extra
- name: 构建PDF文档
run: |
mkdir -p output
for file in docs/*.md; do
output="output/$(basename "${file%.md}.pdf")"
echo "正在构建: $file -> $output"
pandoc "$file" -o "$output" \
--pdf-engine=xelatex \
--toc \
--number-sections \
--filter pandoc-crossref \
--template=templates/report.tex
done
- name: 上传产物
uses: actions/upload-artifact@v4
with:
name: documents
path: output/*.pdfrelease:
needs: build
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: 下载产物
uses: actions/download-artifact@v4
with:
name: documents
path: output
- name: 创建发布
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: output/*.pdfundefinedIntegration Examples
集成示例
Integration with Git Hooks
与Git Hooks集成
bash
#!/bin/bashbash
#!/bin/bash.git/hooks/pre-commit
.git/hooks/pre-commit
Rebuild PDFs before commit
提交前重新构建PDF
set -e
set -e
Check if any markdown files changed
检查是否有Markdown文件被修改
changed_md=$(git diff --cached --name-only --diff-filter=ACM | grep '.md$' || true)
if [[ -n "$changed_md" ]]; then
echo "Rebuilding PDF documents..."
for file in $changed_md; do
if [[ -f "$file" ]]; then
output="${file%.md}.pdf"
echo " Converting: $file -> $output"
pandoc "$file" -o "$output" --pdf-engine=xelatex --toc
git add "$output"
fi
donefi
undefinedchanged_md=$(git diff --cached --name-only --diff-filter=ACM | grep '.md$' || true)
if [[ -n "$changed_md" ]]; then
echo "正在重新构建PDF文档..."
for file in $changed_md; do
if [[ -f "$file" ]]; then
output="${file%.md}.pdf"
echo " 正在转换: $file -> $output"
pandoc "$file" -o "$output" --pdf-engine=xelatex --toc
git add "$output"
fi
donefi
undefinedIntegration with VS Code Tasks
与VS Code任务集成
json
{
"version": "2.0.0",
"tasks": [
{
"label": "Pandoc: Build PDF",
"type": "shell",
"command": "pandoc",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.pdf",
"--pdf-engine=xelatex",
"--toc",
"--number-sections"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always"
}
},
{
"label": "Pandoc: Build DOCX",
"type": "shell",
"command": "pandoc",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.docx",
"--toc"
]
},
{
"label": "Pandoc: Watch and Build",
"type": "shell",
"command": "find . -name '*.md' | entr -c pandoc ${file} -o ${fileDirname}/${fileBasenameNoExtension}.pdf --pdf-engine=xelatex",
"isBackground": true,
"problemMatcher": []
}
]
}json
{
"version": "2.0.0",
"tasks": [
{
"label": "Pandoc: 构建PDF",
"type": "shell",
"command": "pandoc",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.pdf",
"--pdf-engine=xelatex",
"--toc",
"--number-sections"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always"
}
},
{
"label": "Pandoc: 构建DOCX",
"type": "shell",
"command": "pandoc",
"args": [
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.docx",
"--toc"
]
},
{
"label": "Pandoc: 监听并构建",
"type": "shell",
"command": "find . -name '*.md' | entr -c pandoc ${file} -o ${fileDirname}/${fileBasenameNoExtension}.pdf --pdf-engine=xelatex",
"isBackground": true,
"problemMatcher": []
}
]
}Best Practices
最佳实践
1. Document Structure
1. 文档结构
markdown
---
title: "Document Title"
author: "Author Name"
date: "2026-01-17"
abstract: |
Brief summary of the document content.
---markdown
---
title: "文档标题"
author: "作者姓名"
date: "2026年1月17日"
abstract: |
文档内容的简要概述。
---Introduction
引言
Opening paragraph...
开篇段落...
Background
背景
Context and background information...
背景信息...
Main Content
主要内容
Section One
第一部分
Content...
内容...
Subsection
子部分
More detailed content...
更详细的内容...
Section Two
第二部分
Additional content...
额外内容...
Conclusion
结论
Summary and conclusions...
总结与结论...
References
参考文献
::: {#refs}
:::
::: {#refs}
:::
Appendix A: Additional Data {.appendix}
附录A: 额外数据 {.appendix}
Supplementary material...
undefined补充材料...
undefined2. Image Management
2. 图片管理
markdown
<!-- Recommended image syntax -->
{width=80%}
<!-- With cross-reference -->
{#fig:arch width=100%}
See @fig:arch for the overview.
<!-- Multiple images -->
::: {#fig:comparison layout-ncol=2}
{#fig:before}
{#fig:after}
Comparison of results
:::markdown
<!-- 推荐的图片语法 -->
{width=80%}
<!-- 带交叉引用的图片 -->
{#fig:arch width=100%}
系统概述请参见@fig:arch。
<!-- 多张图片 -->
::: {#fig:comparison layout-ncol=2}
{#fig:before}
{#fig:after}
结果对比
:::3. Code Block Best Practices
3. 代码块最佳实践
markdown
<!-- Named code blocks with line numbers -->
```python {.numberLines startFrom="1"}
def process_data(data: list) -> dict:
"""Process input data and return results."""
results = {}
for item in data:
results[item.id] = transform(item)
return results
```
<!-- Highlighted lines -->
```python {.numberLines hl_lines=[2,4]}
def calculate(x, y):
total = x + y # highlighted
average = total / 2
return average # highlighted
```markdown
<!-- 带行号的命名代码块 -->
```python {.numberLines startFrom="1"}
def process_data(data: list) -> dict:
"""处理输入数据并返回结果。"""
results = {}
for item in data:
results[item.id] = transform(item)
return results
```
<!-- 高亮指定行 -->
```python {.numberLines hl_lines=[2,4]}
def calculate(x, y):
total = x + y # 高亮行
average = total / 2
return average # 高亮行
```4. Table Formatting
4. 表格格式
markdown
<!-- Simple table -->
| Column 1 | Column 2 | Column 3 |
|----------|----------|----------|
| Data 1 | Data 2 | Data 3 |
| Data 4 | Data 5 | Data 6 |
<!-- Table with caption and reference -->
| Metric | Value | Unit |
|--------|-------|------|
| Speed | 100 | ms |
| Memory | 256 | MB |
: Performance metrics {#tbl:perf}
See @tbl:perf for benchmarks.
<!-- Grid tables (more flexible) -->
+---------------+---------------+
| Column 1 | Column 2 |
+===============+===============+
| Multi-line | Another cell |
| content here | |
+---------------+---------------+
| More data | Final cell |
+---------------+---------------+markdown
<!-- 简单表格 -->
| 列1 | 列2 | 列3 |
|----------|----------|----------|
| 数据1 | 数据2 | 数据3 |
| 数据4 | 数据5 | 数据6 |
<!-- 带标题和引用的表格 -->
| 指标 | 数值 | 单位 |
|--------|-------|------|
| 速度 | 100 | ms |
| 内存 | 256 | MB |
: 性能指标 {#tbl:perf}
性能基准请参见@tbl:perf。
<!-- 网格表格(更灵活) -->
+---------------+---------------+
| 列1 | 列2 |
+===============+===============+
| 多行内容 | 另一个单元格 |
| 此处 | |
+---------------+---------------+
| 更多数据 | 最后一个单元格 |
+---------------+---------------+Troubleshooting
故障排除
Common Issues
常见问题
PDF Engine Not Found
PDF引擎未找到
bash
undefinedbash
undefinedCheck if xelatex is installed
检查xelatex是否已安装
which xelatex
which xelatex
Install on Ubuntu
在Ubuntu上安装
sudo apt-get install texlive-xetex
sudo apt-get install texlive-xetex
Use pdflatex instead
使用pdflatex替代
pandoc doc.md -o doc.pdf --pdf-engine=pdflatex
undefinedpandoc doc.md -o doc.pdf --pdf-engine=pdflatex
undefinedMissing LaTeX Packages
缺少LaTeX包
bash
undefinedbash
undefinedInstall specific package (TeX Live)
安装特定包(TeX Live)
tlmgr install <package-name>
tlmgr install <package-name>
Install common packages
安装常用包
sudo apt-get install texlive-latex-extra texlive-fonts-extra
sudo apt-get install texlive-latex-extra texlive-fonts-extra
Check which package provides a file
检查哪个包提供指定文件
tlmgr search --file <filename>
undefinedtlmgr search --file <filename>
undefinedUnicode Characters in PDF
PDF中的Unicode字符问题
bash
undefinedbash
undefinedUse XeLaTeX for Unicode support
使用XeLaTeX支持Unicode
pandoc doc.md -o doc.pdf
--pdf-engine=xelatex
-V mainfont="DejaVu Sans"
--pdf-engine=xelatex
-V mainfont="DejaVu Sans"
undefinedpandoc doc.md -o doc.pdf
--pdf-engine=xelatex
-V mainfont="DejaVu Sans"
--pdf-engine=xelatex
-V mainfont="DejaVu Sans"
undefinedImages Not Found
图片未找到
bash
undefinedbash
undefinedUse resource path
使用资源路径
pandoc doc.md -o doc.pdf
--resource-path=.:images:assets
--resource-path=.:images:assets
pandoc doc.md -o doc.pdf
--resource-path=.:images:assets
--resource-path=.:images:assets
Or use absolute paths in markdown
或在Markdown中使用绝对路径

undefined
undefinedCitations Not Processing
引用未被处理
bash
undefinedbash
undefinedEnsure --citeproc is included
确保包含--citeproc选项
pandoc doc.md -o doc.pdf
--citeproc
--bibliography=refs.bib
--citeproc
--bibliography=refs.bib
pandoc doc.md -o doc.pdf
--citeproc
--bibliography=refs.bib
--citeproc
--bibliography=refs.bib
Check BibTeX file syntax
检查BibTeX文件语法
biber --tool refs.bib
undefinedbiber --tool refs.bib
undefinedDebug Mode
调试模式
bash
undefinedbash
undefinedVerbose output
详细输出
pandoc doc.md -o doc.pdf --verbose
pandoc doc.md -o doc.pdf --verbose
Show intermediate LaTeX
查看中间LaTeX代码
pandoc doc.md -t latex > debug.tex
pandoc doc.md -t latex > debug.tex
Check Pandoc version and features
检查Pandoc版本和功能
pandoc --version
pandoc --version
List supported formats
列出支持的格式
pandoc --list-input-formats
pandoc --list-output-formats
undefinedpandoc --list-input-formats
pandoc --list-output-formats
undefinedVersion History
版本历史
v1.0.0 (2026-01-17)
v1.0.0(2026年1月17日)
- Initial skill creation
- PDF, DOCX, HTML conversion workflows
- Custom LaTeX templates
- Citation management with BibTeX/CSL
- Cross-references with pandoc-crossref
- Lua filters documentation
- Batch conversion scripts
- Makefile integration
- GitHub Actions workflows
- 初始技能创建
- PDF、DOCX、HTML转换工作流
- 自定义LaTeX模板
- 使用BibTeX/CSL的引用管理
- 使用pandoc-crossref的交叉引用
- Lua过滤器文档
- 批量转换脚本
- Makefile集成
- GitHub Actions工作流
Related Resources
相关资源
- Pandoc User's Guide
- Pandoc Lua Filters
- Citation Style Language
- pandoc-crossref
- LaTeX Templates
- Zotero Integration
Convert documents between any formats with Pandoc - the universal document converter.