# Listings - Hide range labels and suffix/prefix from code

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},
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.

First of all you should make sure that the range delimiters span the whole line, otherwise the range detection might not be correct. So if all your range comments look like

#:~<marker>~:


you should define

rangeprefix = \#:~,
rangesuffix = ~:


For hiding the marker comments in your full code listings, I see two options.

If you want to just hide the marker comment but keep the line in the listing, you could define a new delimiter that renders the comment in white (or the background) color:

moredelim = [is][\color{white}]{\#:~}{~:}


If you want to remove the corresponding lines completely, you could define a new, invisible comment type which is ended by the end-of-line character ^^M, thus removing the whole line from the output:

morecomment = [is]{\#:~}{\^^M}


Note that the latter makes listings literally not see the line, which has consequences on the line numbering.

Full example:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.py}
import numpy as np

x = np.linspace(0,10,num=100)
#:~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')
\end{filecontents*}

\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},
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,
]{\jobname.py}

\subsection{Full Code, delimiters hidden}
\lstinputlisting[
moredelim={[is][\color{white}]{\#:~}{~:}}
]{\jobname.py}

\subsection{Full Code, delimiter lines removed}
\lstinputlisting[
morecomment={[is]{\#:~}{\^^M}}
]{\jobname.py}

\end{document}


outputs

• Thank you for posting some solutions! Now I'm intrigued by using the morecomment settings to hit block comments (like help/doc I write within my code), while retaining the line numbers. It appears that the lines are indeed excluded when I input a full listing of code, but for an excerpt with say a 3-line comment block above, the lines numbers are inconsistent and off by 3 lines when compared to the full block. Do you have any suggestions for this? – likethevegetable Oct 22 '19 at 16:23
• Not sure I understand the problem correctly. Make sure that your range delimiter comments and your normal comments have different prefixes, such that listings is able to distinguish between them. Perhaps update your question with a new example document (or open a new one if this is a different problem). – siracusa Oct 23 '19 at 6:30