
Además de esa pregunta, me pregunto si es posible tener esa configuración para un idioma y la configuración predeterminada para otro. ¿Cómo resaltar operadores y corchetes en un listado 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}
Aquí quiero que el otro grupo aparezca nuevamente en negro. ¿Cómo puedo hacer esto?
\begin{lstlisting}[]
int i = 0;
if(i > 1) && (1/2 < 2) {
// one-line comment ()={}><;&
printf("string: ()={}><;&");
/*
block comment ()={}><&;
*/
}
\end{lstlisting}
\end{document}
Respuesta1
La forma habitual sería definir a style
y utilizar ese estilo sólo cuando se desee. Sin embargo, sin que esto esté configurado, puedes usar
\renewcommand\opstyle{\color{blue}}
antes del listado para cambiarlo como desee:
Código:
\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}