Please consider this MWE:
\documentclass{article}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\lstset{
columns=flexible,
literate=
{Public Class}{{Public Class\color{red}}}{12}
{.Object}{{.\color{red}Object\color{black}}}{6}
}
\begin{document}
\begin{lstlisting}
Public Class Example
.Object = A
\end{lstlisting}
\end{document}
I would like to have Example
in red, but I am not able to do it.
I thought
{Public Class}{{Public Class\color{red}}}{12}
was going to fill any sequence of characters after Public Class
, like
{.Object}{{.\color{red}Object}}{6}
(Note that I removed \color{black}
and = A
was filled with red.)
Important Example
can be changed i.e. the red color should work for any sequence of characters of the same line of Public Class
. This effect is removed in the next line.
EDIT Oh, I realized that the blank space of {Public Class}
is the problem. I will search if this is a duplicate, though.
EDIT 2 I could not find a duplicate. I also edited the question because I forgot to add columns=flexible
, which makes the things a bit more difficult, because as @Robert pointed out doing {Public\ Class}
solves the problem, but adding columns=flexible
does not work anymore.
{Public\ Class}
? – Robert Mar 8 '20 at 1:28literate
feature works -- try eg.{.IIIIII}{{.\color{red}IIIIII\color{black}}}{6}
– Robert Mar 8 '20 at 1:59columns=flexible
which shrinks the characters width, so{Public\ Class}
does not work anymore. – manooooh Mar 8 '20 at 15:20flexible
, so the color setting doesn't get carried over. Maybe instead ofliterate
, you could use this:moredelim=**[il][\PublicClass]{Public\ Class}
with\newcommand\PublicClass{Public Class\color{red}}
– Robert Mar 8 '20 at 23:44