4

I'm looking for a way to emphasize specific names and hex numbers in an lstlisting. Using emph works for names, but not for hex numbers.

What did not work

Consider the following MWE:

\documentclass[a4paper]{article}
\usepackage{xcolor}
\usepackage{listings}
\begin{document}
\lstset{
  emph={esp,0xffffcd80},
  emphstyle=\color{red}
}
\begin{lstlisting}
(gdb) info registers
eax            0xffffd10f       -12017
ecx            0xffffce30       -12752
edx            0xffffce54       -12716
ebx            0x0              0
esp            0xffffcd80       0xffffcd80
ebp            0xffffcdf8       0xffffcdf8
\end{lstlisting}
\end{document}

which renders the name esp in red, but does not render the address 0xffffcd80 in the same way, although both are given in the emph option.

lstlisting with emph

Another approach involves textcolor within an escaped region:

\begin{lstlisting}[escapechar=!]
ebx            0x0              0
esp            !\textcolor{red}{0xffffcd80}!       0xffffcd80
ebp            0xffffcdf8       0xffffcdf8
\end{lstlisting}

This shows the address in red, but breaks the spacing as shown in the following screenshot:

textcolor in escaped area

Any ideas how to retain correct spacing/alignment and be able to highlight certain names/addresses in an lstlisting?

3

I think you can use this answer: Problems with spacing in lstlisting with escaped code

Your code would then look like this:

\documentclass[a4paper]{article}
\usepackage{xcolor}
\usepackage{listings}
\begin{document}
\lstset{
  emph={esp},
  emphstyle=\color{red},
  moredelim=[is][\color{red}]{!*}{*!}
}
\begin{lstlisting}
(gdb) info registers
eax            0xffffd10f       -12017
ecx            0xffffce30       -12752
edx            0xffffce54       -12716
ebx            0x0              0
esp            !*0xffffcd80*!       0xffffcd80
ebp            0xffffcdf8       0xffffcdf8
\end{lstlisting}
\end{document}

Which then looks like this: Render of MWE

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.