[Tex/LaTex] lstlisting can’t keep page margins

listings

I am confused, although I admit this is the first time I am using lstlisting. I want to insert some python code in my thesis appendices.

Some lines may be quite long. But the document doesn't seem to be able to break the lines and keep the document's margins. Is there a way to set linebreaks when the code reaches the page margin?

Currently I have something like this (sorry, my preamble has 200 lines, I won't insert it all here).

\documentclass[a4paper,12pt,twoside,openright, english]{book}
\usepackage{listings}

\section{Python code}
The following sections include the functions and scripts.

\begin{lstlisting}[language=Python]
import numpy as np
import pandas as pd
import os
dir = # insert here path where the data are saved

files = []
for f in os.listdir(dir):
    if (f.startswith('Temp'))&(f.endswith('matched.csv')): # find matched logs
        files.append(f)

# some MANUAL INPUT for plotting
interval=(0,-1)  ## for slicing of relevant logs
polyOrder = 6

# etc.
\end{lstlisting}

Can I modify the setting somehow, so that the code doesn't just "walk off" the page?

Best Answer

This should probably work.

\documentclass[a4paper,12pt,twoside,openright, english]{book}
\usepackage{listings}
\usepackage{lmodern}  % for bold teletype font
\usepackage{amsmath}  % for \hookrightarrow
\usepackage{xcolor}   % for \textcolor

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
  frame=single,
  breaklines=true,
  postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}

\begin{document}
\section{Python code}
The following sections include the functions and scripts.

\begin{lstlisting}[language=Python]
import numpy as np
import pandas as pd
import os
dir = # insert here path where the data are saved

files = []
for f in os.listdir(dir):
    if (f.startswith('Temp'))&(f.endswith('matched.csv')): # find matched logs
        files.append(f)

# some MANUAL INPUT for plotting
interval=(0,-1)  ## for slicing of relevant logs
polyOrder = 6

# This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. 
\end{lstlisting}
\end{document}

Output:

enter image description here

If you don't want the red arrows,

\documentclass[a4paper,12pt,twoside,openright, english]{book}
\usepackage{listings}
\usepackage{lmodern}  % for bold teletype font
\usepackage{amsmath}  % for \hookrightarrow
\usepackage{xcolor}   % for \textcolor

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
  frame=single,
  breaklines=true
}

\begin{document}
\section{Python code}
The following sections include the functions and scripts.

\begin{lstlisting}[language=Python]
import numpy as np
import pandas as pd
import os
dir = # insert here path where the data are saved

files = []
for f in os.listdir(dir):
    if (f.startswith('Temp'))&(f.endswith('matched.csv')): # find matched logs
        files.append(f)

# some MANUAL INPUT for plotting
interval=(0,-1)  ## for slicing of relevant logs
polyOrder = 6

# This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. This is a long text. This is used to test if the line breaks (or) not. 
\end{lstlisting}
\end{document}

enter image description here

Related Question