Mik*_*sen 56 pdf markdown pandoc
我一直在尝试使用Pandoc将一些Markdown转换为PDF文件.这是Pandoc不会为我转换的示例:
# Header!
## Sub Header
themselves derived respectively from the Greek ??????? i.e. 'anarchy'
Run Code Online (Sandbox Code Playgroud)
这只是我从维基百科数据库转储顶部抓取的东西.潘多克完全不喜欢这样.这是它给我的错误信息:
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 ?
Run Code Online (Sandbox Code Playgroud)
是否有一个命令开关我可以给它来解决这个问题?我尝试按照建议做这样的事情,但失败了:
iconv -t utf-8 test.md | pandoc -o test.pdf
Run Code Online (Sandbox Code Playgroud)
更新在遵循以下John的建议之前,请参阅此内容.
更新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}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ane 30
使用该--pdf-engine=xelatex选项.
Dmi*_*rov 11
更新:下面的答案似乎对 pandoc 1.x 有效,但在更高版本中,语法发生了变化
五年后回到这篇文章,问题仍然存在。命令
pandoc -s test.md -t latex -o test.pdf
Run Code Online (Sandbox Code Playgroud)
当test.md包含包含非拉丁字符、希腊文、西里尔文、CJK、希伯来文和阿拉伯文的文本时失败。
LaTeX 是在 Unicode 之前设计的,它对不同字符集的支持在某些方面很强大,但还不够全面,所以使用 XeLaTeX 的建议是有效的,但需要仔细选择主要字体,因为没有自动选择。
以下是可能出现的问题和一些解决方案的小分类。所有测试均使用 Pandoc 1.19。
通过 T2A 字体编码提供对 LaTeX 中西里尔字母的支持。
考虑一个小样本:
# Header
## Subheader
Tetris (Russian: ??????) quoting Wikipedia is a tile-matching puzzle
video game
Run Code Online (Sandbox Code Playgroud)
使用 pandoc 运行此示例将失败:
! Package inputenc Error: Unicode char ? (U+422)
(inputenc) not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Run Code Online (Sandbox Code Playgroud)
修复是可用的,因为fontenc选项是default.latex模板中的预定义变量。
运行这个例子
pandoc -t latex -o tetris.pdf -V fontenc=T2A cyrillic.md
Run Code Online (Sandbox Code Playgroud)
会产生正确的渲染
但是,这不会正确处理其他语言功能,例如连字符。更好的方法是使用 Babel 并让它选择正确的字体编码。
pandoc -t latex -o tetris.pdf -V lang -V babel-lang=russian cyrillic.md
Run Code Online (Sandbox Code Playgroud)
或者在 Markdown 中使用 Babel 命令切换语言
# Header
## Subheader
Tetris (Russian: \foreignlanguage{russian}{??????}) quoting Wikipedia
is a tile-matching puzzle video game
Run Code Online (Sandbox Code Playgroud)
并运行
pandoc -t latex -o tetris.pdf -V lang -V babel-lang=english \
-V babel-otherlangs=russian cyrillic2.md
Run Code Online (Sandbox Code Playgroud)
原始帖子中的示例包含来自主要和扩展希腊语 Unicode 代码页的字符。
无论如何,广泛使用的 LGR 希腊字体编码不包含在 LaTeX 3 项目中,而是归类为本地编码,即根据LaTeX 编码指南,它可能因站点和系统而异。
在 TeX Live 上需要安装以下软件包:texlive-greek-inputenc,texlive-greek-fontenc和texlive-cbfonts. 请注意,您需要 Babel 3.9 或更高版本。然而结果
pandoc -t latex -o anarchy.pdf -V fontenc=LGR greek.md
Run Code Online (Sandbox Code Playgroud)
可能会出现意外。
为了解决这个问题,必须正确设置 LaTeX Babel 包。并插入命令在原文中的语言之间切换:
# Header!
## Sub Header
themselves derived respectively from the Greek \textgreek{???????}
i.e. 'anarchy'
Run Code Online (Sandbox Code Playgroud)
使用以下命令编译它
pandoc -s greek2.md -t latex -V fontenc=T2A -V lang -V babel-lang=english \
-V babel-otherlangs=greek -o greek.pdf
Run Code Online (Sandbox Code Playgroud)
将完全按照您的预期产生输出:
如果我们使用 XeLaTeX,则不需要所有这些。
只需运行原始示例
pandoc -s greek.md --latex-engine=xelatex -t latex -o greek.pdf
Run Code Online (Sandbox Code Playgroud)
会产生
因为字体在希腊字符位置不包含任何内容,所以输出包含一些空白。
选择一种流行的字体作为新字体mainfont会有所帮助
pandoc -s greek.md --latex-engine=xelatex \
-V mainfont="Liberation Serif" -t latex -o greek.pdf
Run Code Online (Sandbox Code Playgroud)
但是,不会呈现来自扩展希腊代码页的字符,例如带有 psili 口音的小写字母 alpha。
该字体设置为希腊与的XeTEX / LuaTEX等程序指南建议使用dejavu的,风流才子或免费字体系列。
事实上,使用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
Run Code Online (Sandbox Code Playgroud)
默认情况下,Pandoc pdflatex在将markdown文件转换为pdf文件时使用引擎.pdflatex不能非常顺利地处理Unicode字符xelatex.你应该尝试一下xelatex.但是,仅仅使用xelatex命令是不够的.通常情况下,您需要选择一个适当的字体,其中包含您要排版的Unicode字符的字形.
我是中国用户,所以以中文为例.如果您有test.md包含以下内容:
你好汉字
您可以使用以下命令编译此markdown文件:
pandoc --pdf-engine=xelatex -V CJKmainfont="KaiTi" test.md -o test.pdf
Run Code Online (Sandbox Code Playgroud)
在上面的命令中,--pdf-engine=xelatex用于选择LaTeX引擎(对于新版本的Pandoc,--latex-engine不推荐使用该选项).-V CJKmainfont="KaiTi"用于选择支持中文的正确字体.对于其他语言,您可以使用该标志-C mainfont="<FONT_NAME>".
要查找支持您的语言的字体,您需要知道您的语言代码.然后,如果您使用的是Linux系统或安装了TeX Live的 Windows系统.您可以使用以下命令查找适合您语言的有效字体:
fc-list :lang=zh #find the font which support Chinese (language code is `zh`)
Run Code Online (Sandbox Code Playgroud)
如果选择使用,例如字体Source Han Serif CN,则使用以下命令编译markdown文件:
pandoc --pdf-engine=xelatex -V CJKmainfont="Source Han Serif CN" test.md -o test.pdf
Run Code Online (Sandbox Code Playgroud)
适用于西里尔字母字符
pandoc myfile.md --pdf-engine=xelatex -V mainfont=Arial
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17221 次 |
| 最近记录: |