用LaTeX写论文时,插入C++源代码有专门的宏包,方法如下:
1 \usepackage{listings}
2
3 \lstset{language=C++}%这条命令可以让LaTeX排版时将C++键字突出显示
4
5 \lstset{breaklines}%这条命令可以让LaTeX自动将长的代码行换行排版
6
7 \lstset{extendedchars=false}%这一条命令可以解决代码跨页时,章节标题,页眉等汉字不显示的问题
8
9 \begin{lstlisting}
10
11 %paste your C++ code here
12
13 \end{lstlisting}
下面是我常用的设置:
1 \lstset{ %Settings for listings package.
2 language=[ANSI]{C},
3 backgroundcolor=\color{lightgray},
4 basicstyle=\footnotesize,
5 breakatwhitespace=false,
6 breaklines=true,
7 captionpos=b,
8 commentstyle=\color{olive},
9 directivestyle=\color{blue},
10 extendedchars=false,
11 % frame=single,%shadowbox
12 framerule=0pt,
13 keywordstyle=\color{blue}\bfseries,
14 morekeywords={*,define,*,include...},
15 numbersep=5pt,
16 rulesepcolor=\color{red!20!green!20!blue!20},
17 showspaces=false,
18 showstringspaces=false,
19 showtabs=false,
20 stepnumber=2,
21 stringstyle=\color{purple},
22 tabsize=4,
23 title=\lstname
24 }