Here is code demonstrating different approaches, based on your tests. The last case is probably what you want. My comments are below the code.
\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.
Best Answer
The closing
|
is interpreted as part of the comment. Unfortunately, I know of no way to escapelistings
' one-line comment mode once it has started.One workaround is to use a
moredelim
that requires only an opening delimiter and ends at the end of the current line.