# texcl option to listing package's lstset - not working in block comments

I am using the listings package to typeset code listings.

I am further using the texcl option in my \lstset (\lstset{texcl}) so that I can write TeX code, including math, in comments.

However, this does not work in block comments.

Consider the following MWE (well, mostly minimal):

\documentclass{article}

\usepackage{zi4} % if you want a \ttfamily font with bold, italic
\usepackage{listings}
\lstset{
language=C,
basicstyle=\ttfamily,
texcl,
}

\begin{document}

\begin{lstlisting}
int main(int argc, char** argv) {
printf("Hello, world!\n");
}
// This is a comment with \TeX: $2^3 = 8$.
/* This is a block comment with \TeX: $2^3 = 8$ */
\end{lstlisting}

\end{document}

The output is:

I've tried this in language=Java as well, with the same results. (You need some language set so it can recognize comments.)

Since it also appears to be messing up the columns, I tried adding columns=fullflexible, which solves the columns issue but still doesn't allow the TeX comment lines.

I can tell that it realizes that it's a comment, because it's not \ttfamily, and writing some keywords like /* int */ doesn't get it bolded as a keyword. But the TeX code is not working.

How should I approach this problem?

That is texcl's expected behaviour. See the very beginning of subsection 4.14 in the listings documentation:

Note: Any escape to LaTeX may disturb the column alignment since the package can’t control the spacing there.

texcl=⟨true|false⟩ or texcl false activates or deactivates LaTeX comment lines. If activated, comment line delimiters are printed as usual, but the comment line text (up to the end of line) is read as LATEX code and typeset in comment style.

There you have it: texcl only affects line comments (or more precisely, to-end-of-line comments), not block comments. One workaround is to manually escape to LaTeX where needed inside block comments. Note that, in order to get a consistent comment style in line comments and in block comments, you need to apply your comment style at the beginning of those escapes to LaTeX; you can use the escapebegin key for that.

\documentclass{article}

\usepackage{zi4} % if you want a \ttfamily font with bold, italic
\usepackage{listings}
\makeatletter
\lstset{
language=C,
basicstyle=\ttfamily,
texcl,
escapechar =,
}
\makeatother

\begin{document}

\begin{lstlisting}
int main(int argc, char** argv) {
printf("Hello, world!\n");
}
// This is a comment with \TeX: $2^3 = 8$.
/* This is a block comment with \TeX: $2^3 = 8$` */
\end{lstlisting}

\end{document}