# Epigraph and fancyhdr: a patching problem

I would like to add epigraphs on my part pages using epigraph, but since I also use fancyhdr, I cannot get the proper page style on the part page using the solution given in the package documentation.

Since I use the report class, I thought of patching the command \@part using the package xpatch. All I have to do is to insert a command \insertepigraph just before \@endpart (with a couple of \vfill and \vskip to keep the page style sensible). A command called \partepigraph redefine the command to include the \epigraph command, which is cleared immediatly after by \clearpartepigraph so the command affects only one part page.

This approach works perfectly when manually editing report.cls, but pachting the command with \xpatchcmd have no effect, and I do not understand what is wrong with my code (It is the first time I use this command myself).

Could someone help me fixing my code ?

Here is a MWE:

\documentclass[a4paper,twoside,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{epigraph}
\usepackage{fancyhdr}
\usepackage{xpatch}
\usepackage{etoolbox}
\usepackage{hyperref}

\fancypagestyle{plain}{
\fancyfoot[C]{}
\renewcommand{\footrulewidth}{1pt}}

\newcommand\insertepigraph{} % Insert an epigraph at the bottom of a part page

\tracingpatches
\makeatletter
\xpatchcmd{\@part}
{\@endpart}%
{\vfill\insertepigraph\clearpartepigraph\@endpart}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}

\xpatchcmd{\@part}
{\markboth{}{}}%
{\markboth{}{}\vskip 0.25\textheight}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}
\makeatother

\begin{document}
\thispagestyle{plain}

\partepigraph{\footnotesize La notion de passoire est indépendante de la notion de trou, et réciproquement.\\ \vskip3mm The notion of a colander is independant of the notion of a hole, and reciprocally.}{Rouxel (Les Shadoks)\\ \textit{{\scriptsize mocking the non-intuitive principles of quantum mechanics.}}}

\part{Some part title}

\part{Some other part title}

\part{Again some other part title}

\chapter{Ciel}

\end{document}

• \ifpatchable{\@part}{\@endpart}{\typeout{Yes}}{\typeout{Nope!}} yields: Nope! You can't patch \@part this way then. How about \xpretocmd{\@endpart} with your code that should be done before \@endpart is used? – user31729 Nov 26 '15 at 15:57
• \xpretocmd{\@endpart}{\vfill\insertepigraph\clearpartepigraph}{}{}works fine, but any idea how to add the \vskip 0.25\textheight at the level of \markboth{}{} ? – HcN Nov 26 '15 at 16:12
• Unfortunately not yet! I'll try to understand why the command isn't patchable at all – user31729 Nov 26 '15 at 16:14

Apparently hyperref does some changes to the structuring level code such that \xpatchcmd{\@part}{...} etc. can't work anymore. Do the change before hyperref is used, i.e. load hyperref later on.

hyperref uses various driver files and some of use \def\@part, thereby changing the behaviour (slightly) → \@endpart and other code hooks can't be found anymore.

\documentclass[a4paper,twoside,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{epigraph}
\usepackage{fancyhdr}
\usepackage{xpatch}

%\tracingpatches

\makeatletter

\tracingpatches
\xpatchcmd{\@part}
{\@endpart}%
{\vfill\insertepigraph\clearpartepigraph\@endpart}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}

\xpatchcmd{\@part}
{\markboth{}{}}%
{\markboth{}{}\vskip 0.25\textheight}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}
\makeatother

\usepackage{hyperref}

\fancypagestyle{plain}{
\fancyfoot[C]{}
\renewcommand{\footrulewidth}{1pt}}

\newcommand\insertepigraph{} % Insert an epigraph at the bottom of a part page

\begin{document}
\thispagestyle{plain}

\partepigraph{\footnotesize La notion de passoire est indépendante de la notion de trou, et réciproquement.\\ \vskip3mm The notion of a colander is independant of the notion of a hole, and reciprocally.}{Rouxel (Les Shadoks)\\ \textit{{\scriptsize mocking the non-intuitive principles of quantum mechanics.}}}

\part{Some part title}

\part{Some other part title}

\part{Again some other part title}

\chapter{Ciel}

\end{document}


Use a simple redefinition of \@endpart:

\documentclass[a4paper,twoside,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{epigraph}
\usepackage{fancyhdr}
\usepackage{hyperref}

\fancypagestyle{plain}{%
\fancyfoot[C]{}%
\renewcommand{\footrulewidth}{1pt}}

\newcommand\insertepigraph{} % Insert an epigraph at the bottom of a part page

\makeatletter
\def\@part[#1]#2{%
\refstepcounter{part}%
\markboth{}{}
\vspace{0.25\textheight}%
{\centering
\interlinepenalty \@M \normalfont
\huge\bfseries \partname\nobreakspace\thepart\par
\vskip 20\p@ \Huge \bfseries #2\par}%
\@endpart}
\def\@endpart{%
\vfill\insertepigraph\clearpartepigraph\newpage
\if@twoside\if@openright\null
\thispagestyle{empty}%
\newpage\fi\fi}
\makeatother

\begin{document}
\thispagestyle{plain}

\partepigraph{\footnotesize La notion de passoire est indépendante de la notion de trou, et réciproquement.\\ \vskip3mm The notion of a colander is independant of the notion of a hole, and reciprocally.}{Rouxel (Les Shadoks)\\ \textit{{\scriptsize mocking the non-intuitive principles of quantum mechanics.}}}

\part{Some part title}

\part{Some other part title}

\part{Again some other part title}

\chapter{Ciel}

\end{document}


• Thanks a lot, this works nice too, but what about the \vskip 0.25\textheight at the level of \markboth{}{} ? – HcN Nov 26 '15 at 16:14
• Where should the \vskip appear? Before or after the epigraph? – user2478 Nov 26 '15 at 16:17
• Above the moment when \thepart and \partname are being written. – HcN Nov 26 '15 at 16:20
• see edited answer. – user2478 Nov 26 '15 at 16:26

Always do \show\command before trying a patch, so as to ensure that you know what to patch. If you add \show\@part before the first \xpatchcmd, you'll get the following output in the console (and in the log file):

> \@part=macro:
->\ifnum \Hy@secnum@part >\c@secnumdepth \phantomsection \fi \H@old@part .
l.23 \show\@part

?


It's like an error message, but not a real error because there's no leading !.

What does this mean? That hyperref already did something on \@part: it does the usual \let\old@part\@part and \def\@part{<something>\old@part} to add some actions before the real command is performed.

The good candidate is \H@old@part and, indeed, typing i\show\H@old@part at the prompt gives

? i\show\H@old@part
> \H@old@part=macro:
\markboth {}{}{\centering \interlinepenalty \@M \normalfont \ifnum \c@secnumdep
th >-2\relax \huge \bfseries \partname \nobreakspace \thepart \par \vskip 20\p@
\fi \Huge \bfseries #2\par }\@endpart .
<insert>   \show\H@old@part

l.23 \show\@part

?


Good! There's nothing more to be looked for! Stop the interactive session, go back to the file and change \@part into \H@old@part.

\documentclass[a4paper,twoside,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{epigraph}
\usepackage{fancyhdr}
\usepackage{xpatch}
\usepackage{etoolbox}
\usepackage{hyperref}

\fancypagestyle{plain}{
\fancyfoot[C]{}
\renewcommand{\footrulewidth}{1pt}}

\newcommand\insertepigraph{} % Insert an epigraph at the bottom of a part page

\tracingpatches
\makeatletter
\xpatchcmd{\H@old@part}
{\@endpart}%
{\vfill\insertepigraph\clearpartepigraph\@endpart}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}

\xpatchcmd{\H@old@part}
{\markboth{}{}}%
{\markboth{}{}\vskip 0.25\textheight}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}
\makeatother

\begin{document}
\thispagestyle{plain}

\partepigraph{\footnotesize La notion de passoire est indépendante de la notion de trou, et réciproquement.\\ \vskip3mm The notion of a colander is independant of the notion of a hole, and reciprocally.}{Rouxel (Les Shadoks)\\ \textit{{\scriptsize mocking the non-intuitive principles of quantum mechanics.}}}

\part{Some part title}

\part{Some other part title}

\part{Again some other part title}

\chapter{Ciel}

\end{document}


You may want to have the patches in a personal package; in this case the original patching can be done there, provided you load this personal package before hyperref. Or, if you want “independent” code to be after package loading, just use a trick:

\makeatletter
{\xpatchcmd{\H@old@part}}
{\xpatchcmd{\@part}}
{\@endpart}%
{\vfill\insertepigraph\clearpartepigraph\@endpart}%
{\PackageWarning{Patch}{Success}}
{\PackageWarning{Patch}{Failure}}