listings 是專用於代碼排版的 LaTeX宏包,可對關鍵詞、注釋和字串等使用不同的字型和顏色或顏色,也可以為代碼添加邊框、背景等風格。
1 基本用法
下面給出一份用於排版 C 語言 HelloWorld 程式碼的完整的 LaTeX 文檔:
\documentclass{ article}
\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
來源文件將為代碼添加陰影邊框,並將陰影設定為淺灰色:
\begin{ lstlisting}[language={[ANSI]C},keywordstyle=\color{blue!70},commentstyle=\color{red!50!green!50!blue!50},frame=shadowbox,
rulesepcolor=\color{red!20!green!20!blue!20}]
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
5 添加行號
很多時候需要對文檔中的代碼進行解釋,只有帶有行號的代碼才可以讓解釋更清晰,因為你只需要說第
x行代碼有什麼作用即可。如果沒有行號,那對讀者而言就太殘忍了,他們不得不從你的文字敘述中得知行號資訊,然後去一行一行的查到相應程式碼。
listings 宏包通過參數
numbers 來設定行號,該參數的值有兩個,分別是 left
與right,表示行號顯示在代碼的左側還是右側。下面為帶有邊框的代碼添加行號,並設定行號字型為 \tiny:
\begin{ lstlisting}[language={[ANSI]C},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}]
int main(int argc, char ** argv)
{
printf("Hello world! \n");
return 0;
}
\end{ lstlisting}
6 全域設定
上面所給的各個樣本中,lstlisting 環境後面尾隨了很多參數,要是每使用一次
lstlisting環境就要設定這麼多參數,那就沒什麼意思了。
可以使用 \lstset 命令在
LaTeX 來源文件的導言區設定好 lstlisting 環境所用的公用參數,如下:
\documentclass{ article}
\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
命令中設定逃逸字串的開始符號與終止符號,推薦使用的符號是左引號,即 “ `”
\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},
escapeinside=``}
……
\begin{ lstlisting}[language={[ANSI]C}]
int main(int argc, char ** argv)
{
printf("`我愛中文`! \n");
return 0;
}
\end{ lstlisting} 8 調整一下邊距
listings的代碼框的寬度預設是與頁芯等寬的,其上邊距也過於小,可根據自己的審美觀念適度調整一下。我通常是將代碼框的左右邊距設定為2em,上邊距為
1em,下邊距採用預設值即可,所作設定如下:
\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},escapeinside=``,xleftmargin=2em,xrightmargin=2em,
aboveskip=1em} [轉] http://blog.sina.com.cn/s/blog_50f21fed01016fud.html -------------------------------------------------------彙編代碼 設定\usepackage{xcolor}
\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%
} ...... \begin{lstlisting}[language={[x86masm]Assembler}]
DATA SEGMENT
BF DB 3 DUP(0) ;`暫存一次輸入資料`
TABTTL1 DB 'CODE SCORE', '$' ;`輸入圖式`
TABTTL2 DB 'CODE SCORE', 9, 'SEXY', 9, 15, '$' ;`輸出格式`
;.....\end{lstlisting} ---------------------------- 設定模板(from: wikipedia)
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 code10 basicstyle=\footnotesize, % the size of the fonts that are used for the code11 numbers=left, % where to put the line-numbers12 numberstyle=\tiny\color{gray}, % the style that is used for the line-numbers13 stepnumber=2, % the step between two line-numbers. If it's 1, each line 14 % will be numbered15 numbersep=5pt, % how far the line-numbers are from the code16 backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}17 showspaces=false, % show spaces adding particular underscores18 showstringspaces=false, % underline spaces within strings19 showtabs=false, % show tabs within strings adding particular underscores20 frame=single, % adds a frame around the code21 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 spaces23 captionpos=b, % sets the caption-position to bottom24 breaklines=true, % sets automatic line breaking25 breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace26 title=\lstname, % show the filename of files included with \lstinputlisting;27 % also try caption instead of title28 keywordstyle=\color{blue}, % keyword style29 commentstyle=\color{dkgreen}, % comment style30 stringstyle=\color{mauve}, % string literal style31 escapeinside={\%*}{*)}, % if you want to add LaTeX within your code32 morekeywords={*,...} % if you want to add more keywords to the set33 }