I'm using listings to generate some code boxes, and am taking excerpts using range labels with suffixes and prefixes. I'm able to hide the begging tag from the full code by escaping to LaTeX and using \phantom
, but when I try to escape to LaTeX with the end tag inside, it does not work..
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{cleveref}
\lstset{
language=Python,
basicstyle=\ttfamily\small,
aboveskip={1.0\baselineskip},
belowskip={1.0\baselineskip},
frame=tblr,
rulecolor=\color{gray},
keywordstyle=\color[rgb]{0.627,0.126,0.941},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{01,0,0},
numbers=left,
numberstyle=\footnotesize\color{gray},
captionpos=t,
escapeinside={\#`}{`},
}
\begin{document}
\section{Examples}
\subsection{Excerpt of Code}
\lstinputlisting[includerangemarker=false,
rangeprefix=:~,
rangesuffix=~:,
linerange=beg:def-end:def,
]{"ex.py"}
\subsection{Full Code}
\lstinputlisting[
]{"ex.py"}
\end{document}
With the Python code below
import numpy as np
x = np.linspace(0,10,num=100)
#`\phantom{:~beg:def~:}`
def y(x, a=1, b=1, c=1):
return a*x**2 + b*x + c
#:~end:def~:
print(y(x))
print('Finished')
but when I change line 7 to
#`\phantom{:~end:def~:}`
the functionality of the tag end:def stops working and the code excerpt displays the rest of the code, but starting at beg:def.