I assume you're using a class that includes the \chapter
command (e.g. book or report). In that case, within the appendix \autoref
will refer to chapters as "Appendix", but to sections still as "section" (contrary to the article class, where sections would constitute an "Appendix"). Solution: (EDIT: My first "solution" was very ill-advised) Load cleveref
after hyperref
and use \cref
instead of \autoref
. (Yes, the links won't be as pretty as before.)
\documentclass{report}
\usepackage{hyperref}
\usepackage{cleveref}% Load AFTER hyperref!
% \crefname{subappendix}{fairy tale}{fairy tales}
\begin{document}
\chapter{bla}\label{bla}
\section{blubb}\label{blubb}
As shown in \cref{foo} and especially in \cref{bar} \dots
\appendix
\chapter{foo}\label{foo}
\section{bar}\label{bar}
As shown in \cref{bla} and especially in \cref{blubb} \dots
\end{document}
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{%
linkcolor=\csname\ar@counter autorefcolor\endcsname,%
linkbordercolor=\csname\ar@counter autorefcolor\endcsname
}%
\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}
\begin{equation}
E=mc^2\label{eq}
\end{equation}
\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}
![Result](https://i.stack.imgur.com/PGQNq.png)
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
\hypersetup{linkbordercolor=violet}%
\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.
Best Answer
If the
hyperref
package is loaded, use the "starred" versions of various cross-referencing macros --\autoref*
,\ref*
, etc. -- if you don't want to create hyperlinks.