.png)
Quiero incluir código de un archivo en mi documento. Sin embargo, deberían aparecer varios rangos de líneas distintos en el mismo listado. Además, quiero utilizar engullir y sería bueno incluir algunas líneas en blanco.
Si uso el listings
paquete, puedo usar la opción linerange
para incluir varios rangos de líneas del mismo archivo. Sin embargo, la deglución no es compatible con las entradas de archivos, y parece que de alguna manera se omiten líneas vacías individuales en la salida (línea 5 en los ejemplos siguientes).
Si uso minted
, puedo engullir e incluir líneas vacías. Sin embargo, sólo puedo usar un rango de líneas por \inputminted
comando. Eso estaría bien (porque uso diferentes distancias de trago para diferentes rangos de línea), pero existe una desagradable brecha entre los diferentes rangos. Puedo utilizarlo vspace
para hacerlo desaparecer, pero eso no siempre funciona (por ejemplo, si la página se estira por algún motivo). Sería bueno tener algo como esto, que reconociera que estas partes del archivo deberían mostrarse como continuas:
\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}
Aquí está mi ejemplo de trabajo mínimo:
\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}