Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have some code I want to typeset as verbatim (lstlisting would be ok as well) - but I want to highlight some parts of it via a different background color, i.e. something like this:

\begin{verbatim}
hello \colorbox{red}{world} blah
hello world blub
\end{verbatim}

Where colorbox should not be interpreted verbatim and should not introduce additional horizontal spaces, i.e. blah and blub should be correctly aligned (like without any highlighting).

I tried the Verbatim and lstlisting environments like this:

\begin{lstlisting}[escapechar=\%]

or

\begin{Verbatim}[commandchars=\\\{\}]

in combination with a then escaped colorbox command - the highlighting works - but the additional spaces are introduced and screw up the character alignment.

How can I do it right?

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

I think the alltt environment from the package with the same name is better suited here. It makes everything verbatim except \,{ and }, similar to \begin{Verbatim}[commandchars=\\\{\}].

The mentioned added space is due to the used \fboxsep which makes the box bigger. You can avoid this by setting this value to zero, either globally or locally using a own macro. This however makes the box very tight on all four sites. An added \strut makes sure that it has always the same height and depth.

\documentclass{article}
\usepackage{xcolor}
\usepackage{alltt}
% Global:
\setlength{\fboxsep}{0pt}
% Better:
\newcommand\hi[2][red]{{%
  \setlength{\fboxsep}{0pt}%
  \colorbox{#1}{#2\strut}%
}}
\begin{document}
\begin{alltt}
hello \colorbox{red}{world\strut} blah
hello world blub
hello \hi{world} blub
\end{alltt}
\end{document}

Another alternative, the best one IHMO, is to compensate for the fboxsep before and after the box:

\documentclass{standalone}
\usepackage{xcolor}
\usepackage{alltt}
% Compensate for fbox sep:
\newcommand\Hi[2][red]{%
  \hspace*{-\fboxsep}%
  \colorbox{#1}{#2}%
  \hspace*{-\fboxsep}%
}
\begin{document}
\begin{alltt}
hello world blah
hello \Hi{world} blah
\end{alltt}
\end{document}

Result:

Result

share|improve this answer
    
awesome answer! –  maxschlepzig Feb 22 '11 at 22:06
add comment

Just set \fboxsep=0pt to reduce the extra space around \colorbox.

And here's another solution:

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\colorlet{mycolor}{red!50}
\sethlcolor{mycolor}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,columns=flexible,
  escapechar=`}
\begin{document}
\begin{lstlisting}
foo`\hl{bar}`foo
\end{lstlisting}
\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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