md2pdf

TotalClaw 作者 totalclaw

使用完整的 LaTeX 数学公式渲染和 CJK(中文/日文/韩文)支持将 Markdown 文件转换为 PDF。当用户要求将 Markdown 转换为 PDF、将报告渲染为 PDF、将注释导出为 PDF 或从 Markdown 生成可打印文档时使用。处理 $...$ 内联和 $$...$$ 显示数学、代码块、表格和混合 CJK/拉丁文本。需要 pandoc + texlive-xetex。

安装 / 下载方式

TotalClaw CLI推荐
totalclaw install totalclaw:totalclaw~huaruoji-md2pdf-xelatex
cURL直接下载,无需登录
curl -fsSL https://skills.taituai.com/api/skills/totalclaw%3Atotalclaw~huaruoji-md2pdf-xelatex/file -o huaruoji-md2pdf-xelatex.md
## 概述(中文)

使用完整的 LaTeX 数学公式渲染和 CJK(中文/日文/韩文)支持将 Markdown 文件转换为 PDF。当用户要求将 Markdown 转换为 PDF、将报告渲染为 PDF、将注释导出为 PDF 或从 Markdown 生成可打印文档时使用。处理 $...$ 内联和 $$...$$ 显示数学、代码块、表格和混合 CJK/拉丁文本。需要 pandoc + texlive-xetex。

## 原文

# md2pdf

Convert Markdown → PDF via Pandoc + XeLaTeX. Full LaTeX math + CJK support.

## Prerequisites

System packages (apt):

```
pandoc texlive-xetex texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra texlive-lang-chinese
```

## Quick Convert

```bash
bash <skill_dir>/scripts/md2pdf.sh input.md output.pdf
```

The script auto-detects CJK content, picks suitable fonts, sanitizes emoji, adds TOC, and configures XeLaTeX.

## Manual Pandoc Command

For fine-grained control, run pandoc directly:

```bash
pandoc input.md -o output.pdf \
  --pdf-engine=xelatex \
  -f markdown-smart \
  -H header.tex \
  -V mainfont="DejaVu Sans" \
  -V monofont="DejaVu Sans Mono" \
  -V geometry:margin=20mm \
  -V fontsize=10pt \
  -V colorlinks=true \
  --highlight-style=tango \
  --toc -V toc-title="Table of Contents"
```

Where `header.tex` contains:

```latex
\usepackage{xeCJK}
\setCJKmainfont{<CJK font name>}
```

## Key Details

- **Math**: Pandoc natively converts `$...$` (inline) and `$$...$$` (display) to LaTeX math. No MathJax/KaTeX needed.
- **CJK fonts**: Script auto-detects from: Noto Sans CJK SC > WenQuanYi Micro Hei > Droid Sans Fallback > AR PL UMing CN.
- **Emoji**: Replaced with text equivalents (`✅` → `[Y]`, `❌` → `[N]`, `⭐` → `*`) since most LaTeX fonts lack emoji glyphs.
- **Smart quotes**: Use `-f markdown-smart` to avoid curly quote rendering issues.
- **Long tables**: Pandoc may struggle with complex tables; keep tables simple or use `longtable` LaTeX package.

## Troubleshooting

| Problem | Fix |
|---------|-----|
| Missing character warnings | Check `fc-list :lang=zh` for available CJK fonts; install `fonts-noto-cjk` if needed |
| `xelatex not found` | Install `texlive-xetex` |
| PDF has no math rendering | Ensure markdown uses `$...$` / `$$...$$` (not HTML math tags) |
| Broken table layout | Simplify table or add `-V geometry:margin=15mm` for more width |