2

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,
  commentstyle=\rmfamily,
  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:

Output of the above code fragment, showing that the math is rendered as desired in the // comment, but not in the /* comment */

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?

0
3

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.

enter image description here

\documentclass{article}

\usepackage{zi4} % if you want a \ttfamily font with bold, italic
\usepackage{listings}
\makeatletter
\lstset{
  language=C,
  basicstyle=\ttfamily,
  commentstyle=\rmfamily,
  texcl,
  escapechar =`,
  escapebegin=\lst@commentstyle,
}
\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.