4

I am using the listings package for source code in my LaTeX document. I am trying to set the style for docstrings in the Python programming language:

\documentclass{article}
\usepackage{listings}
\begin{document}

\lstset{
    language=Python,
    showstringspaces=false,
    basicstyle=\ttfamily\small,
    commentstyle=\itshape
}

\begin{lstlisting}
# This is a comment
""" This is a docstring """
\end{lstlisting}

\end{document}

As you can see if you compile this code, setting the commentstyle does not change the style of docstrings. How can I set the style of docstrings?

3

Working from section 3.2 of the listings documentation, use the morecomment parameter to use triple quotes as delimiters:

\documentclass{article}
\usepackage{listings}
\begin{document}

\lstset{
    language=Python,
    showstringspaces=false,
    basicstyle=\ttfamily\small,
    commentstyle=\itshape,
    morecomment=[s]{"""}{"""},
}

\begin{lstlisting}
# This is a comment
""" This is a docstring """
\end{lstlisting}

\end{document}

enter image description here

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.