83

I've always used \ref for referring to labelled items, but a LaTeX template I downloaded uses \autoref instead. What is the difference?

Ludovic C.
  • 8,888
Ref
  • 831
  • 1
  • 6
  • 3

2 Answers2

96

\autoref is a command from the hyperref package which is used to have some automatic prefix added to the reference. For instance if you used \label{sec:intro} just after your command for the introduction section the \autoref{sec:intro} command will output "section X" with X being the good number.

Of course you can imagine that the prefix depends on the type of the object to which the label is linked. If it is a figure, it will output "figure" and so on.

Note that you can redefine the prefix for the autoref command associated to a given type of object. For instance for the sections use:

\def\sectionautorefname{New prefix for the sections}

More generally,

\def\<type>autorefname{<new name>}
Ludovic C.
  • 8,888
79

The difference is easy to see in the following example:

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\begin{table}\centering
\caption{foo}\label{foo}
\tabular{c}
foo\\\hline
bar\\\hline
\endtabular
\end{table}
\begin{table}\centering
\caption{bar}\label{bar}
\tabular{c}
foo\\\hline
bar\\\hline
\endtabular
\end{table}

See Table~\ref{foo} and 
          \autoref{bar}
\end{document}

enter image description here