360 Secure SkillHub
360 安全技能中心

md2pdf-xelatex

通过 bash 脚本调用 Pandoc 与 XeLaTeX,把 Markdown 文件转换为 PDF,支持行内与块级 LaTeX 数学公式、中日韩文字字体自动检测、emoji 替换、目录生成及代码块和表格处理;使用前需安装 pandoc 与 texlive-xetex 等系统包。

huaruoji
huaruoji
热度 67.49
最近更新 2026-05-17 12:27:28
分类:效率依赖:代码执行 / 读写文件应用场景:文件转换 / 报告撰写

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add md2pdf-xelatex
Metadata
name
md2pdf
description
Convert Markdown files to PDF with full LaTeX math formula rendering and CJK (Chinese/Japanese/Korean) support. Use when the user asks to convert markdown to PDF, render a report as PDF, export notes to PDF, or generate a printable document from markdown. Handles $...$ inline and $$...$$ display math, code blocks, tables, and mixed CJK/Latin text. Requires 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 <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:

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:

\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