
В дополнение к этому вопросу мне интересно, возможно ли иметь эти настройки для одного языка и настройки по умолчанию для другого? Как выделить операторы и скобки в листинге на языке 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}
Здесь я хочу, чтобы группа other снова появилась в черном. Как мне это сделать?
\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}