Pandoc 与 LaTex
安装 Pandoc
Pandoc 是一个文档转换工具,可以实现 md
转 docx
或 pdf
。
Sudo pacman -Syu pandoc
安装 TexLive
使用 Tex 的核心组件,安装 Arch Linux 中的包组 texlive
即可。
sudo pacman -Syu texlive
LaTex 使用示例
编写 test.tex
文件:
\documentclass{article}
\begin{document}
Hello World!
\end{document}
编译:
latex test.tex
查看:
xdvi test.dvi
也可以直接生成 pdf
文件:
pdflatex test.tex
详细教程参考WIKI
Pandoc 使用示例
创建了一个样例文件 sample.md
:
# Sample Markdown Document
This is a **comprehensive sample** of Markdown syntax to test conversion tools like *Pandoc*. It includes headings, lists, code, blockquotes, tables, links, images, footnotes, math, and more.
---
## Table of Contents
1. [Headings](#headings)
2. [Text Formatting](#text-formatting)
3. [Lists](#lists)
4. [Blockquotes](#blockquotes)
5. [Code](#code)
6. [Tables](#tables)
7. [Links & Images](#links--images)
8. [Footnotes](#footnotes)
9. [Horizontal Rules](#horizontal-rules)
10. [Math](#math)
11. [HTML](#html)
12. [Task Lists](#task-lists)
---
## Headings
# H1 Heading
## H2 Heading
### H3 Heading
#### H4 Heading
##### H5 Heading
###### H6 Heading
---
## Text Formatting
- **Bold**
- *Italic*
- ***Bold and Italic***
- ~~Strikethrough~~
- <u>Underline (via HTML)</u>
- ==Highlight (not native, Pandoc-supported)==
- `Inline code`
---
## Lists
### Unordered List
- Item A
- Subitem A.1
- Sub-subitem A.1.a
- Item B
### Ordered List
1. First item
2. Second item
a. Subitem
b. Another subitem
3. Third item
---
## Blockquotes
> This is a blockquote.
>
> > Nested blockquote.
>
> Another line in the same quote.
---
## Code
### Inline Code
Use the `printf()` function.
### Fenced Code Blocks
def greet(name):
print(f"Hello, {name}!")
!/bin/bash
echo "Hello, world"
console.log("Hello, JavaScript!");
---
## Tables
| Syntax | Description | Output |
|--------|-------------|--------|
| Header | Title | Text |
| `code` | *Italic* | **Bold** |
Right-aligned:
| ID | Name | Score |
|----|---------|------:|
| 1 | Alice | 90 |
| 2 | Bob | 85 |
| 3 | Charlie | 95 |
---
## Links & Images
### Link
[OpenAI](https://www.openai.com)
<https://www.example.com>
### Image

---
## Footnotes
Here is a sentence with a footnote.[^1]
[^1]: This is the footnote definition.
---
## Horizontal Rules
---
---
## Math
Inline: $E = mc^2$
Block:
$$
\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
$$
---
## HTML
<div style="color: blue;">
This is an HTML block inside Markdown.
</div>
---
## Task Lists
- [x] Write a sample file
- [x] Include all major syntax
- [ ] Test with Pandoc
- [ ] Export to PDF/HTML/Docx
---
_End of document._
---
## 中文测试段落
这是一个用于测试 **Pandoc** 是否支持中文的段落。
中文支持对于中文用户来说非常重要。你可以尝试将此文档导出为 PDF 或 DOCX,看看字体是否正常显示、排版是否美观。
以下是一些常见的中文格式测试:
- **加粗文字**
- *斜体文字*
- ~~删除线文字~~
- `行内代码`
### 中文列表
1. 第一步:安装 Pandoc;
2. 第二步:准备 Markdown 文件;
3. 第三步:运行转换命令。
- 项目 A
- 子项目 A.1
- 项目 B
### 中文段落示例
在很久很久以前,有一个叫做 Markdown 的标记语言,它简洁、优雅、易于使用。在中文世界中,它也越来越受欢迎。
> “生活就像一盒巧克力,你永远不知道你会得到什么。” ——《阿甘正传》
---
_测试结束。_
直接转换成 pdf
,显示效果可能不太好。
pandoc -o sample.pdf sample.md
在网上抄来的一套配置,首先在根目录创建 head.tex
:
\usepackage[top=2cm, bottom=1.5cm, left=2cm, right=2cm]{geometry}
% change background color for inline code in
% markdown files. The following code does not work well for
% long text as the text will exceed the page boundary
\definecolor{bgcolor}{HTML}{E0E0E0}
\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{
\colorbox{bgcolor}{\oldtexttt{#1}}
}
\XeTeXlinebreaklocale "zh"
\XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt
\usepackage[top=2cm, bottom=1.5cm, left=2cm, right=2cm]{geometry}
% Change the default style of block quote
\usepackage{framed}
\usepackage{quoting}
\definecolor{bgcolor}{HTML}{DADADA}
\colorlet{shadecolor}{bgcolor}
% define a new environment shadedquotation. You can change leftmargin and
% rightmargin as you wish.
\newenvironment{shadedquotation}
{\begin{shaded*}
\quoting[leftmargin=1em, rightmargin=0pt, vskip=0pt, font=itshape]
}
{\endquoting
\end{shaded*}
}
%
\def\quote{\shadedquotation}
\def\endquote{\endshadedquotation}
随后使用命令:
pandoc --pdf-engine=xelatex -V mainfont='Source Han Serif CN' --highlight-style tango -V colorlinks=true -o output.pdf sample.md -H head.tex
效果稍微好一点。