
その質問に加えて、ある言語に対してその設定を使用し、別の言語に対してデフォルト設定を使用することが可能かどうか疑問に思います。 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}