29

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
  • Why would you expect an upper case "L" when defining \lstnumberautorefname with a lower case "l"? – Gonzalo Medina Nov 8 '11 at 19:56
  • @GonzaloMedina: True, I guess "capitalization" should refer to "auto capitalization". – Werner Nov 8 '11 at 20:00
  • @Werner: As far as I know, \autoref doesn't do "auto capitalization". – Gonzalo Medina Nov 8 '11 at 20:03
  • Yes, that's true. You have to manually alter it to accommodate this, which almost defeats the purpose of "auto"-\ref-ing it. – Werner Nov 8 '11 at 20:05
  • Well, this may be a rather awkward solution, but would it not be possible to create an \Autoref macro, which will locally redefine all of the \somethingautorefnames to a capitalized version? (Or, alternatively, just the one for the autoref target?) – einpoklum - reinstate Monica Jan 6 '12 at 9:25
28

\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}
11

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}​
9

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

  • 7
    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? – Klaus Kopec 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 tex.stackexchange.com/questions/121865/…. tex.stackexchange.com/questions/4386/… is based on the \newcommand command, not on \def. – Vser Oct 16 '14 at 8:49
  • 1
    @KlausKopec Have you meanwhile found a fix for the problem you describe? – sixtyfive Aug 3 '15 at 22:00
  • @RainerVerteidiger I have not resolved this. Sorry that I cannot be of more help. – Klaus Kopec Oct 27 '15 at 7:39
4

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

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.

  • 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
  • 1
    @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

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.