# \autoref with links that include only the number, but not the text, and have counter-dependent colors

I would like the functionality of hyperref's \autoref, but in a way that the link border box covers only the number and not the preceding text (e.g. "section"). This is normally possible with the package cleveref (so this would normally answer my question already), but as an added twist, I'd like the link color to be dependent on the type of counter, say different shades of green for chapters, sections, and subsections, and different shades of purple for my tables and figures. Here is a minimal, self-explanatory example:

\documentclass{memoir}
\setsecnumdepth{subsubsection}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{hyperref}
\renewcommand*{\tableautorefname}{table}
\renewcommand*{\figureautorefname}{figure}

\begin{document}

\chapter{The only chapter}
\label{theonlychapter}

Text.

\begin{table}
\caption{The only table\label{theonlytable}}
\centering
\begin{tabular}{lll}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
\end{table}

\begin{figure}
\caption{The only figure\label{theonlyfigure}}
\centering
A figure.
\end{figure}

\section{The only section}
\label{theonlysection}

Text.

\subsection{The only subsection}
\label{theonlysubsection}

Text.

See \autoref{theonlychapter}. See \autoref{theonlysection}. See \autoref{theonlysubsection}. See \autoref{theonlytable}. See \autoref{theonlyfigure}.

colors: \textcolor{PineGreen}{chapter}, \textcolor{ForestGreen}{section}, \textcolor{LimeGreen}{subsection}, \textcolor{Violet}{table}, \textcolor{Mulberry}{figure}

\end{document}


Whether the solution uses hyperref or cleveref doesn't matter to me, but I need to load hyperref in my document independently.

• Are you sure that your readers will understand the meaning of so many colors? – egreg Mar 4 '13 at 23:54
• "I disagree with your preferences" comments are quite popular, huh ;-) To answer: To some extent this is a "proof of concept" question: how can one do this? (But, reducing the color count by eg making all text section references have the same color would be an option.) The other aspect is that many colors probably don't hurt either: some syntax highlighting schemes use a lot of different styles, and the reader won't die from one color or style too many :-) I do think that too much italicization for instance can be distracting and make things less effective; link border color is unobtrusive. – Lover of Structure Mar 5 '13 at 0:07

A solution with hyperref. Here, the link references are colored and the link borders are invisible. For the link references to be black but the link borders to be colored (as requested in the original question), simply remove the colorlinks option of package hyperref (e.g. by adding a % before colorlinks).

\documentclass{memoir}
\setsecnumdepth{subsubsection}
\usepackage{xparse}
% for \RenewDocumentCommand and \IfValueTF (for handling the optional argument)
\usepackage{xifthen} % for \isempty (testing whether the optional argument is empty)
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[
colorlinks % comment this line out for colored boxes around the link numbers
]{hyperref}

\usepackage{letltxmacro}
\usepackage{refcount}

\makeatletter
\LetLtxMacro\org@autoref\autoref % "org" stands for "original"
\RenewDocumentCommand{\autoref}{o m}{%
% test 1: reference defined?
\@ifundefined{r@#2}{%
\org@autoref{#2}%
}{%
\begingroup
\def\ar@next{\org@autoref{#2}}%
% test 2: anchor available?
\edef\ar@anchor{\getrefbykeydefault{#2}{anchor}{}}%
\ifx\ar@anchor\@empty
\else
% test 3: counter part extractable from anchor name?
\expandafter\ar@getcounter\ar@anchor.\@nil
\ifx\ar@counter\ar@dot
\else
% test 4: color defined?
\@ifundefined{\ar@counter autorefcolor}{%
\def\ar@next{\IfValueTF{#1}{%
\ifthenelse{\isempty{#1}}{\ref{#2}}{#1~\ref{#2}}%
}{\ar@autoref{#2}}}%
}{%
\def\ar@next{%
\hypersetup{%
}%
\IfValueTF{#1}{%
\ifthenelse{\isempty{#1}}{\ref{#2}}{#1~\ref{#2}}%
}{\ar@autoref{#2}}%
}%
}%
\fi
\fi
\ar@next
\endgroup
}%
}
\def\ar@getcounter#1.#2\@nil{%
\def\ar@counter{#1}%
}
\newcommand*{\ar@dot}{.}
\newcommand*{\ar@autoref}[1]{%
\@ifundefined{\ar@counter autorefname}{%
\@ifundefined{\ar@counter name}{%
}{%
\@nameuse{\ar@counter name}~%
}%
}{%
\@nameuse{\ar@counter autorefname}~%
}%
\ref{#1}%
}
\newcommand*{\setautorefcolor}[2]{%
\@namedef{#1autorefcolor}{#2}%
}
\makeatother

\setautorefcolor{chapter}{PineGreen}
\setautorefcolor{section}{ForestGreen}
\setautorefcolor{subsection}{LimeGreen}
\setautorefcolor{table}{violet}
\setautorefcolor{figure}{Mulberry}

\renewcommand*{\tableautorefname}{table}
\renewcommand*{\figureautorefname}{figure}

\begin{document}

\chapter{The only chapter}
\label{theonlychapter}

Text.

\begin{table}
\caption{The only table\label{theonlytable}}
\centering
\begin{tabular}{lll}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
\end{table}

\begin{figure}
\caption{The only figure\label{theonlyfigure}}
\centering
A figure.
\end{figure}

$$E=mc^2\label{eq}$$

\section{The only section}
\label{theonlysection}

Text.

\subsection{The only subsection}
\label{theonlysubsection}

{
\parindent0pt % disablement of paragraph indentation
\setlength{\parskip}{\baselineskip} % empty lines between paragraphs

Text.

See \autoref{theonlychapter}. See \autoref{theonlysection}. See
\autoref{theonlysubsection}.\\
See \autoref{theonlytable}. See \autoref{theonlyfigure}.
See \autoref{eq}.

See \autoref[customtext]{theonlychapter}. See \autoref[customtext]{theonlysection}. See
\autoref[customtext]{theonlysubsection}.\\
See \autoref[customtext]{theonlytable}. See \autoref[customtext]{theonlyfigure}.
See \autoref[customtext]{eq}.

See \autoref[parts]{theonlysection} and especially \autoref[]{theonlysubsection} of
this text.

colors: \textcolor{PineGreen}{chapter}, \textcolor{ForestGreen}{section},
\textcolor{LimeGreen}{subsection}, \textcolor{Violet}{table},
\textcolor{Mulberry}{figure}, \textcolor{red}{equation}.

}

\end{document}


## Modifying the counter descriptor

The original \autoref does not support capitalization or plural forms. Two solutions are given here:

## 1. A manual workaround

The color settings can be specified inside a group, e.g.:

\documentclass{memoir}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{hyperref}

\newcommand*{\tableref}[1]{%
\begingroup
\ref{#1}%
\endgroup
}

\begin{document}
Tables \tableref{one} and \tableref{two}.

\begin{table}
\caption{First caption}\label{one}
\caption{Second caption}\label{two}
\end{table}
\end{document}


## 2. Building this into \autoref

The \autoref command (as defined above) accepts an optional argument specifying the link text directly. Its syntax is \autoref[customtext]{label}. It can be used as follows: see \autoref[parts]{theonlysection} and especially \autoref[]{theonlysubsection} of this text: if the optional argument is empty ([]), no ~ will be inserted before the textless reference number. (We assume that there will already be an ordinary space before it. Otherwise we can still use it like this~\autoref[]{label}.)

The second approach is necessary if the descriptor is generic and the referred-to entity can change categories (between for example "section", "subsection", and "subsubsection"). Here, \autoref with an optional argument will choose the color of the text or link border automatically, where an approach using \sectionref, \subsectionref, or \subsubsectionref would require the TeX user to choose between the three in the source code.

• I approve of the fact that your code colors the link text instead of drawing an unsightly box around it. I'll never understand why those boxes are the hyperref default --- they're like underlines, but four times as ugly. – Esteis Mar 8 '13 at 22:16
• @Esteis I haven't tried how the boxes print, but I think the intention of the default settings is to have ordinary black text (which is printable), with the color being relegated elsewhere. How unobtrusive the boxes are is a matter of taste. That said, your opinion is likely not a fringe opinion, so it's good that you've stated it. – Lover of Structure Mar 12 '13 at 11:53