# Escape comment syntax in strings with listings

Consider the following:

\documentclass{standalone}
\usepackage{listings}
\lstset{
language=Python,
morecomment=[s]{<<}{>>},
}

\begin{document}
\begin{lstlisting}
def expand():
"""<<expand documentation>>"""
for hook in noweb:
<<expand noweb hook>>
\end{lstlisting}
\end{document}


How can I get both occurrences of the syntax to be styled as the latter?

Edit for the wonderful Jubobs: (a less minimal working example :)

\documentclass{standalone}
\usepackage{listings}
\lstset{
language=Python,
showstringspaces=true,
showspaces=false,
showtabs=false,
moredelim=*[s][\lst@stringstyle]{"""}{"""},
}
\renewcommand*\lstlistlistingname{List of Listings}
\lstMakeShortInline[basicstyle=\small\ttfamily]"

\begin{document}
\begin{lstlisting}
def expand():
"""foo <<expand documentation>>"""
for hook in noweb:
<<expand noweb hook>>

def marked_alone(node, neighbors):
"""\forall n \in N(n), "marked"(n) = 0

There are no marked nodes in this node's neighborhood."""

return not any(map(lambda n: n['marked'], neighbors))
\end{lstlisting}
\end{document}


with output

• So you want anything delimited by << and >>, whether it be inside a docstring or elsewhere, to be typeset in comment style? Is that correct? What should be the style applied to docstrings (outside << ... >>)? Mar 2 '14 at 17:33
• @Jubobs You are correct; And I dunno—I didn't really think about that. showstringspaces should definitely be disabled in docstrings (it would get super messy, as you can imagine), but I can't think of anything extra to add to that style. Mar 2 '14 at 18:10
• @Jubobs Thanks! And for the record, my brain is a little toasted at the moment. I've got the draft of my undergrad thesis (30 pages single-spaced) due in two hours; I've already been up for about 40 hours. I might be a tad dramatic ;) Mar 2 '14 at 19:33
• I know the feeling. Good luck! Mar 2 '14 at 19:59
• Sean, I'm tempted to vote to close this as "unclear what you're asking", unless you add further specifications. Mar 20 '14 at 16:38

Try the following. If it suits you, I'll flesh out my answer a bit more.

\documentclass{article}

\usepackage{listings}
\makeatletter
\lstset{
language=Python,
moredelim=*[s][\lst@stringstyle]{"""}{"""},
showstringspaces=false,
}
\makeatother

\begin{document}
\begin{lstlisting}
def expand():
"""foo <<expand documentation>>"""
for hook in noweb:
<<expand noweb hook>>
\end{lstlisting}

\end{document}

• Looks fantastic to me! If you're curious, there seems to be a small issue if you set showstringspaces=true: i.stack.imgur.com/DKrxB.png) (gist.github.com/vermiculus/590ec4d6edfeb415592a) Mar 2 '14 at 18:44
• @SeanAllred I'll look into it. That snippet visible on your screenshot... could you add it to your question, or make it available somewhere? Mar 2 '14 at 18:46
• See my edit above :) Mar 2 '14 at 18:57
• @SeanAllred My approach is little more than a trick, instead of a satisfactory solution. I can't think of a way of solving the showstringspaces problem. Maybe in a few years, when I know the listings source code like the back of my hand... Mar 2 '14 at 19:58
• @SeanAllred To be honest, a comment (typeset as such) inside a string (be it a docstring or not) doesn't really make sense to me... Perhaps you should just define """ to be comment delimiters (morecomment=[s]{"""}{"""}); that would save you some hassle. Mar 7 '14 at 10:34