# Maintainable typesetting of placeholders in code snippets

I'm looking for a maintainable way of typesetting placeholders in code snippets. In my listings, I would like that anything contained between the two delimiters @< and >@ be

• typeset in (typewriter) italics in some colour (RubineRed, say),
• preceded by an upright (not italics) opening chevron,
• appended by an upright (not italics) closing chevron.

For instance, @<some_value>@ should (in one of my listings) be typeset as

Unfortunately, I can't figure out a way of getting my delimiters (@< and >@) to be replaced by upright chevrons without having to hardcode the latter or without breaking what moredelim does. I want to avoid hardcoding the chevrons because it's not maintainable; imagine if I changed my mind later on as to how my placeholders should be typeset...

Below is an example of what I have at the moment and what I would like to get. Any idea?

\documentclass{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{listings}

\lstset
{
moredelim       = **[is][\color{RubineRed}\itshape]{@<}{>@},
basicstyle  = \ttfamily,
}

\begin{document}

Current strategy: hard code the chevrons (which, sadly, get italicised), e.g.
\begin{lstlisting}
x=@<<some_value>>@
\end{lstlisting}

Desired output of \texttt{\detokenize{x=@<some_value>@}} within a listing:
{
\ttfamily
x=%
{%
\color{RubineRed}
\textless\textit{some\_value}\textgreater
}
}

\end{document}


\documentclass{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{listings}

\def\zz{\color{RubineRed}\textless\bgroup\itshape\aftergroup\endzz}
\def\endzz{\textgreater\egroup}
\lstset
{
moredelim       = **[is][\zz]{@<}{>@},
basicstyle  = \ttfamily,
}

\begin{document}

Now the chevrons don't get italicised:
\begin{lstlisting}
x=@<some_value>@
\end{lstlisting}

\end{document}

• Great. That's exactly what I needed. It's the first time I come across \aftergroup! I've still got a lot of TeX to learn... – jub0bs Nov 18 '13 at 18:50

After some tinkering, I've come up with an alternative.

• I replaced < and > by the sexier and more common \textlangle and \textrangle;
• The placeholder text is now typeset in italics in the normal (including italic correction \/ at the end), which, arguably, looks much better.
• Unfortunately, there were some weird spacing issues with David's approach, so I decided to take the escape-to-LaTeX route. I defined a macro for typesetting placeholders, which can be used in listings by escaping to LaTeX; the escapechar is up to you.

\documentclass{article}

\usepackage{textcomp}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}

\begin{document}

\let\us\textunderscore

\newcommand\placeholder[1]%
{%
\bgroup
\normalfont\upshape\color{RubineRed}%
\textlangle{\itshape #1\/}\textrangle%
\egroup
}

\lstset
{%
basicstyle  = \ttfamily,
escapechar=,
}

\begin{lstlisting}
for i= \placeholder{first\us{}value}:\placeholder{last\us{}value}
% do stuff
end
\end{lstlisting}

\end{document}
`