通常地,我们引用源代码的要求有:
- 字体样式与正文有明显区分,美观大方
- 可以标注代码行号
- 可以添加背景色,可以添加边框
- 可以对援引源代码进行语法高亮
那么listing宏包一定是你非常好的选择!
listing宏包支持代码上色的语言非常之多,目前所支持的语言有:
ABAP IDL Plasm ACSL inform POV Ada Java Prolog Algol JVMIS Promela Ant ksh Python Assembler Lisp R Awk Logo Reduce bash make Rexx Basic Mathematica RSL C Matlab Ruby C++ Mercury S Caml MetaPost SAS Clean Miranda Scilab Cobol Mizar sh Comal ML SHELXL csh Modula-2 Simula Delphi MuPAD SQL Eiffel NASTRAN tcl Elan Oberon-2 TeX erlang OCL VBScript Euphoria Octave Verilog Fortran Oz VHDL GCL Pascal VRML Gnuplot Perl XML Haskell PHP XSLT HTML PL/I当然,这个语言列表可能会随时间推移而逐渐庞大,如果你所援引的语言暂时不在上述之列,你仍可以自定义语法高亮的规则,不过,这便不再本文讨论之范畴。
以下给出的是使用了listing宏包的TeX代码示例,该代码可以使用LaTeX,pdfLaTeX,XeLaTeX等编译通过。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass[a4paper,winfonts]{ctexart} | |
\usepackage{listings} | |
\usepackage{xcolor} | |
\lstset{ | |
%行号 | |
numbers=left, | |
%背景框 | |
framexleftmargin=10mm, | |
frame=none, | |
%背景色 | |
%backgroundcolor=\color[rgb]{1,1,0.76}, | |
backgroundcolor=\color[RGB]{245,245,244}, | |
%样式 | |
keywordstyle=\bf\color{blue}, | |
identifierstyle=\bf, | |
numberstyle=\color[RGB]{0,192,192}, | |
commentstyle=\it\color[RGB]{0,96,96}, | |
stringstyle=\rmfamily\slshape\color[RGB]{128,0,0}, | |
%显示空格 | |
showstringspaces=false, | |
escapeinside=``, | |
extendedchars=true | |
} | |
\begin{document} | |
\section{C Language} | |
\begin{lstlisting}[language={C}] | |
int main(int argc, char ** argv) | |
{ | |
//Print Hello world | |
printf("Hello world!\n"); | |
int i; | |
for(int j=0;j<10;j++) | |
{ | |
printf("hello"); | |
} | |
return 0; | |
} | |
\end{lstlisting} | |
\section{C++ Language} | |
\begin{lstlisting}[language={C++}] | |
// custom countdown using while | |
#include <iostream.h> | |
int main () | |
{ | |
int n; | |
cout << "Enter the starting number > "; | |
cin >> n; | |
while (n>0) { | |
cout << n << ", "; | |
--n; | |
} | |
cout << "FIRE!"; | |
return 0; | |
} | |
\end{lstlisting} | |
\section{Verilog HDL} | |
\begin{lstlisting}[language={Verilog}] | |
module Q101detector(CLOCK, X, Y); | |
input CLOCK, X; | |
output Y; | |
reg Y; | |
reg [1:0] D,Q; | |
parameter [1:0] | |
INIT = 2'b00, | |
Q01 = 2'b01, | |
Q10 = 2'b11, | |
UNUSED = 2'b10; | |
always @ (posedge CLOCK) | |
Q <= D; | |
always @ (X, Q) | |
case (Q) | |
INIT: if (X) D = Q01; | |
else D = INIT; | |
Q01: if (X) D = Q01; | |
else D = Q10; | |
Q10: D = INIT; | |
UNUSED: D = INIT; | |
default D = INIT; | |
endcase | |
always @ (X,Q) | |
begin | |
if (Q==Q10 && X==1) Y = 1; | |
else Y = 0; | |
end | |
endmodule | |
\end{lstlisting} | |
\section{MATLAB Computing Language} | |
\begin{lstlisting}[language={Matlab}] | |
function [average,error]=MonteC(L) | |
if nargin==0,L=10000;end | |
for k=1:L | |
S(k)=viviani; | |
average=sum(S)/L; | |
end | |
plot(S) | |
figure(1),figure(2) | |
error=average-(8/3*pi-32/9); | |
hist(S,1000) | |
function V=viviani(n) | |
if nargin==0,n=10000;end | |
P=rand(n,3); | |
X=2*P(:,1);Y=2*P(:,2)-1;Z=2*P(:,3); | |
II=find((X-1).^2+Y.^2<=1&Z<=sqrt(4-X.^2-Y.^2)); | |
V=8*length(II)/n; | |
\end{lstlisting} | |
\section{Python Language} | |
\begin{lstlisting}[language={Python}] | |
class Fish: | |
def eat(self, food): | |
if food is not None: | |
self.hungry=False | |
#`构造`Fish`的实例:` | |
f=Fish() | |
#`以下两种调用形式是等价的:` | |
Fish.eat(f, "earthworm") | |
f.eat("earthworm") | |
\end{lstlisting} | |
\end{document} | |
\end{lstlisting} | |
\end{document} |
https://docs.google.com/leaf?id=0ByIYMq1zOBB4NzJlNmJmMWYtZmU2ZS00NGJlLTliOWUtODc0NjJhZGY5YWIw&hl=zh_CN