使用 texcl 在注释中列出范围结束标记

使用 texcl 在注释中列出范围结束标记

我正在尝试使用该listings包来生成一些 Lisp 风格代码的漂亮列表(其中行注释以 开头;)。我想保持代码可编译,因此将范围标记放在注释中。但是,一旦启用注释语法,字符就会丢失(或者删除的字符不够多,这取决于您的观点),并且当texcl启用注释选项时,listings似乎根本找不到范围标记的结尾!以下是问题的示例以及创建它的代码:

在此处输入图片描述

\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}

答案1

以下是根据您的测试演示不同方法的代码。最后一种情况可能是您想要的。我的注释在代码下方。

示例输出

\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}

首先,标记代码范围应定义为从开头开始的字符串,因此在您的情况下,它们应包含标记;;。更改此项可使正确的代码行在所有情况下都包含在输出中。

现在请注意,texcl如果未定义注释语法,则该选项不执行任何操作。但是,可以mathescape使用该选项将表达式转换$...$为数学,但这将贯穿整个代码清单,如上面的第二个示例所示。

语法方面,注释声明的行为取决于texcl选项是否打开。没有texcl它,只需声明;为注释前缀,参见示例 3。texcl多个;会被吞没,除非您添加适当的组合;;;;;选项morecomment。我怀疑这不是应该textcl影响此行为的预期行为,但您在尝试示例时会看到效果。

相关内容