1. lstdefinestyle
参考 https://blog.csdn.net/ProgramChangesWorld/article/details/52142313
我们在使用listings的时候,需要设置语言和样式。用lstset会设置全局的变量,如果我们文章中有多种代码,那么就需要lstdefinestyle,设置多种样式,在使用的时候选择对应的样式。
% system=ubuntu %soft=Tex Live2015 % complie=XeLaTeX documentclass[a4paper,UTF8]{article} usepackage{listings} usepackage{ctex} usepackage{color} definecolor{keywordcolor}{rgb}{0.8,0.1,0.5} definecolor{webgreen}{rgb}{0,.5,0} definecolor{bgcolor}{rgb}{0.92,0.92,0.92} lstdefinestyle{styleJ}{ language=[AspectJ]Java, keywordstyle=color{keywordcolor}fseries, commentstyle=color{blue} extit, showstringspaces=false, numbers=left, numberstyle=small } lstdefinestyle{styleP}{ language=Python, numbers=right, frame=single, numberstyle=small , } egin{document} egin{lstlisting}[style=styleJ] public int sum(int n){ int sum = 0; for(int i=0;i<n;i++){ //开始的 sum += i; } return sum; } end{lstlisting} egin{lstlisting}[style=styleP] def fun(): print('你好,世界') #我是注释 end{lstlisting} end{document}
可以看到使用lstdefinestyle
定义了两个样式,styleJ和styleP,分别是java和python的样式,在使用lstlisting
环境的时候调设置了这两个样式。
如果不想把代码放在.tex文件里,也可以把代码放在单独的文件,然后使用下面的命令即可:
lstinputlisting[style=styleJ]{code.java}
2. listings样式1
参考 https://tex.stackexchange.com/questions/68091/how-do-i-add-syntax-coloring-to-my-c-source-code-in-beamer
documentclass{beamer} setbeamercovered{transparent} usepackage{listings} egin{document} % Using typewriter font: tfamily inside lstset egin{frame}[fragile] frametitle{Inserting source code} lstset{language=C++, basicstyle= tfamily, keywordstyle=color{blue} tfamily, stringstyle=color{red} tfamily, commentstyle=color{green} tfamily, morecomment=[l][color{magenta}]{#} } egin{lstlisting} #include<stdio.h> #include<iostream> // A comment int main(void) { printf("Hello World "); return 0; } end{lstlisting} end{frame} egin{frame}[fragile] frametitle{Inserting source code without setting typewriter} lstset{language=C++, keywordstyle=color{blue}, stringstyle=color{red}, commentstyle=color{green}, morecomment=[l][color{magenta}]{#} } egin{lstlisting} #include<stdio.h> #include<iostream> // A comment int main(void) { printf("Hello World "); return 0; } end{lstlisting} end{frame} end{document}
第一种使用了 tfamily,这个是一种打印机字体。
参考 https://www.tug.org/pracjourn/2006-1/schmidt/schmidt.pdf
https://tug.org/FontCatalogue/typewriterfonts.html
tfamilyselects a monospaced (“typewriter”) font family
3. listings样式2
参考 https://tex.stackexchange.com/questions/409705/c-code-change-the-font
documentclass{article} usepackage{xcolor} usepackage{listings} colorlet{mygray}{black!30} colorlet{mygreen}{green!60!blue} colorlet{mymauve}{red!60!blue} lstset{ backgroundcolor=color{gray!10}, basicstyle= tfamily, columns=fullflexible, breakatwhitespace=false, breaklines=true, captionpos=b, commentstyle=color{mygreen}, extendedchars=true, frame=single, keepspaces=true, keywordstyle=color{blue}, language=c++, numbers=none, numbersep=5pt, numberstyle= inycolor{blue}, rulecolor=color{mygray}, showspaces=false, showtabs=false, stepnumber=5, stringstyle=color{mymauve}, tabsize=3, title=lstname } egin{document} egin{lstlisting} #include <tesseract/baseapi.h> #include <leptonica/allheaders.h> int main() { char *outText; tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI(); // Initialize tesseract-ocr with English, without specifying tessdata path if (api->Init(NULL, "eng")) { fprintf(stderr, "Could not initialize tesseract. "); exit(1); } // Open input image with leptonica library Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif"); api->SetImage(image); // Get OCR result outText = api->GetUTF8Text(); printf("OCR output: %s", outText); // Destroy used object and release memory api->End(); delete [] outText; pixDestroy(&image); return 0; } end{lstlisting} end{document}