# Cannot emphasize underscored words in listings

I declared an emphasis mode that underlines and colors words in red, as follows:

\lstset{columns=fullflexible,basicstyle=\vttfamily,emph={\color{red}\underline}}


However, when using it in a lstlisting block, emphasising "my_function" does not render it underscored and in red.

Here is a minimal working example:

\documentclass{article}
\usepackage{listings}
\begin{document}

\lstset{
language=C,
basicstyle=\ttfamily,
emph={\color{red}\underline}
}

\begin{lstlisting}[emph={my\_function}]
int my_function() {
}
\end{lstlisting}


\end{document}

How can I fix this ? Thank you !

• It would help us a lot to help you, if you would provide a minimum working example of fully compilable code illustrating your problem. – JMP Apr 14 '16 at 14:44
• Welcome to TeX.SX AnaK! As said by @JMP, please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document} -- i.e. give us a so-called minimal working example (MWE). That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – ebosi Apr 14 '16 at 14:48

As specified in the listings manual (§2.8, p. 19), the (optional) emph option of \lstset is to define what to (automatically) emphasis, when the emphstyle option is for defining how to (automatically or manually as you're doing) emphasis.

So you just need to replace emph by emphstyle! (-:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}

\lstset{
language=C,
basicstyle=\ttfamily,
emphstyle={\color{red}\underline}
}

\begin{lstlisting}[emph={my\_function}]
int my_function() {
}
\end{lstlisting}

\end{document}


• Thanks for the answer ! While compiling your example gives the expected result, I still cannot make my more complex one work...I am using: \usepackage[T1]{fontenc} \usepackage[variablett]{lmodern} – AnaK Apr 14 '16 at 15:29
• It's then likely another issue that needs another thread (-; But the code still compiles fine for me, even with the packages you mention. – ebosi Apr 14 '16 at 15:38
• So the same problem occurs if I declare: moredelim=[is][\color{red}\bfseries\vttfamily]{@}{@} in the \lstdefinelanguage environment. The underscore appears longer again...Any ideas how to fix this ? Thanks ! – AnaK Sep 5 '16 at 17:58
• this is a separate question, and you should thus ask it as a new one (but you add a link to this one if you want). You might thus be more likely to get a valid answer... and to help others! By the way, it is \ttfamily and not \vttfamily, isn't it? – ebosi Sep 5 '16 at 19:35
• Oh, my mistake. It stands for \newcommand*{\vttfamily}{% \fontencoding{T1}\fontfamily{lmvtt}\selectfont } – AnaK Sep 5 '16 at 19:39