轉自 http://blog.csdn.net/lydyangliu/article/details/9208635
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\lstset{%
alsolanguage=Java,
%language={[ISO]C++}, %language為,還有{[Visual]C++}
%alsolanguage=[ANSI]C, %可以添加很多個alsolanguage,如alsolanguage=matlab,alsolanguage=VHDL等
%alsolanguage= tcl,
alsolanguage= XML,
tabsize=4, %
frame=shadowbox, %把代碼用帶有陰影的框圈起來
commentstyle=\color{red!50!green!50!blue!50},%淺灰色的注釋
rulesepcolor=\color{red!20!green!20!blue!20},%代碼塊邊框為淡青色
keywordstyle=\color{blue!90}\bfseries, %代碼關鍵字的顏色為藍色,粗體
showstringspaces=false,%不顯示代碼字符串中間的空格標記
stringstyle=\ttfamily, % 代碼字符串的特殊格式
keepspaces=true, %
breakindent=22pt, %
numbers=left,%左側顯示行號 往左靠,還可以為right,或none,即不加行號
stepnumber=1,%若設置為2,則顯示行號為1,3,5,即stepnumber為公差,默認stepnumber=1
%numberstyle=\tiny, %行號字體用小號
numberstyle={\color[RGB]{0,192,192}\tiny} ,%設置行號的大小,大小有tiny,scriptsize,footnotesize,small,normalsize,large等
numbersep=8pt, %設置行號與代碼的距離,默認是5pt
basicstyle=\footnotesize, % 這句設置代碼的大小
showspaces=false, %
flexiblecolumns=true, %
breaklines=true, %對過長的代碼自動換行
breakautoindent=true,%
breakindent=4em, %
escapebegin=\begin{CJK*}{GBK}{hei},escapeend=\end{CJK*},
aboveskip=1em, %代碼塊邊框
tabsize=2,
showstringspaces=false, %不顯示字符串中的空格
backgroundcolor=\color[RGB]{245,245,244}, %代碼背景色
%backgroundcolor=\color[rgb]{0.91,0.91,0.91} %添加背景色
escapeinside=``, %在``里顯示中文
%% added by http://bbs.ctex.org/viewthread.php?tid=53451
fontadjust,
captionpos=t,
framextopmargin=2pt,framexbottommargin=2pt,abovecaptionskip=-3pt,belowcaptionskip=3pt,
xleftmargin=4em,xrightmargin=4em, % 設定listing左右的空白
texcl=true,
% 設定中文沖突,斷行,列模式,數學環境輸入,listing數字的樣式
extendedchars=false,columns=flexible,mathescape=true
% numbersep=-1em
}
\begin{document}