# Can comment delimiters be typeset in a style different from commentstyle?

Using listings, I'm trying to typeset single-line comments in a very specific way: although whatever follows should be typeset according to the value passed to the commentstyle key, the opening delimiter should be typeset in the identifier style (i.e. that of the "normal code").

I've been playing around with the different types (s,m,l) and the i prefix of morecomment offered by the listings package, to no avail. Any idea?

Edit: Of course, one requirement is that I shouldn't have to alter the listing itself to to obtain the desired output; introducing delimiters in the listing is cheating :)

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\lstset%
{%
basicstyle=\ttfamily,
commentstyle=\itshape\color{blue},
morecomment=[s]{\%\%}{\^^M},
}

\begin{document}

\lstset{}
\begin{lstlisting}
confusion=Inf %% this is a comment
\end{lstlisting}

How can I get the opening comment delimiter (\texttt{\%\%}) to be typeset,
not in blue, but in the default colour (black)?

\end{document}


## 2 Answers

I don't think it's possible with the comment key, but you could (ab)use escapeinside:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset%
{%
basicstyle=\ttfamily,
%    commentstyle=\itshape\color{blue},
%    morecomment=[s]{\%\%}{\^^M},
escapeinside={\%\%}{\^^M},
escapebegin={\%\%\itshape\color{blue}\obeyspaces}
}
\begin{document}

\lstset{}
\begin{lstlisting}
confusion=Inf %% this is a comment
confusion=Inf %%this is  a comment
confusion=Inf %% this is $\alpha$ comment
\end{lstlisting}

\end{document}


(As you pointed out yourself, \obeyspaces should be at the end in order to have all spaces after the comment marker honoured.)

The downside is that TeX text will be typeset instead of being output verbatim:

• Hey thanks. I'm going to ponder whether your approach is a good idea in my case; escaping to LaTeX may be problematic. Side note: putting \obeyspaces at the end seems to obviate the limitation you mentioned; try escapebegin={\%\%\itshape\color{blue}\obeyspaces} – jubobs Dec 7 '13 at 17:37
• Unfortunately, I can't afford to escape to LaTeX. I had to resort to other means. Thanks anyway. – jubobs Dec 7 '13 at 20:50

After a lot of experimentation, I still couldn't figure out how to get the desired output using morecomment; I ended up using moredelim (with the il prefix) instead, and I defined a \processDelimiter macro to process each occurence of my %% delimiter in the listing. See below (I left a few comments in my code).

This solution is mostly okay but suffers from a really puzzling spacing problem, which I'll probably document in another question (edit: see Weird spacing issue using listings delimiters).

There may be better ways of getting the desired output, but I'm at my wit's end.

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

% switch to keep track of context (in a comment or not)
\newif\ifcom\comfalse

\makeatletter

% This is what happens if a delimiter is encountered
\def\processDelimiter{%
\ifcom%                     % In this case, we're already in a comment.
\else                       % Otherwise, we just started a comment;
\global\comtrue%        % set the switch to true and
{\lst@basicstyle\%\%}%  % typeset the delimiter in the basic style.
\fi
\lst@commentstyle%          % In any case, apply the comment style.
}

% Reset the switch at each End-Of-Line character.
\lst@AddToHook{EOL}{\global\comfalse}

\makeatother

\lstset{
basicstyle   = \ttfamily,
commentstyle = \itshape\color{blue},
moredelim    = [il][\processDelimiter]{\%\%},
}

\begin{document}

\begin{lstlisting}
x:=1000; %% this is a comment %% still in a comment
x:=x/2; %% another comment
\end{lstlisting}

\end{document}