# Autoref with optional argument

Something I have always wanted to do is use \autoref with an optional argument, similarly to the use that is often made of \cite: I would like to be able to write \autoref[comment]{label}, to obtain [link to label, comment] in the final pdf. Is there any way of doing this (specifically with autoref, and not the other referencing packages/commands)? Thank you for any help.

See three different versions below (due to change requests by the O.P.)

With xparse it is very easy to redefine some commands, provided the original version of \autoref is stored as well, this requires \LetLtxMacro from letltxmacro package.

Since \autoref has a starred version \autoref* this has to be captured as well.

\RenewDocumentCommand{\autoref}{som}{...}


redefines \autoref and providing a starred first argument, (s), an optional (o) 2nd argument (for the comment) and the mandatory (m) 3rd argument which is meant for the label name.

With \IfBooleanTF{#1}{}{} the calling of the starred version can be checked and with \IfValueTF{#2}{}{} the specification of an optional argument can be checked.

Please note that the comment is not wrapped in the hyperlink box/frame! If this is required, more has work to be done.

\documentclass{article}

\usepackage{hyperref}
\usepackage{letltxmacro}
\usepackage{xparse}

\AtBeginDocument{%
\LetLtxMacro\autoreforig\autoref
\RenewDocumentCommand{\autoref}{som}{%
\IfBooleanTF{#1}{%
\autoreforig*{#3}\IfValueT{#2}{#2}%
}{%
\autoreforig{#3}\IfValueT{#2}{#2}%
}%
}
}

\begin{document}

See \autoref[ is very nice]{section:foo} or \autoref*[ is nice too]{section:foobar}

\section{Foo section}\label{section:foo}

\section{Foo bar section}\label{section:foobar}

\end{document}


Update with square brackets

\documentclass{article}

\usepackage{hyperref}
\usepackage{letltxmacro}
\usepackage{xparse}

\AtBeginDocument{%
\LetLtxMacro\autoreforig\autoref
\RenewDocumentCommand{\autoref}{som}{%
[%
\IfBooleanTF{#1}{%
\autoreforig*{#3}%
}{%
\autoreforig{#3}%
}%
\IfValueT{#2}{,\space#2}]%
}
}

\begin{document}

See \autoref[is very nice]{section:foo} or \autoref*[is nice too]{section:foobar}

\section{Foo section}\label{section:foo}

\section{Foo bar section}\label{section:foobar}

\end{document}


Third installment:

\documentclass{article}

\usepackage{hyperref}
\usepackage{letltxmacro}
\usepackage{xparse}

\AtBeginDocument{%
\LetLtxMacro\autoreforig\autoref
\RenewDocumentCommand{\autoref}{som}{%
\IfValueT{#2}{[}%
\IfBooleanTF{#1}{%
\autoreforig*{#3}%
}{%
\autoreforig{#3}%
}%
\IfValueT{#2}{,\space#2]}%
}
}

\begin{document}

See \autoref[is very nice]{section:foo} or \autoref*[is nice too]{section:foobar}, but \autoref{section:foo} or \autoref*{section:foobar} are without comment.

\section{Foo section}\label{section:foo}

\section{Foo bar section}\label{section:foobar}

\end{document}

• Thank you for this, I wasn't familiar with letltxmacro or xparse. Are you able to include the square brackets, and possibly eliminate the need for the leading space in the comment? I.e. I would like \autoref[is very nice]{section:foo} to produce [section 1, is very nice]. – Emilio Ferrucci Mar 19 '18 at 17:23
• @EmilioFerrucci: The square brackets are no problem. But shall the link border wrap around them or only section 1 be the link? – user31729 Mar 19 '18 at 17:24
• Thank you for answering. I would just like "section 1" to be the link in [section 1, is very nice] (this would mimic the behaviour of \cite[Theorem 1]{reference}). – Emilio Ferrucci Mar 19 '18 at 17:27
• @EmilioFerrucci: See the update at the end of my post – user31729 Mar 19 '18 at 17:30
• Perfect, exactly what I was after. Thank you so much for taking the time. – Emilio Ferrucci Mar 19 '18 at 17:33