我注意到,如果你使用ulem
和重新定义来强调标题\sectionlinesformat
(就像在这个答案) 与 相冲突hyperref
。冲突仅发生在未编号的章节、小节和小小节上(无论它们是否因星号版本、使用\addsec
或 的设置而未编号\setcounter{secnumdepth}{<number>}
),因此本质上发生在使用 -- 的宏上,而不是使用(章节)\sectionlinesformat
的宏上。\chapterlinesformat
如果您不扩展宏的参数\headuline
(删除所有\expandafter
s)或不使用 ,则不会发生错误hyperref
。问题是,如果不扩展标题的参数,\headuline
标题就无法被破坏。
错误信息是:
! Extra \else.
\H@old@ssect ...empty {#5}\@@par }\endgroup \else
\edef \@svsechd {\unexpand...
您可以使用以下 MWE 重现该错误:
\documentclass[a4paper,11pt]{scrreprt}
\usepackage[normalem]{ulem}
\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
\setbox0\hbox{\expandafter\headuline\expandafter{#2}}% @hangfrom had no effect so I used this setup for chapters
\usebox0\parbox[t]{\dimexpr\textwidth-\wd0}{%
\expandafter\headuline\expandafter{#3}%
}%
}
\renewcommand{\sectionlinesformat}[4]{%
\@hangfrom{\hskip #2\expandafter\headuline\expandafter{#3}}%
{\expandafter\headuline\expandafter{#4}}%
}
\makeatother
\newcommand\headuline{%
\bgroup\markoverwith{\rule[-0.42ex]{0.25ex}{1.2pt}}\ULon%
}%
\setcounter{secnumdepth}{3}
\usepackage{hyperref}
\begin{document}
\chapter{This is a really long chapter heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\section{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsection{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsubsection{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\chapter*{This is a really long chapter heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\section*{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsection*{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsubsection*{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\end{document}
答案1
我认为在这样的地方使用 ulem 是疯狂的——你无法控制输入,而且它很容易出问题。而且它看起来很糟糕。除此之外:hyperref 在#4
\sectionlinesformat 参数的开头插入了一个锚点,因此\expandafter
扩展了错误的命令。
您可以尝试输入\headuline
:\Sectionformat
\documentclass[a4paper,11pt]{scrreprt}
\usepackage[normalem]{ulem}
\makeatletter
\renewcommand{\chapterlinesformat}[3]{%
\setbox0\hbox{\expandafter\headuline\expandafter{#2}}% @hangfrom had no effect so I used this setup for chapters
\usebox0\parbox[t]{\dimexpr\textwidth-\wd0}{%
\expandafter\headuline\expandafter{#3}%
}%
}
\renewcommand{\sectionlinesformat}[4]{%
\@hangfrom{\hskip #2\expandafter\headuline\expandafter{#3}}%
{#4}%
}
\makeatother
\newcommand\headuline{%
\bgroup\markoverwith{\rule[-0.42ex]{0.25ex}{1.2pt}}\ULon%
}%
\setcounter{secnumdepth}{3}
\usepackage{hyperref}
\AtBeginDocument{\renewcommand\Sectionformat[2]{\headuline{#1}}}
\begin{document}
\chapter{This is a really long chapter heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\section{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsection{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsubsection{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\chapter*{This is a really long chapter heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\section*{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsection*{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\subsubsection*{This is a really long section heading and I'm proud of it. It is so
long it needs more than one row to be printed.}
\end{document}