我已经写过好几篇博客说这个问题,总体上来讲,我觉得LaTeX的listing特别拉胯,显示的代码段高亮很丑,让人不忍直视。下面举个例子来说明:
例如要显示下面这段C代码:
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
我们有两种选择,一种是用listing,另一种是用minted,用listing的时候,我进行了如下的设置:
\usepackage{listings}
\usepackage{color}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}
\lstset{
backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
basicstyle=\footnotesize, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
captionpos=b, % sets the caption-position to bottom
commentstyle=\color{mygreen}, % comment style
deletekeywords={...}, % if you want to delete keywords from the given language
escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
firstnumber=1, % start line enumeration with line 1
frame=single, % adds a frame around the code
keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
keywordstyle=\color{blue}, % keyword style
language=C, % the language of the code
morekeywords={*,...}, % if you want to add more keywords to the set
numbers=left, % where to put the line-numbers; possible values are (none, left, right)
numbersep=5pt, % how far the line-numbers are from the code
numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
stepnumber=1, % the step between two line-numbers. If it's 1, each line will be numbered
stringstyle=\color{mymauve}, % string literal style
tabsize=2, % sets default tabsize to 2 spaces
title=\lstname % show the filename of files included with \lstinputlisting; also try caption instead of title
}
以上这些主要是抄的现有网页教程中的配置:https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
如果使用minted,就不需要这么复杂的配置了,只引入这个包即可:
\usepackage{minted}
然后在显示代码段的地方,这两种方法分别如下所示:
\begin{lstlisting}[basicstyle=\scriptsize\ttfamily\bfseries,caption={Example},language={C},frame=single]
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
\end{lstlisting}
\begin{listing}[h]
\begin{minted}[frame=single,linenos,fontsize=\scriptsize\bfseries,tabsize=2,xleftmargin=20pt]{c}
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
\end{minted}
\caption{Example}
\end{listing}
其实思路是大同小异的,给大家看看最终的结果:
第二种是minted的效果。可以看到,在我上面介绍的选项下,其实两种显示方法的字体什么的是完全一样的,但是明显第二种更好看一些。
另外提一下,如果用VS Code编辑LaTeX,引入minted包的时候会报错(其他的编辑器也一样):
Package minted Error: You must invoke LaTeX with the -shell-escape flag.
解决方法按照网友的介绍:vscode配置latex的代码高亮(minted)_lucyLee的博客-CSDN博客
在VS Code的界面下按F1,然后选择打开settings.json,给pdflatex和xelatex的配置都加上:"-shell-escape"选项,如下所示:
"latex-workshop.latex.tools": [{
"name": "latexmk",
"command": "latexmk",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
"-pdf",
"%DOC%"
]
}, {
"name": "xelatex",
"command": "xelatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-shell-escape",//新加入的选项
"-file-line-error",
"%DOC%"
]
}, {
"name": "pdflatex",
"command": "pdflatex",
"args": [
"-synctex=1",
"-interaction=nonstopmode",
"-shell-escape",//新加入的选项
"-file-line-error",
"%DOC%"
]
},
......
minted的其他使用方法可以参考:
latex中代码高亮显示宏包minted用法_xenonhu的博客-CSDN博客_minted
Code Highlighting with minted - Overleaf, Online LaTeX Editor
就简单总结这么多。