Latex添加代码块

转载自https://blog.csdn.net/golden1314521/article/details/39924449。


注意要用xelatex编译。

LaTeX支持多种编程语言的输入,而且允许你对其显示效果(如背景框,各部分的颜色)等进行设置。

1.必须引入的包\usepackage{listings},为了设置颜色,再引入颜色包\usepackage{xcolor}。

2.支持的语言:

[plain]  view plain  copy
  1. ABAP2,4 IDL4    PL/I  
  2. ACSL    inform  Plasm  
  3. Ada4    Java4   POV  
  4. Algol4  JVMIS   Prolog  
  5. Ant ksh Promela  
  6. Assembler2,4    Lisp4   Python  
  7. Awk4    Logo    R  
  8. bash    make4   Reduce  
  9. Basic2,4    Mathematica1,4  Rexx  
  10. C4  Matlab  RSL  
  11. C++4    Mercury Ruby  
  12. Caml4   MetaPost    S4  
  13. Clean   Miranda SAS  
  14. Cobol4  Mizar   Scilab  
  15. Comal   ML  sh  
  16. csh Modelica3   SHELXL  
  17. Delphi  Modula-2    Simula4  
  18. Eiffel  MuPAD   SQL  
  19. Elan    NASTRAN tcl4  
  20. erlang  Oberon-2    TeX4  
  21. Euphoria    OCL4    VBScript  
  22. Fortran4    Octave  Verilog  
  23. GCL Oz  VHDL4  
  24. Gnuplot Pascal4 VRML4  
  25. Haskell Perl    XML  
  26. HTML    PHP XSLT  

3.使用方法

3.1第一种方法,直接对显示风格进行设置,即在使用时设置

[plain]  view plain  copy
  1. \begin{colorboxed}  
  2. \begin{lstlisting}[language={[ANSI]C},numbers=left,numberstyle=\tiny,%frame=shadowbox,  
  3.    rulesepcolor=\color{red!20!green!20!blue!20},  
  4.    keywordstyle=\color{blue!70!black},  
  5.    commentstyle=\color{blue!90!},  
  6.    basicstyle=\ttfamily]  
  7. #include <iostream>  
  8. #define LENGTH 8  
  9. using namespace std;  
  10. //测试用的代码,bubbleSort函数  
  11. int main() {  
  12.     int temp,number[LENGTH]={95,45,15,78,84,51,24,12};  
  13.     for(int i=0;i<LENGTH;i++)  
  14.         for(int j=0;j<LENGTH-1-i;j++)  
  15.             if(number[j]>number[j+1]) {  
  16.                 temp=number[j];  
  17.                 number[j]=number[j+1];  
  18.                 number[j+1]=temp;  
  19.             } //if end  
  20.     for(int i=0;i<LENGTH;i++) cout<<number[i]<<" ";  
  21.     cout<<endl;  
  22. }//main end  
  23. \end{lstlisting}  
  24. \end{colorboxed}  


显示效果:



3.2.第二种方法,预先设置

[plain]  view plain  copy
  1. \definecolor{mygreen}{rgb}{0,0.6,0}  
  2. \definecolor{mygray}{rgb}{0.5,0.5,0.5}  
  3. \definecolor{mymauve}{rgb}{0.58,0,0.82}  
  4.   
  5. \lstset{ %  
  6.   backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}  
  7.   basicstyle=\footnotesize,        % the size of the fonts that are used for the code  
  8.   breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace  
  9.   breaklines=true,                 % sets automatic line breaking  
  10.   captionpos=bl,                    % sets the caption-position to bottom  
  11.   commentstyle=\color{mygreen},    % comment style  
  12.   deletekeywords={...},            % if you want to delete keywords from the given language  
  13.   escapeinside={\%*}{*)},          % if you want to add LaTeX within your code  
  14.   extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8  
  15.   frame=single,                    % adds a frame around the code  
  16.   keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)  
  17.   keywordstyle=\color{blue},       % keyword style  
  18.   %language=Python,                 % the language of the code  
  19.   morekeywords={*,...},            % if you want to add more keywords to the set  
  20.   numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)  
  21.   numbersep=5pt,                   % how far the line-numbers are from the code  
  22.   numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers  
  23.   rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))  
  24.   showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'  
  25.   showstringspaces=false,          % underline spaces within strings only  
  26.   showtabs=false,                  % show tabs within strings adding particular underscores  
  27.   stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered  
  28.   stringstyle=\color{orange},     % string literal style  
  29.   tabsize=2,                       % sets default tabsize to 2 spaces  
  30.   %title=myPython.py                   % show the filename of files included with \lstinputlisting; also try caption instead of title  
  31. }  
  32. \begin{lstlisting}[language={[ANSI]C},title={bubbleSort.c}]  
  33. #include <iostream>  
  34. #define LENGTH 8  
  35. using namespace std;  
  36. //测试用的代码,bubbleSort函数  
  37. int main() {  
  38.     int temp,number[LENGTH]={95,45,15,78,84,51,24,12};  
  39.     for(int i=0;i<LENGTH;i++)  
  40.         for(int j=0;j<LENGTH-1-i;j++)  
  41.             if(number[j]>number[j+1]) {  
  42.                 temp=number[j];  
  43.                 number[j]=number[j+1];  
  44.                 number[j+1]=temp;  
  45.             } //if end  
  46.     for(int i=0;i<LENGTH;i++) cout<<number[i]<<" ";  
  47.     cout<<endl;  
  48. }//main end  
  49. \end{lstlisting}  
  50. \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 命令中设置逃逸字串的开始符号与终止符号,推荐使用的符号是左引号,即 “ `”

[plain]  view plain  copy
  1. \lstset{numbers=left,   
  2. numberstyle= \tiny,keywordstyle= \color{ blue!70},commentstyle=\color{red!50!green!50!blue!50},   
  3. frame=shadowbox, rulesepcolor= \color{ red!20!green!20!blue!20},   
  4. escapeinside=``}   
  5. \begin{lstlisting}[language={[ANSI]C}]  
  6. int main(int argc, char ** argv)  
  7. {  
  8. //`中文`  
  9. printf("`我爱中文`! \n");  
  10. return 0;  
  11. }  
  12. \end{lstlisting}  



  • 7
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值