[Tex/LaTex] Bold subsection numbers with hyperref

amsarthyperrefsectioning

When I use the amsart document class and leave a subsection title empty, then the subsection number is bold. Minimal working example:

\documentclass{amsart}
\begin{document}
\section{Test}
\subsection{} Text
\end{document}

Strangely, when I include the hyperref package, subsection numbers do not get bolded any more. MWE:

\documentclass{amsart}
\usepackage{hyperref}
\begin{document}
\section{Test}
\subsection{} Text
\end{document}

Is there a way I can make subsection numbers appear in bold when using hyperref? I am afraid to break some of the functionality of hyperref if I redefine the \subsection command after loading hyperref.

Best Answer

The amsart class indeed has the feature that if the argument to \subsection is empty, then the number is printed boldface, while normally it is medium series.

I don't like this feature at all, personally. However, what happens is that the code in \@sect that does the job is (line numbers refer to amsart.cls)

1055     \@ifempty{#8}{%
1056       \ifnum #2=\tw@ \def\@secnumfont{\bfseries}\fi}{}%

but when hyperref is loaded, the argument #8 is

\Sectionformat{<title>}{2}

so it is not empty as far as \@ifempty is concerned.

Here's a patch for it.

\documentclass{amsart}

\usepackage{xpatch}
\usepackage{hyperref}

\makeatletter
% we need to patch the saved version of \@sect
\providecommand{\H@old@sect}{} % so this doesn't choke if hyperref is removed
\xpatchcmd{\H@old@sect}
  {\@ifnotempty{#8}}
  {\checkempty@title{#8}{}}
  {}{}
\xpatchcmd{\H@old@sect}
  {\@ifempty{#8}}
  {\checkempty@title{#8}}
  {}{}
\xpatchcmd{\H@old@sect}
  {\@ifempty{#8}}
  {\checkempty@title{#8}}
  {}{}
\def\checkempty@title#1{\checkempty@title@aux#1}
\def\checkempty@title@aux#1#2#3{\@ifempty{#2}}
\makeatother

\begin{document}
\section{Test}
\subsection{} Text
\end{document}

enter image description here

Actually, there is a bug in amsart: if you have one subsection with empty title, all subsequent section numbers are typeset boldface.

In order to cure this bug another couple of patches are needed:

\documentclass{amsart}

\usepackage{xpatch}
\usepackage{hyperref}

\makeatletter
% we need to patch the saved version of \@sect
\providecommand{\H@old@sect}{} % so this doesn't choke if hyperref is removed
\xpatchcmd{\H@old@sect}
  {\@ifnotempty{#8}}
  {\checkempty@title{#8}{}}
  {}{}
\xpatchcmd{\H@old@sect}
  {\@ifempty{#8}}
  {\checkempty@title{#8}}
  {}{}
\xpatchcmd{\H@old@sect}
  {\@ifempty{#8}}
  {\checkempty@title{#8}}
  {}{}
\xpatchcmd{\H@old@sect}
  {\def\@secnumfont}
  {\gdef\@secnumfont}
  {}{}
\def\checkempty@title#1{\checkempty@title@aux#1}
\def\checkempty@title@aux#1#2#3{\@ifempty{#2}}
% patch the bug of amsart
\xapptocmd{\@seccntformat}
  {\noexpand\reset@secnumfont}
  {}{}
\AtBeginDocument{%
  \edef\reset@secnumfont{%
    \gdef\noexpand\@secnumfont{\expandafter\noexpand\@secnumfont}%
  }%
}
\makeatother

\begin{document}

\section{Test}
\subsection{} Text

\section{Test}

\end{document}

enter image description here

Related Question