考虑这个输入文件:
~~~{.html caption="test caption"}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Store</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
~~~
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
当我运行命令时:
pandoc ./DOC.md -o ./doc.pdf
列表有很好的样式:
我还想列出标题。因此
很多
的
人们建议使用--listings
包。
但当我跑完
pandoc ./DOC.md -o ./doc.pdf --listings
输出文件缺少代码块的样式!
如何配置 pandoc 以同时拥有良好的代码块和标题样式?
答案1
向代码块添加标题的一种方法是使用pandoc-crossref
:
~~~{#lst:captionAttr .html caption="test caption"}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Store</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
~~~
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
pandoc
作为pandoc-crossref
过滤器调用:
$ pandoc --filter pandoc-crossref doc.md -o doc.pdf
另一种方法是使用现有的配色方案listings
。有一个实现日光化主题你可以从这里开始:
从 github 获取包:https://github.com/jez/latex-solarized
通过添加 YAML 块来将
pandoc
包包含在 LaTeX 前言中:
---
header-includes: \usepackage{solarized-light}
---
导致
---
header-includes: \usepackage{solarized-light}
---
~~~{.html caption="test caption"}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Store</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
~~~
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
- 跑步
pandoc
:
$ pandoc --listings doc.md -o doc.pdf
答案2
我遇到了同样的问题,结果发现--listings
选项缺少一些样式。我从模板中复制了部分逻辑eisvogel.latex
,并在其上添加了一些自定义样式。
\usepackage{listings}
\newcommand{\passthrough}[1]{#1} % required from pandoc to provide inline
\usepackage{xcolor} % custom colors
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
%numbers=left, % I recommend disabling this, as one can use .numberLines in markdown
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
% if you want to use cleveref
\usepackage[nameinlink]{cleveref} % must come after hyperref package
\crefname{listing}{code}{codes}
\Crefname{listing}{Code}{Codes}
并且以下工作正常:
~~~{label=lst:captionAttr .html .numberLines caption="test caption"}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Store</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
~~~
Referencing [@Lst:captionAttr] using cleveref.
欢迎随意在 Legrand Orange Book 的 Pandoc 模板中测试:https://github.com/igormcoelho/pandoc-template-legrand-orange-book
pandoc --listings -s -F pandoc-crossref --citeproc --template=orangelegrand.latex book.md -o book.pdf