10

When we reference equations, we can use \eqref, and when we reference figures, we can use \fref. Then the reference for equation has ( ) and the word figure is inserted into the document locations. Is there a reference for chapters, sections, subsections, subsubsections, etc?

This way when I reference the section, I can just do \somethingref instead section~\ref?

lockstep
  • 250,273
dustin
  • 18,617
  • 23
  • 99
  • 204
  • 2
    Have you seen the cleveref package? – Gonzalo Medina Jun 14 '13 at 20:37
  • @GonzaloMedina From reading the documentation, I don't use fancyref or hyperref with cleveref. Is that correct? – dustin Jun 14 '13 at 20:42
  • You can use hyperref and cleveref; just load hyperref before cleveref. As for fancyref, why use it if cleveref does a much better work? – Gonzalo Medina Jun 14 '13 at 20:47
  • @GonzaloMedina last questions: so \cref{} treats everything in equation and displaymath environments as equations but what about align? If you want, you can make an answer with our previous comments and this one since I will be using cleveref now. How does \cref now it is in a section argument? Do I need to do \cref{sec:}? From my reading, I was under the impression the argument before the : wasn't needed in cleveref – dustin Jun 14 '13 at 20:52
  • Using cleveref you don't have to use any prefix. Simply use \label{text} and then \ref{text}. The package automatically detects the type of the referenced object and produces the correct string. – Gonzalo Medina Jun 14 '13 at 20:58

3 Answers3

23

I would suggest you cleveref as another option. While hyperref's \autoref is powerful, \cref (\Cref) from cleveref are more powerful and more easily customizable. Some comparisons:

\documentclass{book}
\usepackage[colorlinks=true]{hyperref}
\usepackage{cleveref}

\begin{document}

\chapter{First Chapter}\label{chapter}
\section{Some Section}\label{sectiona}
\section{Some Other Section}\label{sectionb}

\begin{figure}[!ht]
  \centering
  \rule{30mm}{20mm}
  \caption{Some Figure}\label{figure}
\end{figure}

Using \texttt{\textbackslash autoref}, we can refer to \autoref{chapter}, \autoref{sectiona}, and \autoref{figure}, but using a range fails:~\autoref{sectiona,sectionb}

Using \texttt{\textbackslash cleveref}, we can refer to \cref{chapter}, \cref{sectiona}, and \cref{figure}. \Cref{chapter}. \Cref{sectiona}. \Cref{figure}. A range has no problem now:~\cref{sectiona,sectionb}

\end{document}

enter image description here

You want the object name to be a part of the hyperlink? You can use the nameinlink option for cleveref:

\documentclass{book}
\usepackage[colorlinks=true]{hyperref}
\usepackage[nameinlink]{cleveref}

\begin{document}

\chapter{First Chapter}\label{chapter}
\section{Some Section}\label{sectiona}
\section{Some Other Section}\label{sectionb}

\begin{figure}[!ht]
  \centering
  \rule{30mm}{20mm}
  \caption{Some Figure}\label{figure}
\end{figure}

Using \texttt{\textbackslash autoref}, we can refer to \autoref{chapter}, \autoref{sectiona}, and \autoref{figure}, but using a range fails:~\autoref{sectiona,sectionb}

Using \texttt{\textbackslash cleveref}, we can refer to \cref{chapter}, \cref{sectiona}, and \cref{figure}. \Cref{chapter}. \Cref{sectiona}. \Cref{figure}. A range has no problem now:~\cref{sectiona,sectionb}

\end{document}

enter image description here

Want to change the default format for hyperlinks? Use \crefdefaultlabelformat:

\documentclass{book}
\usepackage[colorlinks=true]{hyperref}
\usepackage{cleveref}

\crefdefaultlabelformat{[#2#1#3]}

\begin{document}

\chapter{First Chapter}\label{chapter}
\section{Some Section}\label{sectiona}
\section{Some Other Section}\label{sectionb}

\begin{figure}[!ht]
  \centering
  \rule{30mm}{20mm}
  \caption{Some Figure}\label{figure}
\end{figure}

Using \texttt{\textbackslash autoref}, we can refer to \autoref{chapter}, \autoref{sectiona}, and \autoref{figure}, but using a range fails:~\autoref{sectiona,sectionb}

Using \texttt{\textbackslash cleveref}, we can refer to \cref{chapter}, \cref{sectiona}, and \cref{figure}. \Cref{chapter}. \Cref{sectiona}. \Cref{figure}. A range has no problem now:~\cref{sectiona,sectionb}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
5

How about \autoref from the hyperref package:

enter image description here

\documentclass{book}

\usepackage{hyperref}

\begin{document}

\chapter{First Chapter}\label{chapter}

\section{Some Section}\label{section}

\begin{figure}[h!]
  \centering
  \rule{30mm}{20mm}
  \caption{Some Figure}\label{figure}
\end{figure}

Using \texttt{\textbackslash autoref}, we can refer to \autoref{chapter}, \autoref{section},
and \autoref{figure}.

\end{document}

For user defined environments such as Lemma, Proposition, etc., just define

\<env_name>autorefname

where <env_name> is the name of the environment you want to refer to. For details, see this post.

Herr K.
  • 17,946
  • 4
  • 61
  • 118
1

(Near) anything what you want. For example: \def\somethingref#1{section~\ref{#1}}. But if you love sections, it can be \def\somethingref#1{My $\heartsuit$~section~\ref{#1}}. :-)