2

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?

4

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

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

\lstdefinestyle{mystyle}{
  basicstyle = \linespread{0.8}\footnotesize\ttfamily,
  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}

enter image description here

| improve this answer | |
  • 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

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.