listings 是專用於代碼排版的 LaTeX宏包,可對關鍵詞、注釋和字符串等使用不同的字體和顏色或顏色,也可以為代碼添加邊框、背景等風格。
1 基本用法
下面給出一份用於排版 C 語言 HelloWorld 程序代碼的完整的 LaTeX 文檔:
\usepackage{ listings}
\begin{ document}
\begin{ lstlisting}[ language=C]
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
\end{ document}
注意,要使用 listings 宏包提供的語法高亮,需要 xcolor 宏包支持。
語法高亮的排版效果如下圖所示:

4 添加邊框
listings宏包為代碼邊框提供了很多風格,大體可分為帶有陰影的邊框與圓角邊框。這里僅僅給出一個陰影邊框的示例,至於其它邊框風格,可查閱listings 宏包文檔,里面給出了一些示例。
下面 LaTeX 源文檔將為代碼添加陰影邊框,並將陰影設置為淺灰色:
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
5 添加行號
很多時候需要對文檔中的代碼進行解釋,只有帶有行號的代碼才可以讓解釋更清晰,因為你只需要說第 x行代碼有什么作用即可。如果沒有行號,那對讀者而言就太殘忍了,他們不得不從你的文字敘述中得知行號信息,然后去一行一行的查到相應代碼行。
listings 宏包通過參數 numbers 來設定行號,該參數的值有兩個,分別是 left 與right,表示行號顯示在代碼的左側還是右側。下面為帶有邊框的代碼添加行號,並設置行號字體為 \tiny:
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
6 全局設置
上面所給的各個示例中,lstlisting 環境后面尾隨了很多參數,要是每使用一次 lstlisting環境就要設置這么多參數,那就沒什么意思了。
可以使用 \lstset 命令在 LaTeX 源文檔的導言區設定好 lstlisting 環境所用的公共參數,如下:
\usepackage{ listings}
\usepackage{ xcolor}
\begin{ document}
\lstset{numbers=left,
numberstyle= \tiny,
keywordstyle= \color{ blue!70},commentstyle=\color{red!50!green!50!blue!50},
frame=shadowbox,
rulesepcolor= \color{ red!20!green!20!blue!20}
}
\begin{ lstlisting}[language={[ANSI]C}]
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
\end{ document}
7 顯示中文
listings 宏包默認是不支持包含中文字串的代碼顯示的,但是可以使用 “逃逸” 字串來顯示中文。
在 \lstset 命令中設置逃逸字串的開始符號與終止符號,推薦使用的符號是左引號,即 “ `”
numberstyle= \tiny,keywordstyle= \color{ blue!70},commentstyle=\color{red!50!green!50!blue!50},
frame=shadowbox, rulesepcolor= \color{ red!20!green!20!blue!20},
escapeinside=``}
……
\begin{ lstlisting}[language={[ANSI]C}]
int main(int argc, char ** argv)
{
printf("`我愛中文`! \n");
return 0;
}
\end{ lstlisting}
8 調整一下邊距
listings的代碼框的寬度默認是與頁芯等寬的,其上邊距也過於小,可根據自己的審美觀念適度調整一下。我通常是將代碼框的左右邊距設置為2em,上邊距為 1em,下邊距采用默認值即可,所作設定如下:
\usepackage{listings}
\lstset{
basicstyle=\small,%
escapeinside=``,%
keywordstyle=\color{red} \bfseries,% \underbar,%
identifierstyle={},%
commentstyle=\color{blue},%
stringstyle=\ttfamily,%
%labelstyle=\tiny,%
extendedchars=false,%
linewidth=\textwidth,%
numbers=left,%
numberstyle=\tiny \color{blue},%
frame=trbl%
}
DATA SEGMENT
BF DB 3 DUP(0) ;`暫存一次輸入數據`
TABTTL1 DB 'CODE SCORE', '$' ;`輸入圖式`
TABTTL2 DB 'CODE SCORE', 9, 'SEXY', 9, 15, '$' ;`輸出格式`
;.....
1 \usepackage{listings} 2 \usepackage{color} 3 4 \definecolor{dkgreen}{rgb}{0,0.6,0} 5 \definecolor{gray}{rgb}{0.5,0.5,0.5} 6 \definecolor{mauve}{rgb}{0.58,0,0.82} 7 8 \lstset{ % 9 language=Octave, % the language of the code 10 basicstyle=\footnotesize, % the size of the fonts that are used for the code 11 numbers=left, % where to put the line-numbers 12 numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers 13 stepnumber=2, % the step between two line-numbers. If it's 1, each line 14 % will be numbered 15 numbersep=5pt, % how far the line-numbers are from the code 16 backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color} 17 showspaces=false, % show spaces adding particular underscores 18 showstringspaces=false, % underline spaces within strings 19 showtabs=false, % show tabs within strings adding particular underscores 20 frame=single, % adds a frame around the code 21 rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here)) 22 tabsize=2, % sets default tabsize to 2 spaces 23 captionpos=b, % sets the caption-position to bottom 24 breaklines=true, % sets automatic line breaking 25 breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace 26 title=\lstname, % show the filename of files included with \lstinputlisting; 27 % also try caption instead of title 28 keywordstyle=\color{blue}, % keyword style 29 commentstyle=\color{dkgreen}, % comment style 30 stringstyle=\color{mauve}, % string literal style 31 escapeinside={\%*}{*)}, % if you want to add LaTeX within your code 32 morekeywords={*,...} % if you want to add more keywords to the set 33 }