59

I have defined the following

\providecommand*{\lstnumberautorefname}{line}

and within my text

\begin{lstlisting} 
            |\label{sdd_1}|<sqlCallStatistics>YES</sqlCallStatistics>
\end{lstlisting}

When I now use in my text

xxx. \autoref{sdd_1} 

then I receive the following output :

xxxx. line 1 

instead of (what I had expected)

xxxx. Line 1
lockstep
  • 250,273
user9131
  • 875

6 Answers6

52

\autoref doesn't do automatic capitalization, as the following example illustrates:

\documentclass{article}
\usepackage{listings}
\usepackage{hyperref}

\providecommand*{\lstnumberautorefname}{line}

\begin{document}

\section{Test}\label{sec:test} \autoref{sec:test} and a period before the reference. \autoref{sec:test} \begin{lstlisting} [escapeinside=||,numbers=left] <sqlCallStatistics>YES</sqlCallStatistics>|\label{sdd_1}| \end{lstlisting}

\autoref{sdd_1} and a period before the reference. \autoref{sdd_1}

\end{document}

You could use the cleveref package to have \cref (for lower case) and \Cref (for upper case):

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

\begin{document}

\begin{lstlisting} [escapeinside=||,numbers=left] <sqlCallStatistics>YES</sqlCallStatistics>|\label{sdd_1}| \end{lstlisting}

\cref{sdd_1} and a period before the reference. \Cref{sdd_1}

\end{document}

Edit: As MWin123 commented: By default \cref turns only the number "1" into a hyperlink, while \autoref makes the whole "line 1" a clickable link. Fortunately there's an option nameinlink for it, i.e., you can use \usepackage[nameinlink]{cleveref}

foobar
  • 153
Gonzalo Medina
  • 505,128
  • 16
    By default \cref turns only the number "1" into a hyperlink, while \autoref makes the whole "line 1" a clickable link. Fortunately there's an option nameinlink for it, i.e., you can use \usepackage[nameinlink]{cleveref}. – MWin123 Nov 23 '20 at 06:04
20

There is no easy way around this, without going as far as to redefine \lstnumberautorefname to Line or including additional packages. To that extent, you might just as well be better off using

\hyperref[sdd_1]{Line~\ref*{sdd_1}}

without having to resort to the other methods. This will provide the same linkage, but capitalized. \ref* works just like \ref but does not include a hyperlink. The hyperlink is provided by \hyperref[<label>]{<stuff>}. Here is a minimal example:

enter image description here

\documentclass{article} 
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{numbers=left,escapeinside=||}
\providecommand*{\lstnumberautorefname}{line}
\begin{document}
\begin{lstlisting} 
|\label{sdd_1}|<sqlCallStatistics>YES</sqlCallStatistics>
\end{lstlisting}
Here is some text. \hyperref[sdd_1]{Line~\ref*{sdd_1}} is important, and so is \autoref{sdd_1}.
\end{document}​
Werner
  • 603,163
  • It's sad that autoref cannot take start of lines into account (possibly by taking an extra parameter for an arbitrary string to insert, if not possible to work automatically), but your hyperref trick works like a charm. Thank you. – gustafbstrom Apr 26 '22 at 08:04
17

Another option is mentioned in the manual.

Use:

\usepackage[english]{babel}  
\addto\extrasenglish{  
  \def\typeautorefname{Type}  
}
\usepackage{hyperref}   

If you want to capitalize usage of \autoref or want to completely rename the auto-reference name. Just replace type with the type of reference, so chapter, section, subsection, etc..

E.g.:

\usepackage[english]{babel}
\addto\extrasenglish{
  \def\subsectionautorefname{Subsection}
}

I realize this does not meet your requirement of defining a \Autoref command. However, I thought it would be worth mentioning since it's a cleaner version, might fulfill someone-else's requirements and it took me a while to find something that actually works.

Byebye
  • 280
  • I think OP wants "Line" at the beginning of a sentence, but "line" elsewhere. Just (re)defining (type)autorefname would make it always capitalized. – Teepeemm Nov 20 '18 at 14:11
  • 4
    @Teepeemm I assumed the same. Since OP specified the desire for a \Autoref command. However, I thought it would still be worth mentioning. – Byebye Nov 21 '18 at 15:25
  • 1
    You're both correct. This is not exactly what the OP has asked for. Yet, for other people stambling across this thread (e.g. me) it might be useful. I therefore see it as a constructive contribution to the thread. – Ido_f Jun 03 '21 at 10:22
14

Here is a way to get \Autoref from hyperref package. Heiko Oberdiek (the maintainer of the package) will provide a package solution in due course.

\documentclass{article}
\usepackage{catoptions}
\makeatletter
\def\figureautorefname{figure}
\def\tableautorefname{table}
\def\Autoref#1{%
  \begingroup
  \edef\reserved@a{\cpttrimspaces{#1}}%
  \ifcsndefTF{r@#1}{%
    \xaftercsname{\expandafter\testreftype\@fourthoffive}
      {r@\reserved@a}.\\{#1}%
  }{%
    \ref{#1}%
  }%
  \endgroup
}
\def\testreftype#1.#2\\#3{%
  \ifcsndefTF{#1autorefname}{%
    \def\reserved@a##1##2\@nil{%
      \uppercase{\def\ref@name{##1}}%
      \csn@edef{#1autorefname}{\ref@name##2}%
      \autoref{#3}%
    }%
    \reserved@a#1\@nil
  }{%
    \autoref{#3}%
  }%
}
\makeatother

% Example:
\usepackage{booktabs,caption,xcolor}
\usepackage[colorlinks]{hyperref}

\begin{document}
\def\withop#1#2{With \texttt{\string#1}: #1{#2}\par}
\section{Test section}
\parindent0pt
\label{xx}
\withop\autoref{xx}
\withop\Autoref{xx}

\subsection{Test subsection}
\label{yy}
\withop\autoref{yy}
\withop\Autoref{yy}
\withop\autoref{tab:1}
\withop\Autoref{tab:1}
\withop\autoref{fig:1}
\withop\Autoref{fig:1}

\begin{table}[!h]
\centering
\caption{Sample table}\label{tab:1}
\begin{tabular}{l|l}
...&...
\end{tabular}
\end{table}

\begin{figure}[!h]
...
\end{figure}

\end{document} 

enter image description here

Ahmed Musa
  • 11,742
  • 8
    As of fall 2013 the current hyperref version is from 2012-11-06 (6.83m) and does not feature an \Autoref AFAICS. Where did he say to provide one in the future or did I understand you wrong? – stefanct Dec 27 '13 at 21:47
  • 1
    This works well with the default autoref names, but I have problems with custom names. Example: adding \def\subsectionautorefname{myprettyname} after the \begin{document} line will make the lower case autoref show myprettyname but the upper case Autoref Subsection. Is there an easy fix? – KlausenK Apr 24 '14 at 12:05
  • @ahmed-musa Is there an easy way to define a starred version of this macro to use it in the command defined in this question https://tex.stackexchange.com/questions/121865/nameref-how-to-display-section-name-and-its-number. https://tex.stackexchange.com/questions/4386/defining-starred-versions-of-commands-macro is based on the \newcommand command, not on \def. – Vser Oct 16 '14 at 08:49
  • 1
    @KlausKopec Have you meanwhile found a fix for the problem you describe? – Sixtyfive Aug 03 '15 at 22:00
  • @RainerVerteidiger I have not resolved this. Sorry that I cannot be of more help. – KlausenK Oct 27 '15 at 07:39
  • Does not appear to work any more ("Extra \endgroup" error message). – phk Jan 09 '22 at 13:53
2

A simple hacky way is to add the following

\renewcommand{\figureautorefname}{Figure}
\renewcommand{\tableautorefname}{Table}
\renewcommand{\partautorefname}{Part}
\renewcommand{\appendixautorefname}{Appendix}
\renewcommand{\chapterautorefname}{Chapter}
\renewcommand{\sectionautorefname}{Section}
\renewcommand{\subsectionautorefname}{Section}
\renewcommand{\subsubsectionautorefname}{Section}

Right before your

\begin{document}
KaO
  • 121
-2

The quickest way that I found, without replace by \cref in the entire document (as it was shown and suggested in previous answers) was to implement the following lines below of \usepackage{hyperref} to capitalize the chapter word:

\usepackage[english]{babel}
\usepackage{hyperref}
\addto\extrasenglish{
    \def\chapterautorefname{Chapter}
}

It's mandatory to include \usepackage[english]{babel} if you don't have it. I found this information on https://github.com/Cryptosaurus/iacrtrans/issues/9

aldajo92
  • 101