当我使用 amsart 文档类并将子部分标题留空时,子部分编号将以粗体显示。最小工作示例:
\documentclass{amsart}
\begin{document}
\section{Test}
\subsection{} Text
\end{document}
奇怪的是,当我包含 hyperref 包时,小节编号不再加粗。MWE:
\documentclass{amsart}
\usepackage{hyperref}
\begin{document}
\section{Test}
\subsection{} Text
\end{document}
在使用 hyperref 时,有没有什么方法可以让子节编号以粗体显示?我担心如果在加载 hyperref 后重新定义 \subsection 命令,会破坏 hyperref 的某些功能。
答案1
该类amsart
确实具有这样的特点:如果参数为\subsection
空,则数字以粗体打印,而通常为中等系列。
我个人一点也不喜欢这个功能。然而,\@sect
执行该任务的代码是(行号参考amsart.cls
)
1055 \@ifempty{#8}{%
1056 \ifnum #2=\tw@ \def\@secnumfont{\bfseries}\fi}{}%
但是当hyperref
加载时,参数#8
是
\Sectionformat{<title>}{2}
因此就其本身而言它并不是空的\@ifempty
。
这是它的补丁。
\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}
事实上,是一个错误amsart
:如果您有一个标题为空的子节,则所有后续节号都将以粗体排版。
为了修复此错误,需要另外几个补丁:
\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}
答案2
您可以指定章节编号是否为粗体:
\documentclass{amsart}
\usepackage{hyperref}
\makeatletter
\def\@secnumfont{\bfseries}
\makeatletter
\begin{document}
\section{Test}
\subsection{} Text
\end{document}
如果您指定一个空标题,那么遗憾的是,为标题文本指定的字体将应用于数字,但是您不应该依赖该错误功能,因为它可以被修复。
空标题不只是使一个标题有粗体数字,它会影响所有级别的所有标题,请考虑
\documentclass{amsart}
%\usepackage{hyperref}
\begin{document}
\section{Test}
\subsection{x} Text
\subsection{} Text
\subsection{} Text
\subsection{x} Text
\section{test2}
\end{document}
这使得
这修复了空标题更改所有后续标题字体的错误,并在 hyperref 中获取了正确的字体,在 hyperref 情况下它仍然有额外的水平空间
\documentclass{amsart}
\usepackage{hyperref}
\makeatletter
%\def\@secnumfont{\bfseries}
\def\@sect#1#2#3#4#5#6[#7]#8{%
\edef\@toclevel{\ifnum#2=\@m 0\else\number#2\fi}%
\ifnum #2>\c@secnumdepth \let\@secnumber\@empty
\else \@xp\let\@xp\@secnumber\csname the#1\endcsname\fi
\@tempskipa #5\relax
\ifnum #2>\c@secnumdepth
\let\@svsec\@empty
\else
\refstepcounter{#1}%
\edef\@secnumpunct{%
\ifdim\@tempskipa>\z@ % not a run-in section heading
\@ifnotempty{#8}{.\@nx\enspace}%
\else
\@ifempty{#8}{.}{.\@nx\enspace}%
\fi
}%
%%%%
\def\zz{#8}%
\def\tmp{\Sectionformat{}{2}}%
\ifx\zz\tmp\def\zz{}\fi
\let\zzz\@secnumfont
%%%%
\expandafter\@ifempty\expandafter{\zz}{%
\ifnum #2=\tw@ \def\@secnumfont{\bfseries}\fi}{}%
\protected@edef\@svsec{%
\ifnum#2<\@m
\@ifundefined{#1name}{}{%
\ignorespaces\csname #1name\endcsname\space
}%
\fi
\@seccntformat{#1}%
}%
\let\@secnumfont\zzz
\fi
\ifdim \@tempskipa>\z@ % then this is not a run-in section heading
\begingroup #6\relax
\@hangfrom{\hskip #3\relax\@svsec}{\interlinepenalty\@M #8\par}%
\endgroup
\ifnum#2>\@m \else \@tocwrite{#1}{#8}\fi
\else
\def\@svsechd{#6\hskip #3\@svsec
\expandafter\@ifnotempty\expandafter{\zz}{\ignorespaces#8\unskip
\@addpunct.}%
\ifnum#2>\@m \else \@tocwrite{#1}{#8}\fi
}%
\fi
\global\@nobreaktrue
\@xsect{#5}%
}
\def\@seccntformat#1{%
\protect\textup{\@secnumfont
\csname the#1\endcsname
\protect\@secnumpunct
}%
}
\makeatletter
\begin{document}
\section{Test}
\subsection{x} Text
\subsection{} Text
\subsection{} Text
\subsection{x} Text
\section{test2}
\end{document}