How to make this code works:
\documentclass[dvipsnames]{report}
\usepackage[utf8]{inputenc}
\usepackage{xspace}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{s}{escapechar={|}}
\begin{document}
\begin{lstlisting}[style=s]
foo
|\color{red}{bar_42}|
\end{lstlisting}
\end{document}
Without having to add a \
before the _
?
I have a large portion of code to put into my listing with lots of _
and I don't want to manually add a \
everywhere.
underscore
package has some limitations (like you can't have _ in filenames). A solution to this is changing the category code of the underscore. Corresponding answer.