.png)
ファイルのコードをドキュメントに含めたいのですが、同じリストに複数の異なる行範囲が表示される必要があります。また、gobbling を使用したいので、空白行をいくつか含めると便利です。
パッケージを使用する場合listings
、同じファイルから複数の行範囲を含めるオプションを使用できますlinerange
。ただし、ファイル入力では gobbling はサポートされておらず、出力では単一の空行が何らかの理由でスキップされるようです (以下の例の 5 行目)。
を使用するとminted
、空行を取り込み、含めることができます。ただし、\inputminted
コマンドごとに使用できる行範囲は 1 つだけです。これは問題ありません (行範囲ごとに異なる取り込み距離を使用するため) が、異なる範囲の間には厄介なギャップがあります。 を使用してこのギャップを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}