
我想將文件中的程式碼包含到我的文件中。然而,幾個不同的行範圍應該出現在同一個清單中。另外,我想使用吞噬,並且最好包含一些空行。
如果我使用該listings
包,我可以使用該選項linerange
來包含同一文件中的多個行範圍。但是,檔案輸入不支援吞噬,而且輸出中似乎會以某種方式跳過單一空白行(下面範例中的第 5 行)。
如果我使用minted
,我可以吞噬並包含空行。但是,每個命令只能使用一個行範圍\inputminted
。那很好(因為我對不同的線範圍使用不同的吞噬距離),但是不同範圍之間存在令人討厭的差距。我可以用來vspace
讓它消失,但這並不總是有效(例如,如果頁面因某種原因被拉伸)。最好有這樣的東西,它會識別這些文件部分應該顯示為連續的:
\begin{minted}
\pythonfile[firstline=5,lastline=7]{mymodule.py}
\pythonfile[firstline=16,lastline=17,firstnumber=last]{mymodule.py}
\pythonfile[gobble=4,firstline=22,lastline=23,firstnumber=last]{mymodule.py}
\end{minted}
這是我的最小工作範例:
\documentclass{article}
\usepackage{filecontents}
\usepackage{listings}
\usepackage{minted}
\begin{filecontents}{mymodule.py}
import numpy as np
def mymean(x, axis=0) :
''' Here I explain why I need my own mean function.
Suppose for instance that I want to use axis=0 as a default.
Keyword arguments:
axis -- the axis over which to compute the mean.
Return:
The mean.
'''
return np.mean(x, axis=axis)
def mystd(x) :
return np.std(x)
if __name__ == '__main__' :
x = np.random.randint(0,100,150)
print mymean(x)
print mystd(x)
\end{filecontents}
\newmintedfile{python}{frame=leftline,linenos}
\begin{document}
\section{Listings}
\lstinputlisting[frame=left,numbers=left,language=Python,linerange={5- 7,16-17,22-23}]{mymodule.py}
\section{Minted}
\pythonfile[firstline=5,lastline=7]{mymodule.py}
\pythonfile[firstline=16,lastline=17,firstnumber=last]{mymodule.py}
\pythonfile[gobble=4,firstline=22,lastline=23,firstnumber=last]{mymodule.py}
\end{document}