Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting a Chunk of Code within a lstlisting

I have a bunch of code in a lstlisting environment. How can I highlight (color background) just a particular chunk of code within the environment?

like image 903
Ryan R. Rosario Avatar asked Aug 20 '09 06:08

Ryan R. Rosario


2 Answers

You can use \colorbox and an escape character inside your listing:

Add to your preamble

  \usepackage{color}    \definecolor{light-gray}{gray}{0.80} 

then use it like this in your document:

  \begin{lstlisting}[escapechar=!]   def mult(m: Matrix[Int], n: Matrix[Int]) {     val p = !\colorbox{light-gray}{new MatrixInt}!(m.rows, n.cols)   }   \end{lstlisting} 
like image 88
Iulian Dragos Avatar answered Oct 06 '22 06:10

Iulian Dragos


It's a bit cumbersome, but you can break the code into several lstlisting environments.

\begin{lstlisting} line \end{lstlisting} \vspace{-\baselineskip} \begin{lstlisting}[backgroundcolor=\color{pink}] very interesting \end{lstlisting} \vspace{-\baselineskip} \begin{lstlisting} line line \end{lstlisting} 
like image 42
Anton Geraschenko Avatar answered Oct 06 '22 06:10

Anton Geraschenko