3

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?

output


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]{"""}{"""},
  moredelim=[s][\lst@commentstyle]{<<}{>>},
}
\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

output 2

14
  • 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 << ... >>)?
    – jub0bs
    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!
    – jub0bs
    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.
    – jub0bs
    Mar 20 '14 at 16:38
2

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

enter image description here

\documentclass{article}

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

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

\end{document}
5
  • 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?
    – jub0bs
    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...
    – jub0bs
    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.
    – jub0bs
    Mar 7 '14 at 10:34

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.