1

I am inserting snippets of python code into a latex document using the listing package. When writing code comments in the code snippet the indentation level is not maintained.

Minimal working example:

\documentclass{article}
\usepackage{listings}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},   
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,         
breaklines=true,                 
captionpos=b,                    
keepspaces=true,                 
numbers=left,                    
numbersep=5pt,                  
showspaces=false,                
showstringspaces=false,
showtabs=false,                  
tabsize=2
}
\begin{document}
\begin{lstlisting}[language=Python]
def blahblahblah:
''' some very loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong code description'''
\end{lstlisting}
\end{document}

Using breaklines=true breaks the long code to fit on the page but it does not maintain the current indentation.

Question: Is there a way to get listing to recognise and maintain the current indentation state within listings?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.