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}  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章