# Package hyperref destroy \leftmark and \rightmark with KOMA

I noticed, when I use \usepackage{hyperref} the left and right marks are destroyed. One can verify this by uncommenting hyperref in MWE below.

Without hyperref

With hyperref

\documentclass[oneside,
a4paper,
parskip=off,
12pt]{scrbook}

%\usepackage{hyperref}

\usepackage{lipsum}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@part}{\@mkboth{}{}}{\@mkboth{#1}{#1}}{}{} %
\makeatother

\usepackage{scrlayer-scrpage}
\begin{document}
\part{One}
\lipsum[1-20]
\part{Two}
\lipsum[1-20]
\end{document}


How can I prevent this undesirable behavior? Or, how to insert headers, which is contain a part title in KOMA scrbook simultaneously using hyperref?

• hyperref is usually loaded last. – Johannes_B Jan 19 '15 at 19:54
• Of course, there are exceptions, like glossaries. scrlayer-scrpage should be loaded before hyperref and you are fine. – Johannes_B Jan 19 '15 at 20:01
• @Johannes_B Thank you, I'll remember this rule for hyperref. – sergiokapone Jan 19 '15 at 20:54

hyperref does a lot of behind-the-scenes work in order to make hyperlinks work correctly inside a document. As such, it typically stores existing commands for usage later, and updating the definitions according to its requirements.

In your specific case, \@part is not the macro to be patched when you are using hyperref. Instead you need to patch \H@old@part - made by hyperref to store the "old" version of \@part:

\documentclass[oneside,
a4paper,
parskip=off,
12pt]{scrbook}

\usepackage{hyperref}

\usepackage{lipsum}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\H@old@part}{\@mkboth{}{}}{\@mkboth{#1}{#1}}{}{} %
\makeatother

\usepackage{scrlayer-scrpage}
\begin{document}
\part{One}
\lipsum[1-20]
\part{Two}
\lipsum[1-20]
\end{document}


You would have also picked up that your \patchcmd is not work as expected in you added \tracinpatches to your preamble and checked your .log:

[debug] tracing \patchcmd on input line 13
[debug] analyzing '\@part'
[debug] ++ control sequence is defined
[debug] ++ control sequence is a macro
[debug] ++ macro can be retokenized cleanly


The search pattern \@mkboth{}{} is not found, since hyperref redefined \@part to be

\def\@part{%
\ifnum\Hy@secnum@part>\c@secnumdepth
\phantomsection
\fi
\H@old@part
}


The alternative, as with most things related to hyperref, would be to load it last.

• Since hyperref should be loaded last, hyperref will use the patched macro. – Johannes_B Jan 19 '15 at 19:59
• @Johannes_B: Yes, I've added that to my answer. I first wanted to address the reason why it doesn't/didn't work. – Werner Jan 19 '15 at 20:01
• Got my upvote with that addition ;-) – Johannes_B Jan 19 '15 at 20:02

You could use the KOMA-Script command \automark and redefine \partmarkformat to get the desired result:

\usepackage{scrlayer-scrpage}
\automark[part]{part}
\renewcommand\partmarkformat{}


Code:

\documentclass[oneside,
a4paper,
parskip=off,
12pt]{scrbook}

\usepackage{hyperref}

\usepackage{lipsum}

\usepackage{scrlayer-scrpage}
\automark[part]{part}
\renewcommand\partmarkformat{}

\begin{document}
\part{One}
\lipsum[1-20]
\part{Two}
\lipsum[1-20]
\end{document}

• Yes, this more laconic solution. Thank you! And I have another problem with koma. Did you know how to assign empty pagestyle to all toc pages only (in KOMA)? – sergiokapone Jan 19 '15 at 20:50
• Only in the table of contents or also in the list of tables etc? Can you open a new question? – esdd Jan 20 '15 at 17:00