由于最近需要把markdown文件導(dǎo)出為pdf并且需要用到latex的行內(nèi)的數(shù)學(xué)公式,只用typora無法做到掷漱,于是開始使用pandoc。問題是pandoc中在從md轉(zhuǎn)pdf的時候需要手動設(shè)置中文榄檬,否則會有轉(zhuǎn)換問題卜范。這個坑查了很久,最終從一個pandoc模板里得到了解決方法鹿榜。
系統(tǒng):macOS 10.13.2
基本操作
準(zhǔn)備:需要先安裝HomeBrew
- 安裝pandoc
brew install pandoc
- 安裝texlive海雪,支持latex
brew cask install mactex
- 導(dǎo)出pandoc轉(zhuǎn)換為時的latex默認(rèn)模板
pandoc -D latex > template.latex
- 在template.latex里添加中文字體支持
\usepackage{fontspec} % 允許設(shè)定字體
\usepackage{xeCJK} % 分開設(shè)置中英文字型
\setCJKmainfont{SimSun} % 設(shè)定中文字型
\setmainfont{Helvetical} % 設(shè)定英文字型
\setromanfont{Helvetical} % 字型
\setmonofont{Courier New}
\linespread{1.2}\selectfont % 行距
- 使用模板把md轉(zhuǎn)換為pdf
pandoc --pdf-engine=xelatex --template=[template.latex的路徑] newfile.md -o newfile.pdf
pandoc的默認(rèn)使用的模板路徑
在mac os下,在~/.pandoc/templates文件夾里舱殿。沒有的話要手動創(chuàng)建的奥裸。
cd ~
mkdir -p ~/.pandoc/templates
導(dǎo)出latex的默認(rèn)模板至默認(rèn)的模板路徑
pandoc -D latex > ~/.pandoc/templates/default.latex
在default.latex里添加中文字體支持
\usepackage{fontspec} % 允許設(shè)定字體
\usepackage{xeCJK} % 分開設(shè)置中英文字型
\setCJKmainfont{SimSun} % 設(shè)定中文字型
\setmainfont{Vollkorn} % 設(shè)定英文字型
\setromanfont{Vollkorn} % 字型
\setmonofont{Courier New}
\linespread{1.2}\selectfont % 行距
這樣下來轉(zhuǎn)換的時候不使用指定的template,使用的就是default模板沪袭。例如
pandoc --pdf-engine=xelatex newfile.md -o newfile.pdf