我被迫想要将\autoref
任何表格或图形的第一个部分排版为粗体,并将所有后续引用排版为普通字体。如何才能实现这一点,而不必\textbf
像下面这样在第一次出现时手动插入?
\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}
我找到了一个非常相似的问题的解决方案这里,但它适用于\ref
而不是\autoref
。
答案1
通常的程序是添加一个检查,以检查已定义的宏是否存储在其名称中当前标签;如果它仍然未定义,则将其\autoref
加粗并定义该宏。
既然我们需要超负荷工作\autoref
,我们就需要小心。
\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}
答案2
一个小的“过载” \autoref
(尚未完全完成),将标签添加到expl3
\seq
列表中并检查它是否已被添加。
\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}
替代版本:使用名为 的包装器宏\autorefext
,类似地检查已使用的标签。第三个可选参数默认为\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}