\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}{,.,}
\definecolor{bgcolor}{rgb}{0.92,0.92,0.92}
\lstdefinestyle{styleJ}{
language=[AspectJ]Java,
keywordstyle=\color{keywordcolor}\bfseries,
commentstyle=\color{blue} \textit,
showstringspaces=false,
numbers=left,
numberstyle=\small
}
\lstdefinestyle{styleP}{
language=Python,
numbers=right,
frame=single,
numberstyle=\small ,
}
\begin{document}
\begin{lstlisting}[style=styleJ]
public int sum(int n){
int sum = ;
for(int i=;i<n;i++){ //开始的
sum += i;
}
return sum;
}
\end{lstlisting}
\begin{lstlisting}[style=styleP]
def fun():
print('你好,世界') #我是注释
\end{lstlisting}
\end{document}
可以看到使用lstdefinestyle
定义了两个样式,styleJ和styleP,分别是java和python的样式,在使用lstlisting
环境的时候调设置了这两个样式。
如果不想把代码放在.tex文件里,也可以把代码放在单独的文件,然后使用下面的命令即可:
\lstinputlisting[style=styleJ]{code.java}
\documentclass{beamer}
\setbeamercovered{transparent}
\usepackage{listings}
\begin{document}
% Using typewriter font: \ttfamily inside \lstset
\begin{frame}[fragile]
\frametitle{Inserting source code}
\lstset{language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
morecomment=[l][\color{magenta}]{\#}
}
\begin{lstlisting}
#include
#include
// A comment
int main(void)
{
printf("Hello World\n");
return ;
}
\end{lstlisting}
\end{frame}
\begin{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}]{\#}
}
\begin{lstlisting}
#include
#include
// A comment
int main(void)
{
printf("Hello World\n");
return ;
}
\end{lstlisting}
\end{frame}
\end{document}
第一种使用了\ttfamily,这个是一种打印机字体。
参考 https://www.tug.org/pracjourn/2006-1/schmidt/schmidt.pdf
https://tug.org/FontCatalogue/typewriterfonts.html
\ttfamilyselects a monospaced (“typewriter”) font family
参考 https://tex.stackexchange.com/questions/409705/c-code-change-the-font
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\colorlet{mygray}{black!}
\colorlet{mygreen}{green!!blue}
\colorlet{mymauve}{red!!blue}
\lstset{
backgroundcolor=\color{gray!},
basicstyle=\ttfamily,
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=\tiny\color{blue},
rulecolor=\color{mygray},
showspaces=false,
showtabs=false,
stepnumber=,
stringstyle=\color{mymauve},
tabsize=,
title=\lstname
}
\begin{document}
\begin{lstlisting}
#include
#include
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.\\n");
exit();
}
// 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:\\n%s", outText);
// Destroy used object and release memory
api->End();
delete \[\] outText;
pixDestroy(&image);
return ;
}
\end{lstlisting}
\end{document}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章