i have defined a new command for colored bold text as follows to use in listing
environment:
\definecolor{mycolor}{rgb}{0,0.8,0}
\newcommand{\lstemph}[1]{\textcolor{mycolor}{#1}}
In my \lstdefinestyle
, there is an option provided: escapechar=!
, and everytime i want to use my command, i have to write !
before and after the command:
return !\lstemph{tmp\_val + 2}!;
How can I define the command to include the escape char, so I must not write the !
in the listing
?
Update: my sample code:
\documentclass{article}
\usepackage{t1enc}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{color}
\usepackage{listings}
\definecolor{mycolor}{rgb}{0,0.8,0}
\newcommand{\lstemph}[1]{\textcolor{mycolor}{#1}}
\lstloadlanguages{C++}
\lstdefinestyle{myCpp}
{
language=C++,
tabsize = 4,
framesep = 3mm,
frame=tb,
classoffset = 0,
basicstyle = \footnotesize\ttfamily,
keywordstyle = \bfseries\color[rgb]{0,0,1},
commentstyle = \itshape\color[rgb]{0.133,0.545,0.133},
stringstyle = \color[rgb]{0.627,0.126,0.941},
extendedchars = true,
breaklines = true,
prebreak = \textrightarrow,
postbreak = \textleftarrow,
escapeinside = {(*@}{@*)},
escapechar=!,
moredelim=**[is][\color{red}]{@}{@},
numbers = left,
numberstyle = \tiny,
stepnumber = 5
}
\begin{document}
\begin{lstlisting}[style=myCpp,caption={Test}]
return !\lstemph{tmp\_val + 2}!;
\end{lstlisting}
\end{document}
\documentclass{...}
and ending with\end{document}
. – user31729 Oct 9 '15 at 10:52listings
recognize\lstemph
to be command to be executed and not to be typeset literally. – user31729 Oct 9 '15 at 11:25listings
internally – user31729 Oct 9 '15 at 16:19