除了这个问题之外,我想知道是否可以为一种语言设置该设置,并为另一种语言设置默认设置? 如何在 C 列表中突出显示运算符和括号?
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\newcommand\opstyle{\color{red}} % <--- customise operator style here
\makeatletter
\lstset
{%
language=C++,
alsoletter=0123456789,% to prevent \opstyle from being applied to digits
}
% Hook into listings
\lst@AddToHook{OutputOther}{\ProcessOther@silmeth}
% helper macro
\newcommand\ProcessOther@silmeth
{%
\ifnum\lst@mode=\lst@Pmode% % If we're in `Processing' mode...
\def\lst@thestyle{\opstyle}% % ... redefine the style locally
\fi%
}
\makeatother
\begin{document}
\begin{lstlisting}[]
int i = 0;
if(i > 1) && (1/2 < 2) {
// one-line comment ()={}><;&
printf("string: ()={}><;&");
/*
block comment ()={}><&;
*/
}
\end{lstlisting}
这里,我想让其他组再次以黑色显示。我该怎么做?
\begin{lstlisting}[]
int i = 0;
if(i > 1) && (1/2 < 2) {
// one-line comment ()={}><;&
printf("string: ()={}><;&");
/*
block comment ()={}><&;
*/
}
\end{lstlisting}
\end{document}
答案1
通常的做法是定义一个style
,然后只在需要时使用该样式。但是,如果不设置,您可以使用
\renewcommand\opstyle{\color{blue}}
在列出之前根据需要进行更改:
代码:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\newcommand\opstyle{\color{red}} % <--- customise operator style here
\makeatletter
\lstset
{%
language=C++,
alsoletter=0123456789,% to prevent \opstyle from being applied to digits
}
% Hook into listings
\lst@AddToHook{OutputOther}{\ProcessOther@silmeth}
% helper macro
\newcommand\ProcessOther@silmeth
{%
\ifnum\lst@mode=\lst@Pmode% % If we're in `Processing' mode...
\def\lst@thestyle{\opstyle}% % ... redefine the style locally
\fi%
}
\makeatother
\begin{document}
\begin{lstlisting}[]
int i = 0;
if(i > 1) && (1/2 < 2) {
// one-line comment ()={}><;&
printf("string: ()={}><;&");
/*
block comment ()={}><&;
*/
}
\end{lstlisting}
\renewcommand\opstyle{\color{blue}}
I want the group other to appear in Black again.
\begin{lstlisting}[]
int i = 0;
if(i > 1) && (1/2 < 2) {
// one-line comment ()={}><;&
printf("string: ()={}><;&");
/*
block comment ()={}><&;
*/
}
\end{lstlisting}
\end{document}
\end{document}