Document ClassesHighlight one line in listings environment

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
ajg112
Posts: 3
Joined: Wed Oct 22, 2008 5:32 pm

Highlight one line in listings environment

Post by ajg112 »

Hi All,

I'm using the listings environment to syntax highlight some code. I'd like to be able to highlight just a single line of the code, perhaps with a boxed outline, or a different background colour.

Code: Select all

\begin{lstlisting}
   start:$
   mov   r1,   xF0  -- Reset ptr value$
   mov   r2,   x00  -- Reset total value$
\end[lstlisting}
So, can I highlight just the

Code: Select all

'mov r1, xF0  -- Reset ptr value'
line?

Does anyone know if this possible, and if so, how it is done?

Many thanks
Andy

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: Highlight one line in listings environment

Post by phi »

I tried several things including adding customized delimiters (works fine as described in the manual, but is unusable for things like background colors) or combining the listings and fancyvrb packages (doesn't seem to work), but was never really successful. Perhaps it's easier to use an external pretty printer, or to draw a highlight rectangle manually.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Highlight one line in listings environment

Post by Juanjo »

Try this and modify it at your own convenience:

Code: Select all

\documentclass[12pt]{article}
\usepackage{listings,color}
\begin{document}

\newcommand{\Hilight}{\makebox[0pt][l]{\color{cyan}\rule[-4pt]{0.65\linewidth}{14pt}}}
\begin{lstlisting}[escapechar=\%]
   start:$
   %\Hilight%mov   r1,   xF0  -- Reset ptr value$
   mov   r2,   x00  -- Reset total value$
\end{lstlisting}
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
ajg112
Posts: 3
Joined: Wed Oct 22, 2008 5:32 pm

Re: Highlight one line in listings environment

Post by ajg112 »

phi, thank you for your efforts.

Juanjo, that works brilliantly. Many thanks.
I made a slight tweak:
\definecolor{lightGrey}{rgb}{0.9, 0.9, 0.9}
\newcommand{\Hilight}{\makebox[0pt][l]{\color{lightGrey}\rule[-0.45em]{\linewidth}{1.5em}}}

Thanks again
Andy
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Re: Highlight one line in listings environment

Post by Juanjo »

I've seen that, before I replied, you sent the same question to comp.text.tex. For the sake of completeness (and politeness), I think it would convenient to post here any interesting answer you receive there and conversely.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply