I have started using \autoref
to perform some referencing between sections inside LaTeX.
Consider the following MWE and its screenshot:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{Foo}
\label{sec:foo}
Some random text
\section{Bar}
\label{sec:bar}
Some more random text
\section{Baz}
\label{sec:baz}
More random text again \\
I will now reference \autoref{sec:foo},
\autoref{sec:bar}, and \autoref{sec:baz}
\end{document}
The output looks like:
While reading this it becomes very irritating to read the word section
repeated multiple times while referencing several sections separated by commas.
Is it possible to generate the last sentence as I will now reference 1, 2 and 3
, with the hyperlinks to the sections embedded "inside" the numbers as before?. Thus I specifically do not want that section
prefix before each number in the output.
Best Answer
If you rewrite the sentence with one
\autoref
and two\ref
statements, i.e., asyou will get
which isn't quite right as the word 'section' appears in singular form.
Instead, I suggest you load the cleveref package (after loading
hyperref
) with the optionnameinlink
and use\cref
, the package's main user-level macro. Importantly,\cref
can take multiple arguments. If the you writeyou'll get
with the strings "sections 1" and "3" highlighted as hyperlink targets. (If you don't want the word "sections" to be included in the hyperlink target, don't set the
nameinlink
option.) Note that\cref
has automatically performed compression of the range of items. If you don't want compression, writeand you'll get
with the strings "sections 1", "2", and "3" acting as hyperlink targets.
A full MWE (minimum working example):
(If you wanted to create a cross-reference of the form "1, 2 and 3" without the "sections" prefix, you could do so by writing
\labelcref{sec:foo,,sec:bar,sec:baz}
.)The effect of setting the
nameinlink
option is to make the cross-references look like those produced by\autoref
.