我的大部分代码都是用 Python 编写的,通过以下简单的配置,渲染效果非常好lstlisting
在某个时候,我必须包含一个 XML 文件,因此我切换到,language=XML
但它使用默认字符集呈现起来很难看,所以我还添加了basicstyle=\ttfamily
。现在我不知道之后如何切换回默认样式。
\documentclass{article}
\usepackage{listings}
\lstset{tabsize=2, language=Python, breaklines=true,
breakatwhitespace=true, xleftmargin=.25in}
\begin{document}
%some python listings here
%XML listing
\lstset{language=XML, basicstyle=\ttfamily}
\begin{lstlisting}
<XML></XML>
\end{lstlisting}
%what do I put here?
%python listings again
\begin{lstlisting}
for i in python:
pass
\end{lstlisting}
\end{document}
答案1
\documentclass[preview,border=12pt]{standalone}% change it to your own document class
% begin
% this part is used to allow your readers to copy the code from a PDF viewer but without copying the line numbers.
\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
% end
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{shared}
{
numbers=left,
numbersep=1em,
numberstyle=\tiny\color{red}\noaccsupp,
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
breaklines=true,
tabsize=2,
columns=flexible,
}
\lstdefinestyle{xml}
{
style=shared,
language={XML},
%alsolanguage={PSTricks},
basicstyle=\small\tt,
keywordstyle=\color{blue},
commentstyle=\color[rgb]{0.13,0.54,0.13},
backgroundcolor=\color{yellow!10},
morekeywords={
graphicspath,
includegraphics,
blinddocument,
},
}
\lstdefinestyle{python}
{
style=shared,
language={Python},
%alsolanguage={[Sharp]C},
basicstyle=\small\tt,
keywordstyle=\color{blue},
commentstyle=\color[rgb]{0.13,0.54,0.13},
backgroundcolor=\color{cyan!10},
morekeywords={
Console,
WriteLine,
int,
},
}
\lstnewenvironment{xml}
{\lstset{style=xml}}
{}
\lstnewenvironment{python}
{\lstset{style=python}}
{}
\begin{document}
\begin{xml}
<XML></XML>
\end{xml}
\begin{python}
for i in python:
pass
\end{python}
\end{document}