[Tex/LaTex] Removing prefix from autoref output

autorefcross-referencinghyperref

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:

Depiction of the output for the above code

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., as

I will now reference \autoref{sec:foo}, \ref{sec:bar}, and \ref{sec:baz}

you will get

I will now reference section 1, 2, and 3

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 option nameinlink and use \cref, the package's main user-level macro. Importantly, \cref can take multiple arguments. If the you write

I will now reference \cref{sec:foo,sec:bar,sec:baz}

you'll get

I will now reference sections 1 to 3

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, write

I will now reference \cref{sec:foo,,sec:bar,sec:baz}

and you'll get

I will now reference sections 1, 2 and 3

with the strings "sections 1", "2", and "3" acting as hyperlink targets.


A full MWE (minimum working example):

Rendered output of the above.

(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.

\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\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

\bigskip
I will now reference \autoref{sec:foo}, \autoref{sec:bar}, and \autoref{sec:baz}

I will now reference \autoref{sec:foo}, \ref{sec:bar}, and \ref{sec:baz}

I will now reference \cref{sec:foo,,sec:bar,sec:baz}

I will now reference \cref{sec:foo,sec:bar,sec:baz}

\end{document}
Related Question