20

I have some pseudo-code where I want to highlight some sections in a different color (this is why: in an assignment at university, I have some code given and have to adapt it. I want to highlight what I changed for enhanced readability).

\documentclass[english, a4paper, 11pt]{article}
\usepackage{xcolor}
\usepackage{listings}
lstset(language=C)  //best for pseudo-code IMHO

\begin{document}
\begin{lstlisting}[frame=single]
   public class A<T> extends B<T> {
      \textcolor{red} {Stack stack};  //how can I achive this? Nothing should change, except for the textcolor. This code just prints the \textcolor command though.

       //ctor
       public A() {};
   }
\end{lstlisting}
\end{document}
26

You should provide a MWE. However, I have the following answer:

\documentclass{article}
\usepackage{listings}

\usepackage{xcolor}

\lstdefinestyle{base}{
  language=C,
  emptylines=1,
  breaklines=true,
  basicstyle=\ttfamily\color{black},
  moredelim=**[is][\color{red}]{@}{@},
}

\begin{document}

\begin{lstlisting}[frame=single,style=base]
   public class A<T> extends B<T> {
      @Stack stack@;  //how can I achive this? Nothing should change, except for the textcolor. This code just prints the \textcolor command though.

       //ctor
       public A() {};
   }
\end{lstlisting}

\end{document}

enter image description here

  • Do you know why it doesn't work locally: \begin{lstlisting}[moredelim=**[is][\color{red}]{@}{@}]? – anderstood Nov 5 '14 at 20:16
  • What you mean by locally? In general you need to define a new style, so that you can use it later. – cacamailg Nov 10 '14 at 13:50
  • it doesn't work with rgb? i tried moredelim=**[is][\color[rgb]{0,1,0}]{@}{@} but it does not work – scmg May 31 '15 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.