# How to change style for right parenthesis inside lstlisting environment

\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},
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}


If possible, I would like to change the color of the right parenthesis in the same way. Anyway with this method it is not possible for me.

I think I tried all the possibilites:

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


but none of these works.

Also I looked at the manual but I can not find anything.

How can it be solved?

Thank you!!

EDIT: Also it does not work with /.

EDIT 2: Also it does not work with ..

• I guess you got your code from here. In this answer it seems to work. There is also an alternative answer, did you try that one? Otherwise you may want to replace the closing bracket by something that gets translated to the bracket... – user121799 Jul 21 '18 at 7:31
• Yep, I got my code from there. I didn't try that solution because it includes if statements and so on and I don't like it very much... :P. Let me try it. – manooooh Jul 21 '18 at 7:34
• YAAAAAAAY in a indirect way you solved it marmot!! Did you know about this package? Thank you so much! – manooooh Jul 21 '18 at 7:41
• No, I am a marmot. ;-) – user121799 Jul 21 '18 at 7:46
• Shhh, you are a smart marmot. Take a look here. – manooooh Jul 21 '18 at 7:47

Use

\lstdefinestyle{miestilo}{
backgroundcolor=\color{backcolour},
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
}


There is something fishy with that option ...

• It is so strange... – manooooh Jul 21 '18 at 7:51

I have no idea why this is but here is an ugly workaround. Just prepend another symbol to the brackets, ~ in the example below, and this seems to work.

\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},
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}


And here is another comment: if you get your code from somewhere else, it is always better to disclose the source.