LaTeX插入源代码

时间:2021-01-29 06:13:19


注意要用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}


显示效果:

LaTeX插入源代码

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")

显示效果:

LaTeX插入源代码

3.3进一步修改

我们在设置中去掉边框(注释掉%frame=single, ),增加背景颜色(backgroundcolor=\color{lightgray},),改变源文件标题位置(captionpos=t, % t(top) or b(bottom)),效果如下:

LaTeX插入源代码

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}