# Cleveref with hyperref uses wrong label when referring to line in code listing

When I use cleveref to reference a line within a lstinputlisting: \Cref{testtest}

Latex writes "section ..." instead of "line ...". I already defined "Listing"/"Listings" for the Listing's label, can I do something similar for line references within a listing?

Here's a minimal working example
(taken from https://tex.stackexchange.com/a/338137/107626):

  \begin{filecontents*}{\jobname.py}
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
attack_module = importlib.import_module(attack_name)  (*\label{testtest}*)
attack_class = getattr(attack_module, attack_name)
\end{filecontents*}

\documentclass{article}
\usepackage{listings}
\usepackage{cleveref}

% Adds Cleveref support for Listing
\crefname{lstlisting}{listing}{listings}
\Crefname{lstlisting}{Listing}{Listings}

\lstset{
basicstyle=\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
frame=single,
% start delimiter: (*, end delimiter: *)
escapeinside={(*}{*)},
captionpos=b,
aboveskip=15pt,
belowskip=-0.5 \baselineskip,
breaklines=true,
}

\begin{document}

\begin{lstlisting}[language=python]
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
attack_module = importlib.import_module(attack_name) (*\label{test}*)
attack_class = getattr(attack_module, attack_name)
\end{lstlisting}

\lstinputlisting[
label=lst:my_code_sample,
caption={My caption.},
language=python
]{\jobname.py}

% Ideally LaTeX should output: As depicted in line xx.
% Instead it is written: As depicted in section xx.
As depicted in \cref{test}.

\end{document}

• I am getting "line 6" i.stack.imgur.com/BNPbT.png. How are you compiling? I used pdflatex from texlive 2016 – samcarter_is_at_topanswers.xyz Nov 8 '16 at 11:41
• I am using TeXmaker 4.5 with pdfTeX, Version 3.14159265-2.6-1.40.16 (MiKTeX 2.9) (preloaded format=pdflatex 2016.10.19). – Patrick Nov 8 '16 at 11:44
• mmm, sounds similar. Have you tried to remove all auxillary files and compile the above MWE from a clean start? – samcarter_is_at_topanswers.xyz Nov 8 '16 at 11:47
• @samcarter Yes, I tried it and the MWE is working fine. Sorry, I did no check that before. As this is not exactly the document I am working on, It looks like it has something to do with the used template, see sharelatex.com/project/5821bc3e34d2a43b682a6eae Any idea how I can fix this? – Patrick Nov 8 '16 at 11:56

I hate questions with MWE not showing the problem. This wastes the time and joy of people trying to help!!!

The great crystal ball of things not mentioned in the MWE told me, that you are a) using the hyperref package and b) not loading the packages in the order given in the MWE, but this order is important, it works with

\usepackage{listings}
\usepackage{hyperref}
\usepackage{cleveref}


Full MWE:

  \begin{filecontents*}{\jobname.py}
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
attack_module = importlib.import_module(attack_name)  (*\label{testtest}*)
attack_class = getattr(attack_module, attack_name)
\end{filecontents*}

\documentclass{article}

\usepackage{listings}
\usepackage{hyperref}
\usepackage{cleveref}

% Adds Cleveref support for Listing
\crefname{lstlisting}{listing}{listings}
\Crefname{lstlisting}{Listing}{Listings}

\lstset{
basicstyle=\ttfamily,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
frame=single,
% start delimiter: (*, end delimiter: *)
escapeinside={(*}{*)},
captionpos=b,
aboveskip=15pt,
belowskip=-0.5 \baselineskip,
breaklines=true,
}

\begin{document}

\begin{lstlisting}[language=python]
def do_something(self, attack_name: str):
"""
A short description of the method comes here.
"""
attack_module = importlib.import_module(attack_name) (*\label{test}*)
attack_class = getattr(attack_module, attack_name)
\end{lstlisting}

\lstinputlisting[
label=lst:my_code_sample,
caption={My caption.},
language=python
]{\jobname.py}

As depicted in \cref{test}.

\end{document}


• Dear Sam, I really apologize for the confusion and for wasting your time but I did not pre-check that the MWE does encounter the same problem. Further, I did not know that hyperref influences the use of listings/cleveref. Anyway, thanks a lot for your patience and support! – Patrick Apr 9 '17 at 0:15