我很困惑,尽管我承认这是我第一次使用lstlisting
。我想在我的论文附录中插入一些python代码。
有些行可能很长。但文档似乎无法换行并保持文档的页边距。当代码到达页边距时,有没有办法设置换行?
目前我有类似的东西(抱歉,我的序言有 200 行,我不会在这里全部插入)。
\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}
我能以某种方式修改设置,以便代码不会直接“离开”页面吗?
答案1
这应该会有效。
\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}
输出:
如果你不想要红色箭头,
\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}