# Escaping underscore

I want to colorize a part of text within lstlisting. Here is an example:

\begin{lstlisting}[basicstyle=\linespread{0.8}\footnotesize\ttfamily, language=tcl, escapeinside={<@}{@>}]
set_param drc.disableLUTOverUtilError 1
} {

<@\textcolor{red}{

red\_text \\

set\_property PACKAGE\_PIN V20 [get\_ports {red[0]}]

}@>

\end{lstlisting}


\textcolor does not allow to use underscore without using \ in front of it. There are tens of underscores that I want use within \textcolor and I don't want to add \ for each underscore. Is there a way to get rid of this?

You probably rather want a delimiter than to escape to LaTeX:

\documentclass{article}
\usepackage{listings,xcolor}

\lstdefinestyle{mystyle}{
language   = tcl,
moredelim  = [is][\color{red}]{<@}{@>}
}

\begin{document}

\begin{lstlisting}[style=mystyle]
set_param drc.disableLUTOverUtilError 1
}{
<@red_text \\
set_property PACKAGE_PIN V20 [get_ports {red[0]}] @>
}
\end{lstlisting}

\end{document}


• Maybe something like moredelim = [is][\color{red}]{<RED}{RED>} or moredelim = [is][\color{red}]{<EMPH}{EMPH>} would be a better choice than {<@}{@>} – cgnieder Jan 31 '17 at 11:18