# Non-bold math in header with fancyhdr?

MWE:

\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
\patchcmd{\@sect}{#8}{\boldmath #8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{\ori@chapter[\boldmath#1]{\boldmath#2}}
\makeatother

\begin{document}

\pagestyle{fancy}
\fancyhf{}

\tableofcontents

\setcounter{page}{1}

\chapter{Proof that $x=y$}

\section{Proof that $x=y$}
This chapter proves that $x=y$

\newpage

$x=y$.

\end{document}


On compiling this document you'll see that the math is correctly typeset in the TOC (bold where regular text is bold, not otherwise) but not in the fancy header with the title. Is there an easy fix for this?

• \fancyhead[L]{\bfseries\nouppercase{\leftmark}} -- boldmath works only for math mode, not for regular text – user31729 Apr 28 '16 at 19:04
• But do you really want to have a boldface header? – Bernard Apr 28 '16 at 19:05

\boldmath does have no effect on regular text, see the example at the end of the code.

There must be a \bfseries command in \leftmark, if this is really wanted.

I've increased the head height value (since fancyhdr complains about the value being too small!)

\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
\patchcmd{\@sect}{#8}{\boldmath #8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{\ori@chapter[\boldmath#1]{\boldmath#2}}
\makeatother

\begin{document}

\pagestyle{fancy}
\fancyhf{}

\tableofcontents

\pagenumbering{arabic}

\chapter{Proof that $x=y$}

\section{Proof that $x=y$}
This chapter proves that $x=y$

\newpage

$x=y$.

\verb!\boldmath foo -- will be normal text and not bold!!

\boldmath foo

\end{document}


Update

\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{xpatch}
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents

\xpatchcmd{\@sect}{#8}{\boldmath#8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{%
\ori@chapter[#1]{\boldmath#2}
%  \addtocontents{toc}{\protect\unboldmath} % Use this for switch of bold math after chapter
}

\AtBeginDocument{%
\unboldmath
}

\begin{document}
\pagestyle{fancy}
\fancyhf{}

\tableofcontents

\pagenumbering{arabic}

\chapter{Proof that $x=y$}

\section{Proof that $x=y$}
This chapter proves that $x=y$

\newpage

$x=y$.

\verb!\boldmath foo -- will be normal text and not bold!!

\boldmath foo\unboldmath

\end{document}

• Thanks for this. However, I had initially wanted to change things in the other direction, i.e. to have the entire header including math not be bold (sorry, I now realise that my question was ambiguous). Is this possible? – Gavin Kirby Apr 28 '16 at 19:51
• @GavinKirby: I'll try – user31729 Apr 28 '16 at 19:59
• @GavinKirby: See the update at the bottom please! – user31729 Apr 28 '16 at 20:23

You should patch more carefully:

\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\setlength{\headheight}{14.5pt} % to comply with fancyhdr recommendation

\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
{\bfseries}
{\bfseries\boldmath}
{}{}
{\bfseries}
{\bfseries\boldmath}
{}{}
\patchcmd{\l@chapter}% typesets the chapter titles in the TOC
{\bfseries}
{\bfseries\boldmath}
{}{}
\patchcmd{\@sect}% for sectional titles
{#8}
{\boldmath #8}
{}{}
\makeatother

\begin{document}

\pagestyle{fancy}
\fancyhf{}

\tableofcontents

\setcounter{page}{1}

\chapter{Proof that $x=y$}

\section{Proof that $x=y$}
This chapter proves that $x=y$

\newpage

$x=y$.

\end{document}


Instead of adding \boldmath to the arguments of \@chapter, I just add it at typesetting time, when \@makechapterhead or \@makeschapterhead is involved. The patch to \@sect works because all sectional commands use \bfseries; in other cases, patches similar to the other ones (that is, \bfseries to \bfseries\boldmath) should be applied.