轉載自https://blog.csdn.net/golden1314521/article/details/39924449。
注意要用xelatex編譯。
LaTeX支持多種編程語言的輸入,而且允許你對其顯示效果(如背景框,各部分的顏色)等進行設置。
1.必須引入的包\usepackage{listings},爲了設置顏色,再引入顏色包\usepackage{xcolor}。
2.支持的語言:
- ABAP2,4 IDL4 PL/I
- ACSL inform Plasm
- Ada4 Java4 POV
- Algol4 JVMIS Prolog
- Ant ksh Promela
- Assembler2,4 Lisp4 Python
- Awk4 Logo R
- bash make4 Reduce
- Basic2,4 Mathematica1,4 Rexx
- C4 Matlab RSL
- C++4 Mercury Ruby
- Caml4 MetaPost S4
- Clean Miranda SAS
- Cobol4 Mizar Scilab
- Comal ML sh
- csh Modelica3 SHELXL
- Delphi Modula-2 Simula4
- Eiffel MuPAD SQL
- Elan NASTRAN tcl4
- erlang Oberon-2 TeX4
- Euphoria OCL4 VBScript
- Fortran4 Octave Verilog
- GCL Oz VHDL4
- Gnuplot Pascal4 VRML4
- Haskell Perl XML
- HTML PHP XSLT
3.使用方法
3.1第一種方法,直接對顯示風格進行設置,即在使用時設置
- \begin{colorboxed}
- \begin{lstlisting}[language={[ANSI]C},numbers=left,numberstyle=\tiny,%frame=shadowbox,
- rulesepcolor=\color{red!20!green!20!blue!20},
- keywordstyle=\color{blue!70!black},
- commentstyle=\color{blue!90!},
- basicstyle=\ttfamily]
- #include <iostream>
- #define LENGTH 8
- using namespace std;
- //測試用的代碼,bubbleSort函數
- int main() {
- int temp,number[LENGTH]={95,45,15,78,84,51,24,12};
- for(int i=0;i<LENGTH;i++)
- for(int j=0;j<LENGTH-1-i;j++)
- if(number[j]>number[j+1]) {
- temp=number[j];
- number[j]=number[j+1];
- number[j+1]=temp;
- } //if end
- for(int i=0;i<LENGTH;i++) cout<<number[i]<<" ";
- cout<<endl;
- }//main end
- \end{lstlisting}
- \end{colorboxed}
3.2.第二種方法,預先設置
- \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}
- 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=bl, % 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
- 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=Python, % 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{orange}, % string literal style
- tabsize=2, % sets default tabsize to 2 spaces
- %title=myPython.py % show the filename of files included with \lstinputlisting; also try caption instead of title
- }
- \begin{lstlisting}[language={[ANSI]C},title={bubbleSort.c}]
- #include <iostream>
- #define LENGTH 8
- using namespace std;
- //測試用的代碼,bubbleSort函數
- int main() {
- int temp,number[LENGTH]={95,45,15,78,84,51,24,12};
- for(int i=0;i<LENGTH;i++)
- for(int j=0;j<LENGTH-1-i;j++)
- if(number[j]>number[j+1]) {
- temp=number[j];
- number[j]=number[j+1];
- number[j+1]=temp;
- } //if end
- for(int i=0;i<LENGTH;i++) cout<<number[i]<<" ";
- cout<<endl;
- }//main end
- \end{lstlisting}
- \lstinputlisting[language=Python, title=myPython.py]{myPython.py}
上面的代碼有兩個引入源代碼的語句,前者引入C代碼,具體代碼在引入語句中;後者引入Python代碼,通過指定源文件名稱引入代碼。
basicstyle=\footnotesize, 這個選項設定源代碼的顯示字號,這裏羅列一下(從小到大)LaTeX支持的10類字號類型命令:
- \tiny
- \scriptsize
- \footnotesize
- \small
- \normalsize (default)
- \large
- \Large (capital "l")
- \LARGE (all caps)
- \huge
- \Huge (capital "h")
顯示效果:
3.3進一步修改
我們在設置中去掉邊框(註釋掉%frame=single, ),增加背景顏色(backgroundcolor=\color{lightgray},),改變源文件標題位置(captionpos=t, % t(top) or b(bottom)),效果如下:
3.加入中文或中文註釋
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}
- 在LaTeX中插入程序代碼 http://blog.sina.com.cn/s/blog_50f21fed01016fud.html