25

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}

1 Answer 1

31

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

3
  • Do you know why it doesn't work locally: \begin{lstlisting}[moredelim=**[is][\color{red}]{@}{@}]?
    – anderstood
    Commented Nov 5, 2014 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
    Commented Nov 10, 2014 at 13:50
  • it doesn't work with rgb? i tried moredelim=**[is][\color[rgb]{0,1,0}]{@}{@} but it does not work
    – scmg
    Commented May 31, 2015 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .