如何更改 lstlisting 环境中右括号的样式

如何更改 lstlisting 环境中右括号的样式

请考虑以下 MWE(摘自Antal Spector-Zabusky 答案):

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{color}

\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{miestilo}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    numberstyle=\noncopynumber,
    numberstyle=\tiny\color{codegray},
    keywordstyle=\color{magenta},
    stringstyle=\color{codepurple},
    basicstyle=\scriptsize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\newcommand{\CodeSymbol}[1]{\bfseries\textcolor{red}{#1}}   % Code associated to defining styles

\lstset {%
  columns=fullflexible,
  keepspaces=true,
  style=miestilo,
    language=C++,
  literate=%
        {\&}{{\CodeSymbol{\&}}}1
        {<}{{\CodeSymbol{$<$}}}1
        {(}{{\CodeSymbol{(}}}1
        {)}{{\CodeSymbol{)}}}1                              % This DOES NOT work good
}

\begin{document}

\begin{lstlisting}
Example: & works good, < and ( too...
(...)
\end{lstlisting}

\end{document}

右括号错误

如果可能的话,我想用同样的方式改变右括号的颜色。无论如何,用这种方法对我来说是不可能的。

我想我已经尝试了所有的可能性:

{\)}{{\CodeSymbol{\)}}}1            % Does not work
{)}{{\CodeSymbol{$)$}}}1            % Does not work
{)}{{\CodeSymbol{\)}}}1             % Does not work
{$)$}{{\CodeSymbol{$)$}}}1          % Does not work
% ...

但这些都不起作用。

我也看了手册,但什么也没找到。

如何解决?

谢谢你!!

编辑:它也不适用于/

编辑2:它也不起作用.

答案1

使用

\lstdefinestyle{miestilo}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    numberstyle=\noncopynumber,
    numberstyle=\tiny\color{codegray},
    keywordstyle=\color{magenta},
    stringstyle=\color{codepurple},
    basicstyle=\scriptsize,
    breakatwhitespace=false,         
%    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

在此处输入图片描述

这个选项有点可疑……

答案2

我不知道这是为什么,但这里有一个不太好的解决方案。只需在括号前添加另一个符号,~如下例所示,这似乎有效。

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage{color}

\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{miestilo}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    numberstyle=\noncopynumber,
    numberstyle=\tiny\color{codegray},
    keywordstyle=\color{magenta},
    stringstyle=\color{codepurple},
    basicstyle=\scriptsize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}

\newcommand{\CodeSymbol}[1]{\bfseries\textcolor{red}{#1}}   % Code associated to defining styles

\lstset {%
  columns=fullflexible,
  keepspaces=true,
  style=miestilo,
    language=C++,
  literate={\{}{{\CodeSymbol{\{}}}1
           {\}}{{\CodeSymbol{\}}}}1
           {(}{{\CodeSymbol{(}}}1
           {~)}{{\CodeSymbol{)}}}1
           {>}{{\CodeSymbol{$>$}}}1
           {=}{{\CodeSymbol{$=$}}}1
           {;}{{\CodeSymbol{$;$}}}1
}

\begin{document}

\begin{lstlisting}
Example: & works good, < and ( too...
(...~)
\end{lstlisting}

\end{document}

在此处输入图片描述

这里还有另一条评论:如果你从其他地方获取代码,披露消息来源总是更好的

相关内容