[Tex/LaTex] listings range end markers in comments with texcl

listings

I'm trying to using the listings package to generate nice listings of some Lisp-style code (where line comments begin with a ;). I'd like to keep the code compilable, and so put the range markers inside of a comment. However, characters go missing (or not enough are removed, depending on your perspective) once comment syntax is enabled, and when the texcl option is enabled with comments, listings doesn't seem to find the end of range marker at all! Here's an example of the problem, and the code that creates it:

enter image description here

\documentclass{article}

\usepackage{listings}

\begin{document}

\lstset{%
  basicstyle=\ttfamily\small,%
  rangeprefix=MARK\ ,%
  includerangemarker=false,%
}

$-$texcl, $-$comment
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
a line to include
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

$+$texcl, $-$comment
\lstset{texcl=true}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
a line to include
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

$-$texcl, $+$comment
\lstset{texcl=false,comment=[l];}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
a line to include
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

$+$texcl, $+$comment
\lstset{texcl=true,comment=[l];}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
a line to include
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

\end{document}

Best Answer

Here is code demonstrating different approaches, based on your tests. The last case is probably what you want. My comments are below the code.

Sample output

\documentclass{article}

\usepackage{listings}

\begin{document}

\lstset{%
  basicstyle=\ttfamily\small,%
  rangeprefix=;;MARK\ ,%
  includerangemarker=false,%
  texcl=false
}

$-$texcl, $-$comment
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

$+$mathescape, $-$comment
\lstset{mathescape=true}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

$-$texcl, $+$comment
\lstset{texcl=false,mathescape=false,comment=[l];;}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

$+$texcl, $+$comment
\lstset{texcl=true,comment=[l];,morecomment=[l];;,morecomment=[l];;;}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}

\end{document}

Firstly the markers code ranges should be defined to be strings that start at the beginning of the, so in your case they should include the ;; marker. Changing this gets the correct lines of code included in your output in all cases.

Now note that the texcl option does nothing if no syntax for comments has been defined. There is however the mathescape option that can be used to turn expressions $...$ in to mathematics, but this will be throughout the code listing, as in the second example above.

Syntax wise there is a different behaviour for the comment declarations depending on whether the texcl option is on or not. Without texcl it is sufficient to declare ; as the comment prefix, see example 3. With texcl multiple ; get swallowed, unless you add the appropriate combinations ;; and ;;; with the morecomment option. I suspect that this is not the intended behaviour that the textcl should affect this, but you will see the effect when trying examples.