3

I am forced would like to typeset the first \autoref to any table or figure in boldface and all subsequent references in normalfont. How can I achieve this, without having to manually insert \textbf at the first occurrence as shown below?

\documentclass{article}

\usepackage{hyperref}

\begin{document}

The first reference to~\textbf{\autoref{tab:a}} should be bold.

\begin{table}
\caption{A table}\label{tab:a}
\end{table}

Further references to~\autoref{tab:a} should be normal.

\end{document}

I found a solution for a very similar problem here, but it works for \ref rather than \autoref.

4

The usual procedure is to add a check for an already defined macro storing in its name the current label; if it's still undefined, make the \autoref bold and define the macro.

Since we need to overload \autoref, we need some care.

\documentclass{article}
\usepackage{letltxmacro}
\usepackage[colorlinks]{hyperref}

\AtBeginDocument{%
  \LetLtxMacro\hyperrefautoref\autoref
  \LetLtxMacro\autoref\firstboldautoref
}
\makeatletter
\DeclareRobustCommand\firstboldautoref{\@firstboldautoref}
\def\@firstboldautoref#1#{%
  \def\fb@autoref@star{#1}%
  \fb@autoref
}
\def\fb@autoref#1{%
  \ifcsname boldautoref@#1\endcsname
    \expandafter\hyperrefautoref\fb@autoref@star{#1}%
  \else
    \global\expandafter\let\csname boldautoref@#1\endcsname\@empty
    \textbf{\expandafter\hyperrefautoref\fb@autoref@star{#1}}%
  \fi
}
\makeatother


\begin{document}

\section{Test}\label{sec:a}

The first reference to~\autoref*{sec:a} should be bold.

The first reference to~\autoref{tab:a} should be bold.

\begin{table}
\caption{A table}\label{tab:a}
\end{table}

Further references to~\autoref*{sec:a} should be normal.

Further references to~\autoref{tab:a} should be normal.

\end{document}

enter image description here

4

A small 'overload' of \autoref (not fully finished), adding the label to an expl3 \seq list and checking whether it was added already.

\documentclass{article}

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

\newcommand{\autorefformat}[1]{%
  {\bfseries#1}%
}

\ExplSyntaxOn
\seq_new:N \g_ref_alreadyused_seq% Define a sequence list

\makeatletter
\AtBeginDocument{
  \LetLtxMacro\autoref@@orig\autoref% Get the old definition of \autoref
  \RenewDocumentCommand{\autoref}{sm}{%
    \seq_if_in:NnTF \g_ref_alreadyused_seq {#2} {
      \IfBooleanTF{#1}{% Yes, starred version
        \autoref@@orig*{#2}
      }{
        \autoref@@orig{#2}
      }
    }{% No, it's not yet used, apply \autorefformat
      \seq_gput_right:Nn \g_ref_alreadyused_seq {#2}
      \autorefformat{%
      \IfBooleanTF{#1}{% Starred version
        \autoref@@orig*{#2}
      }{
        \autoref@@orig{#2}
      }
      }
    }% End of \seq_if_in:F 
  }
}
\makeatother
\ExplSyntaxOff

\begin{document}

The first reference to~\textbf{\autoref{tab:a}} should be bold.

\begin{table}
\caption{A table}\label{tab:a}
\end{table}

Further references to~\autoref{tab:a} should be normal.

\end{document}

Alternate version: Using a wrapper macro named,say, \autorefext, that checks similarly for the label being used already. The third, optional argument defaults to \bfseries.

\documentclass{article}

\usepackage{hyperref}
\usepackage{xparse}

\newcommand{\autorefformat}[1]{%
  {\bfseries#1}%
}

\ExplSyntaxOn
\seq_new:N \g_ref_alreadyused_seq

\NewDocumentCommand{\autorefext}{smO{\bfseries}}{%
  \seq_if_in:NnTF \g_ref_alreadyused_seq {#2} {
    \IfBooleanTF{#1}{
      \autoref*{#2}
    }{
      \autoref{#2}
    }
  }{%
    \seq_gput_right:Nn \g_ref_alreadyused_seq {#2}
    {#3%
      \IfBooleanTF{#1}{
        \autoref*{#2}
      }{
        \autoref{#2}
      }
    }
  }
}

\ExplSyntaxOff

\begin{document}

The first reference to~\autorefext{tab:a} should be bold.

\begin{table}
\caption{A table}\label{tab:a}
\end{table}

Further references to~\autorefext{tab:a} should be normal.

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.