我有时会在 Perl 使用子文档中编写普通的英语散文,例如
--[no]alias [don't] use the alias list in the ini file (default noalias)
问题在于单个撇号似乎会扰乱列表包。一个简短的例子:
\documentclass[article]{memoir}
\usepackage{listings}
\lstset{language=Perl,stringstyle=\slshape}
\begin{document}
\begin{lstlisting}
my $commentA = "Please, don't do this";
my $commentB = "But do this";
print <<HERE;
Please, don't do this
But do this
HERE
my $commentC = "Please, don't do this";
my $commentD = "But do this";
\end{lstlisting}
\end{document}
结果(使用 MiKTEX/TeXstudio)
commentA 中的撇号被忽略,而此处文档中的撇号似乎被解释为开头的引号。
我尝试过避开 here 终止符,但无济于事。有什么建议吗(除了避免在 here 文档中使用撇号)?
答案1
您可以morestring
在此处添加这些语句。使用该s
选项,您可以有一对分隔符,尽管这在文档中并不十分清楚:
\documentclass[article]{memoir}
\usepackage{listings}
\lstset{language=Perl,stringstyle=\slshape}
\begin{document}
\begin{lstlisting}[morestring={[s]{<<HERE;}{HERE}}]
my $commentA = "Please, don't do this";
my $commentB = "But do this";
print <<HERE;
Please, don't do this
But do this
HERE
my $commentC = "Please, don't do this";
my $commentD = "But do this";
\end{lstlisting}
\end{document}