3

I sometimes write ordinary English prose in my here documents in a Perl usage sub, for example

 --[no]alias    [don't] use the alias list in the ini file (default noalias)

The problem is the a single apostrophe seems to disturb the listings package. A short example:

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

This results (with MiKTEX/TeXstudio) in

Result

The apostrophe in commentA is ignored, while the one in the here document seems to be interpreted as an opening quote.

I have tried to escape the here terminator, but to no avail. Any suggestions (short of avoiding apostrophes in here documents)?

3

You can add a morestring for these here statements. With the s option you can have a pair of delimiters, though this is not quite clear in the documentation:

Sample output

\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}
  • I didn't even notice the bold do. Glad there is a solution. – Johannes_B Oct 22 '14 at 12:48
  • I have two minor worries. I usually use meaningful here terminators, which requires several morestrings, and in calls to subs with several arguments like mysub (<<HERE, $arg1, $arg2); … HERE the first line is formatted as a string -- unless, of course, I use [morestring={[s]{HERE, $arg1, $arg2);}{HERE}] and then I lose a possible keywordstyle for keywords in the arguments. – Henrik Snog Oct 22 '14 at 18:52
  • @HenrikSnog Agreed - the solution is going to work best if you keep the keyword and HERE name spaces disjoint. In particular, your HERE name should not be something that could be the argument to a binary shift operator, or you should use a space after << in such a case. – Andrew Swann Oct 23 '14 at 6:37

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.