7

This Link provides an answer to the question how to make line-number referencing work with lstlisting. However, if I use \autoref it will only return the number and not something like Line 31.

1
  • \autoref can't provide for any label/counter entity.
    – user31729
    Aug 10 '15 at 15:04
6

The \autoref command can't 'know' the ref name for each counter, this has to be provided as a separate macro:

I used the listings example by Marco Daniel provided in the Link by the O.P. and found that listings uses lstnumber as counter, so

\newcommand{\lstnumberautorefname}{Line}

is the relevant macro name to be defined (i.e. counter name + autorefname as suffix.

'Only' the standard counters such as chapter, etc, figure, page, table etc. are predefined. For a full list see the hyperref manual. Other packages might define additional \...autorefname macros.

Please have a look on the cleveref package too.

\documentclass{article}

\usepackage{listings}
\usepackage{hyperref}


\newcommand{\lstnumberautorefname}{Line}

\begin{document}




\lstset{escapeinside={(*@}{@*)}}
\begin{lstlisting}
 for i:=maxint to 0 do begin
   { comment }(*@\label{comment}@*) 
 end;
\end{lstlisting}
\autoref{comment} shows a comment.


\end{document}

enter image description here

3
  • Exactly what I was looking for.
    – NOhs
    Aug 10 '15 at 15:10
  • The cleverref package looks great but at this point I am afraid to change my hyperref setup to a cleverref setup as I do not know how much work that will be.
    – NOhs
    Aug 10 '15 at 15:42
  • @MrZ: As long as you include cleveref later than hyperref everything should be alright anyway.
    – user31729
    Aug 10 '15 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.