I want to include code from a file into my document. However, several distinct line ranges should appear in the same listing. Also, I want to use gobbling, and it would be nice to include some blank lines.
If I use the listings package, I can use the option linerange to include several line ranges from the same file. However, gobbling is not supported for file inputs, and it seems that single empty lines are somehow skipped in the output (line 5 in the examples below).
If I use minted, I can gobble and include empty lines. However, I can only use one line range per \inputminted command. That would be fine (because I use different gobble distances for different line ranges), but there is this nasty gap between the different ranges. I can use vspace to make it disappear, but that does not always work (e.g., if the page is stretched for some reason). It would be nice to have something like this, which would recognize that these file parts should be shown as continuous:
\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}
Here is my minimal working example:
\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}

\pythonfilecommands and a negativevspacein between, but that seems a little dirty. – Jenny Apr 17 '15 at 15:07\lstinputlisting, see http://tex.stackexchange.com/questions/151208/how-to-remove-the-leading-unnecessary-white-spaces-when-using-linerange-option/164610#164610 – jub0bs Apr 17 '15 at 15:34