# Automatically typeset math in section headings in bold-face

This question is motivated by a question asked me by a friend who is working on his thesis in mathematics. His section headings occasionally contain math, which:

• he would like to be in boldface to match the rest of the section title; but
• he would like to be medium-weight when it occurs in the table of contents, as with the rest of the section title.

He was using \mathbf to make the math boldface in his section headings; this of course also made them bold in the table of contents. I gave him a simple solution which inserted code at the beginning of the table of contents, which would make \mathbf pass its arguments unchanged. Of course, \mathbf also makes its arguments upright rather than math-italics, which I would also consider less than desirable; and it has no effect on e.g. greek characters, for which you would have to use something like the \bm command of bm.sty instead. (You could play the same trick with \bm as with \mathbf in the table of contents, but having to repeatedly use different math macros for all math in section headings to me seems inelegant.)

What would be the most elegant way to automatically render all math (including non-latin characters) in bold-face for section headings, but the normal medium-weight everywhere else (including the entries of the table of contents)?

• Note that the weight of a character usually has mathematical meaning: $a$ is not the same as $\bm{a}$ (the package bm provides the command). If there are math formulas in titles, it would probably be best to avoid having them in boldface to begin with. Jan 17 '12 at 15:54
• As people who frequently work with vectors, my friend and I are both quite familiar with notations which use boldface as a syntactic indicator. However, if working in a discipline where there is no particular use of boldface, the similarity of entities in the section heading to entities in the prose should be sufficient to suggest that the boldface does not serve to describe a different mathematical object, in which case uniformity of typesetting would be a greater desideratum than mitigating the (marginal) risk of confusion. Jan 17 '12 at 16:11

The simplest thing is to patch \@startsection and \@chapter (assuming book or report class):

\usepackage{etoolbox}
\makeatletter
% \tracingpatches
\patchcmd{\@sect}{#8}{\boldmath #8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{\ori@chapter[\boldmath#1]{\boldmath#2}}
\makeatother


#8 is the argument to \@sect that contains the title; but \boldmath will not leak to the table of contents nor to the headers (for headers it's sufficient to use fancyhdr and issue \boldmath, if bold headers are desired).

It doesn't work with \paragraph and \subparagraph, nor with \chapter*. It shouldn't be a problem to issue \boldmath in the argument of an occasional \chapter*.

• Will this work if there is a mixture of maths and text? \section{On euler's equation, $e^{i\pi}+1=0$} Jan 17 '12 at 16:55
• @Seamus Yes, of course. :) \boldmath is a declaration, not a command with arguments. When the section's title is typeset, it's in a group, so the declaration loses its effect afterwards. Jan 17 '12 at 17:06

Here's a solution that I've found to work very well -- add the following to your document preamble

\makeatletter
\makeatother


(or just add the middle line to a .sty/.cls file, where the at-symbol escaping isn't needed.) This just modifies that \bfseries macro to include \boldmath. You also need to make sure that your font family contains bold math symbols, of course: not all do.

I'm not really sure why this isn't done by default... it seems an obviously desirable behaviour. Credit where it's due: I think this was supplied to me some years ago by Donald Arseneau on comp.text.tex.

(PS. Before finding out about this "trick" I wrote the hacky maybemath package, which is a far less elegant way to do this sort of thing but does also allow context-sensitive adaptation of math to sans-serif/roman fonts, and italic/upright styles. The latter may seem a bit strange given that math mode is already italicised by default, but is useful for when the math mode is only being used for access to sub/superscripting, in particular in the hepparticles package (see the testhepparticles.pdf example. A more elegant way to do this would be greatly appreciated!)

• Just as an addendum, I really like this answer. "it seems an obviously desirable behaviour"—I would wholeheartedly agree. Sep 10 '13 at 21:35
• This has now become a permanent part of my standard preamble. Jun 6 '15 at 13:38
• Such a simple solution.... Jun 5 '17 at 23:02
• Such an awesome solution. It works like a charm. Much thanks! Jun 19 '18 at 4:09
• Wow, this works really amazing, it looks like this is exactly what was missing, thanks!! Jul 19 '18 at 18:24

Well, it is possible to do this automatically, but here is a simpler "semi-automatic" solution. Note that \section has two arguments: the mandatory one sets section title, and the optional one (if present) sets the table of contents entry. Here we use \boldmath in the mandatory argument, but not in the optional one. Note also that bold math is not upright, as you want:

\documentclass{article}
\begin{document}
\pagestyle{empty}
\tableofcontents
\section[First: $a=b$]{First: \boldmath{$a=b$}}
\end{document}


Actually, \boldmath does not look bad in TOC either:

\documentclass{article}
\begin{document}
\pagestyle{empty}
\tableofcontents
\section{First: \boldmath{$a=b$}}
\end{document}


I personally like the second solution better: here math blends with the text both in the section title and TOC.

• Good point about the section headings in the table of contents; but that will probably vary with different styles (e.g. in the case of my friend's thesis, apparently they aren't in boldface). For article.cls, the subsection headings in the table of contents aren't in boldface either. Having an automatic approach which is independent of documentclass and section-level would be more in the spirit of good LaTeX-artisanship. Jan 18 '12 at 14:54
\usepackage{sectsty}
\allsectionsfont{\boldmath} % documentclass: not memoir; works also with KOMA-script