5

I noticed, that if you underline headings with the use of ulem and redefining \sectionlinesformat (as done in this answer) does conflict with hyperref. The conflict only occurs on unnumbered sections, subsections and subsubsections (regardless whether they are unnumbered due to a starred version, the usage of \addsec or the setting of \setcounter{secnumdepth}{<number>}), so essentially on those macros which use \sectionlinesformat -- not on those which use \chapterlinesformat (chapters).

The error doesn't occur if you don't expand the argument of the \headuline macro (remove all \expandafters) or if you don't use hyperref. The problem is, without expanding the argument of \headuline the headings are not breakable.

The error message is:

! Extra \else.
\H@old@ssect ...empty {#5}\@@par }\endgroup \else 
                                                  \edef \@svsechd {\unexpand...

You can use the following MWE to reproduce the error:

\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 Answer 1

3

Imho it is madness to use ulem in such a place -- you have not much control over the input and it can easily break. Also it looks very bad. Beside this: hyperref inserts an anchor at the begin of argument #4 of \sectionlinesformat and so the \expandafter expand the wrong command.

You can try to put your \headuline in \Sectionformat instead:

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .