4

In the preamble of my document, I put the following command to allow strings wrapped with < and > printed as italic-styled comment:

\lstset{morecomment=[s]{<}{>},commentstyle=\rmfamily\itshape}

And according to my understanding of the documentation of listings package, if I want to temporarily disable this delimiter, I should use deletecomment=[s]{<}{>} as optional argument of a particular lstlisting environment:

\begin{lstlisting}[deletecomment=[s]{<}{>}]
\lstset{morecomment=[s]{<}{>},commentstyle=\rmfamily\itshape}
\end{lstlisting}

But LaTeX issued an error because of this.

So what is the correct way to disable a comment delimiter?

Here is a MWE:

\documentclass[a4paper]{article}
\usepackage{listings}
\lstset{morecomment=[s]{<}{>},commentstyle=\rmfamily\itshape}
\begin{document}
\begin{lstlisting}[deletecomment=[s]{<}{>}]
<text>
\end{lstlisting}
\end{document}
Naitree
  • 575

1 Answers1

4

The problem lies in the parsing of the lstlisting environment's optional argument. When the ] character in

deletecomment=[s]{<}{>}

is found, LaTeX assumes that it marks the end of that optional argument. This problem and a solution to it are explained in subsection 2.3 of the listings documentation.

language=[77]Fortran does not work inside an optional argument. You must put braces around the value if a value with optional argument is used inside an optional argument. In the case here write language={[77]Fortran} to select Fortran 77.

If you apply that trick here, by writing

deletecomment={[s]{<}{>}}

instead, everything works as expected.

enter image description here

\documentclass[a4paper]{article}
\usepackage{listings}
\lstset{morecomment=[s]{<}{>},commentstyle=\rmfamily\itshape}
\begin{document}
\begin{lstlisting}
<text>
\end{lstlisting}
\begin{lstlisting}[deletecomment={[s]{<}{>}}]
<text>
\end{lstlisting}
\end{document}
jub0bs
  • 58,916
  • Wow, thanks for explanation. Your answer clarified obscurity in my mind that I was not aware of. :-) – Naitree May 21 '14 at 10:00