我一直在尝试使用Pandoc将一些Markdown转换为PDF文件。这是Pandoc不会为我转换的示例:
# Header!
## Sub Header
themselves derived respectively from the Greek ἀναρχία i.e. 'anarchy'
这只是我从维基百科数据库转储顶部抓取的东西。潘多克完全不喜欢这样。这是它给我的错误信息:
pandoc: Error producing PDF from TeX source.
! Package inputenc Error: Unicode char \u8:ἀ not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.53 ...es derived respectively from the Greek ἀ
是否有一个命令开关我可以给它来解决这个问题?我尝试按照建议做这样的事情,但失败了:
iconv -t utf-8 test.md | pandoc -o test.pdf
更新在遵循John的建议之前,see this。
更新2这是最终使其正常工作的命令。希望这会帮助某人:
pandoc test2.md -o test2.pdf --latex-engine=xelatex --template=my.latex --variable mainfont="DejaVu Serif" --variable sansfont=Arial
这是my.latex
的内容:
\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$lang$,$endif$$if(papersize)$$papersize$,$endif$]{$documentclass$}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
% use microtype if available
\IfFileExists{microtype.sty}{\usepackage{microtype}}{}
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[utf]{inputenc}
\usepackage{ucs}
$if(euro)$
\usepackage{eurosym}
$endif$
\else % if luatex or xelatex
\usepackage{fontspec}
\ifxetex
\usepackage{xltxtra,xunicode}
\fi
\defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
\setromanfont{TeX Gyre Pagella}
\newcommand{\euro}{€}
$if(mainfont)$
\setmainfont{$mainfont$}
$endif$
$if(sansfont)$
\setsansfont{$sansfont$}
$endif$
$if(monofont)$
\setmonofont{$monofont$}
$endif$
$if(mathfont)$
\setmathfont{$mathfont$}
$endif$
\fi
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{plainnat}
$endif$
$if(biblatex)$
\usepackage{biblatex}
$if(biblio-files)$
\bibliography{$biblio-files$}
$endif$
$endif$
$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
$endif$
$if(tables)$
\usepackage{longtable}
$endif$
$if(graphics)$
\usepackage{graphicx}
% We will generate all images so they have a width \maxwidth. This means
% that they will get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=\maxwidth]{#1}}
$endif$
\ifxetex
\usepackage[setpagesize=false, % page size defined by xetex
unicode=false, % unicode breaks when used with xetex
xetex]{hyperref}
\else
\usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true,
bookmarks=true,
pdfauthor={$author-meta$},
pdftitle={$title-meta$},
colorlinks=true,
urlcolor=$if(urlcolor)$$urlcolor$$else$blue$endif$,
linkcolor=$if(linkcolor)$$linkcolor$$else$magenta$endif$,
pdfborder={0 0 0}}
\urlstyle{same} % don't use monospace font for urls
$if(links-as-notes)$
% Make links footnotes instead of hotlinks:
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
$if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
$endif$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em} % prevent overfull lines
$if(numbersections)$
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(verbatim-in-note)$
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$
$if(lang)$
\ifxetex
\usepackage{polyglossia}
\setmainlanguage{$mainlang$}
\else
\usepackage[$lang$]{babel}
\fi
$endif$
$for(header-includes)$
$header-includes$
$endfor$
$if(title)$
\title{$title$}
$endif$
\author{$for(author)$$author$$sep$ \and $endfor$}
\date{$date$}
\begin{document}
$if(title)$
\maketitle
$endif$
$for(include-before)$
$include-before$
$endfor$
$if(toc)$
{
\hypersetup{linkcolor=black}
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
}
$endif$
$body$
$if(natbib)$
$if(biblio-files)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$biblio-files$}
$endif$
$endif$
$if(biblatex)$
\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$
$endif$
$for(include-after)$
$include-after$
$endfor$
\end{document}
使用--pdf-engine=xelatex
选项。
默认情况下,Pandoc在将markdown文件转换为pdf文件时使用pdflatex
引擎。 pdflatex
无法像xelatex
那样顺利处理Unicode字符。你应该尝试xelatex
。但是,仅使用xelatex
命令是不够的。通常情况下,您需要选择一个适当的字体,其中包含您要排版的Unicode字符的字形。
我是中国用户,所以以中文为例。如果你有一个包含以下内容的test.md
:
你好汉字
您可以使用以下命令编译此markdown文件:
pandoc --pdf-engine=xelatex -V CJKmainfont="KaiTi" test.md -o test.pdf
在上面的命令中,--pdf-engine=xelatex
用于选择LaTeX引擎(用于新版本的Pandoc,--latex-engine
option is deprecated)。 -V CJKmainfont="KaiTi"
用于选择支持中文的正确字体。对于其他语言,您可以使用标志-C mainfont="<FONT_NAME>"
。
为了找到支持您的语言的字体,您需要知道您的language code。然后,如果您使用的是Linux系统或安装了TeX Live的Windows系统。您可以使用以下命令查找适合您语言的有效字体:
fc-list :lang=zh #find the font which support Chinese (language code is `zh`)
如果您选择使用,例如字体Source Han Serif CN
,然后使用以下命令编译markdown文件:
pandoc --pdf-engine=xelatex -V CJKmainfont="Source Han Serif CN" test.md -o test.pdf
五年后回到这个职位,问题仍然存在。命令
pandoc -s test.md -t latex -o test.pdf
当test.md
包含非拉丁字符,包括希腊语,西里尔语,捷克语,希伯来语和阿拉伯语的文本时失败。
LaTeX是在Unicode之前设计的,它对不同字符集的支持在某些方面很强大,但远非全面,因此使用XeLaTeX的建议是有效的,但需要人们仔细选择主字体,因为没有自动选择。
以下是可能的问题和一些解决方案的小分类。全部用Pandoc 1.19测试。
通过T2A字体编码提供对LaTeX中的西里尔字母的支持。
考虑一个小样本:
# Header
## Subheader
Tetris (Russian: Тетрис) quoting Wikipedia is a tile-matching puzzle
video game
使用pandoc运行此示例将失败:
! Package inputenc Error: Unicode char Т (U+422)
(inputenc) not set up for use with LaTeX.
See the inputenc package documentation for explanation.
由于fontenc
选项是default.latex
模板中的预定义变量,因此可以使用修复程序。
运行此示例
pandoc -t latex -o tetris.pdf -V fontenc=T2A cyrillic.md
会产生正确的渲染
然而,这不能正确处理其他语言功能,例如连字符。更好的方法是使用Babel并让它选择正确的字体编码。
pandoc -t latex -o tetris.pdf -V lang -V babel-lang=russian cyrillic.md
或者使用Markdown中的Babel命令切换语言
# Header
## Subheader
Tetris (Russian: \foreignlanguage{russian}{Тетрис}) quoting Wikipedia
is a tile-matching puzzle video game
并运行
pandoc -t latex -o tetris.pdf -V lang -V babel-lang=english \
-V babel-otherlangs=russian cyrillic2.md
原始帖子中的示例包含来自主要和扩展希腊语Unicode代码页的字符。
无论如何,广泛使用的LGR希腊字体编码不包含在LaTeX 3项目中,并且被归类为本地编码,即根据LaTeX Encoding Guide,它可能因站点而异,也可能因系统而异。
在TeX Live上,需要安装以下软件包:texlive-greek-inputenc
,texlive-greek-fontenc
和texlive-cbfonts
。请注意,您需要使用Babel 3.9或更高版本。然而结果
pandoc -t latex -o anarchy.pdf -V fontenc=LGR greek.md
可能会出乎意料。
为了解决这个问题,必须正确设置LaTeX Babel包。并插入命令以在原始文本中切换语言:
# Header!
## Sub Header
themselves derived respectively from the Greek \textgreek{ἀναρχία}
i.e. 'anarchy'
使用以下命令编译它
pandoc -s greek2.md -t latex -V fontenc=T2A -V lang -V babel-lang=english \
-V babel-otherlangs=greek -o greek.pdf
将产生与您期望的完全相同的输出:
如果我们使用XeLaTeX,则不需要所有这些。
只需运行原始示例
pandoc -s greek.md --latex-engine=xelatex -t latex -o greek.pdf
会产生
因为字体在希腊字符位置中不包含任何内容,所以输出包含一些空格。
选择一种流行的字体作为新的mainfont
会有所帮助
pandoc -s greek.md --latex-engine=xelatex \
-V mainfont="Liberation Serif" -t latex -o greek.pdf
但是,扩展的希腊语代码页中的字符(例如带有psili重音的小写字母alpha)不会呈现。
Font Setup for Greek with XeTeX/LuaTeX指南建议使用DejaVu,Libertine或Free字体系列。
确实有DejaVu Serif
,Linux Libertine O
以及Tempora
和其他一些字体,结果将如预期的那样。请参阅下面使用XeLaTeX和Linux Libertine字体进行渲染。
pandoc -s greek.md --latex-engine=xelatex -V mainfont="Linux Libertine O" \
-t latex -o greek.pdf
如果您正在使用LaTeX中间输出,那么您可以使用内联\mbox{t\'ext}
来获取重音字符。如果没有\mbox{}
,Pandoc解析器通常无法正确解释反斜杠。
您可以使用--latex-engine=xelatex
,如前所述,但我发现最好的是使用lang
变量来指定标题中的文档语言,如下所示:lang: ru-RU
。我的debian工作站上的一个工作示例:
---
title: Lady Macbeth de Mzensk (Chostakovitch, livret d'Alexandre Preis, 1934)
lang: ru-RU
---
# Acte I / Tableau 1
*[Народ ненадежный]*
Ха, ха, ха, ха, ха, ха, ха. *[...]* Чуыствуем
На кого ты нас покидаешь?
Без хозяина будет скучно,
скучно, тоскливо, безрадостно.
Не работа. Без тебя невеселье. Воз вращайся
Как можно скорей, скорей !
然后你可以启动:
$ pandoc -o your-file-output.pdf your-source-file.md
我有一个类似的问题试图让数学符号显示在输出中。
正如其他人所提到的,最近的pandoc版本(在我的情况下为v2.2.3.2),使用的选项是pdf-engine=xelatex
。在这种情况下我不需要指定字体:
pandoc -o MyDoc.pdf --pdf-engine=xelatex MyDoc.md
我确实得到了latinmodern-math字体缺失的错误。我用它安装了:
tlmgr install collection-fontsrecommended
适用于西里尔字符
pandoc myfile.md --pdf-engine=xelatex -V mainfont=Arial