3

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}
3
+50

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.

  • I haven't had a chance to check on my document yet, but this looks like it will take care of the issues I had. I see that you've answered the related question too. Once I can check whether these answers work in that environment, I'll be back to accept (or humbly ask for clarification)! Many thanks! – Joshua Taylor Jun 12 '13 at 13:50
  • OK, when I first applied this to my document, I was still getting errors, but they were a result of a different, but related issue; it seems that when using comment and morecomment to have, e.g., ; and ;;, range prefix markers must begin with the longest comment prefix. I opened another question about that. After addressing that, this answer works for me. – Joshua Taylor Jun 12 '13 at 16:40
  • Glad this helped. In the fourth example above, the prefix ;; works for the markers, even though there is a morecomment with ;;;... – Andrew Swann Jun 12 '13 at 17:46
  • You're right, and that's a good point. That means that I'm wrong about what's happening in the question that I posted about it. If you take a look at that one, let me know (on that question, of course) mention whether you can reproduce the problem. Maybe it's something to do with the order in which the comments, prefix, and texcl are included? – Joshua Taylor Jun 12 '13 at 18:02

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.