# pass escapechar to newcommand inside listing

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}

• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Oct 9 '15 at 10:52
• hi, i updated the example – scmg Oct 9 '15 at 11:17
• Thanks. The crucial point here is that it's the escape char which lets listings recognize \lstemph to be command to be executed and not to be typeset literally. – user31729 Oct 9 '15 at 11:25
• Yes i know that, and wonder if there is anyway to make a new command/macro which is equivalent to the whole !\lstemph{}! ? – scmg Oct 9 '15 at 11:40
• I fear you're out of look unless changing listings internally – user31729 Oct 9 '15 at 16:19

## 2 Answers

If your command accepts only one argument and you'll never need to pass ending curly brace (}) to it, then you can "emulate" a command in listings by appropriate use of moredelim key:

moredelim=[is][\lstemph]{\\lstemph\{}{\}}


If you use your escapechar only to input \lstemph command, then you could simply use ! as delimiter:

moredelim=[is][\lstemph]{!}{!}


Full example:

\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},moredelim={[is][\lstemph]{\\lstemph\{}{\}}}]
return \lstemph{tmp_val + 2};
\end{lstlisting}

\begin{lstlisting}[style=myCpp,caption={Test},moredelim={[is][\lstemph]{!}{!}}]
return !tmp_val + 2!;
\end{lstlisting}

\end{document}


• prebreak and postbreak don't work anymore, so long lines are not auto-broken, however, my question was solved! Thank you. – scmg Oct 11 '15 at 6:50

As Christian pointed out in his comment, what you're asking is not possible. It is akin to asking:

In [some C language], how can I print a newline by writing n instead of \n?

One way or another, you need to specify what needs to be interpreted as TeX rather than as literal text. The only way to do that with listings is via its escape-character mechanism.