4

I'm trying to highlight code fragments in the listings environment. To do this, I'm using a colorbox with no padding as shown in this answer:

\newcommand{\reducedstrut}{\vrule width 0pt height .9\ht\strutbox depth .9\dp\strutbox\relax}
\newcommand{\hlight}[1]{\begingroup\setlength{\fboxsep}{0pt}\colorbox{yellow}{\reducedstrut#1\/}\endgroup}

The listings are set up to syntax highlight C code and use ttfamily as the font:

\lstset{ 
    language=C, 
    basicstyle=\small\ttfamily, 
    breakatwhitespace=true, 
    columns=fullflexible,
    tabsize=2,
    escapeinside={(*@}{@*)}
}

Now, if I create a lstlisting, the special characters contained therein are displayed in ttfamily:

\begin{lstlisting}
int main() {
    printf("\n");
}
\end{lstlisting}

example listing

However, if I highlight the code, special characters, ({, } and \ in the example below) are displayed using the default latex font:

\begin{lstlisting}
(*@\hlight{\textbf{int main() \{}}@*)
    (*@\hlight{printf("\textbackslash n");}@*)
}
\end{lstlisting}

example listing 2

How do I define \hlight so that the font remains the same in the inner colorbox?

Here is a MWE:

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{listings}
\usepackage[T1]{fontenc}
\lstset{ 
    language=C, 
    basicstyle=\small\ttfamily, 
    breakatwhitespace=true, 
    columns=fullflexible,
    tabsize=2,
    escapeinside={(*@}{@*)}
}
\newcommand{\reducedstrut}{\vrule width 0pt height .9\ht\strutbox depth .9\dp\strutbox\relax}
\newcommand{\hlight}[1]{\begingroup\setlength{\fboxsep}{0pt}\colorbox{yellow}{\reducedstrut#1\/}\endgroup}
\begin{document}

\begin{lstlisting}
int main() {
    printf("\n");
}
\end{lstlisting}

\begin{lstlisting}
(*@\hlight{\textbf{int main() \{}}@*)
    (*@\hlight{printf("\textbackslash n");}@*)
}
\end{lstlisting}

\end{document}
  • Did you try adding \usepackage[T1]{fontenc}? – egreg Sep 9 '16 at 14:18
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – egreg Sep 9 '16 at 15:26
  • Adding the package fixed the problem! Could you please add this as an answer (possibly with an explanation) so I can accept it? – peteykun Sep 9 '16 at 16:15
3

With the default OT1 encoding, the commands \{, \} and \textbackslash take the corresponding character from the math font even if the context is typewriter type. One might try and fix this, but it's simpler to use the T1 encoding.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\lstset{ 
    language=C, 
    basicstyle=\small\ttfamily, 
    breakatwhitespace=true, 
    columns=fullflexible,
    tabsize=2,
    escapeinside={(*@}{@*)}
}
\newcommand{\reducedstrut}{\vrule width 0pt height .9\ht\strutbox depth .9\dp\strutbox\relax}
\newcommand{\hlight}[1]{\begingroup\setlength{\fboxsep}{0pt}\colorbox{yellow}{\reducedstrut#1\/}\endgroup}
\begin{document}

\begin{lstlisting}
int main() {
    printf("\n");
}
\end{lstlisting}

\begin{lstlisting}
(*@\hlight{\textbf{int main() \{}}@*)
    (*@\hlight{printf("\textbackslash n");}@*)
}
\end{lstlisting}

\end{document}

enter image description here

  • Is there any way to do this without relying on T1 fontenc? The submission instructions for paper do not allow me to use the T1 fontenc package and they recommend using CM super instead. – peteykun Nov 25 '16 at 7:45
  • @peteykun I think you're misunderstanding the instructions. There's no CM super package and those fonts are used precisely when T1 is passed to fontenc. – egreg Nov 25 '16 at 8:43

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.