50

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)?

2
  • 5
    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.
    – egreg
    Jan 17, 2012 at 15:54
  • 5
    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, 2012 at 16:11

5 Answers 5

16

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*.

2
  • Will this work if there is a mixture of maths and text? \section{On euler's equation, $e^{i\pi}+1=0$}
    – Seamus
    Jan 17, 2012 at 16:55
  • 2
    @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.
    – egreg
    Jan 17, 2012 at 17:06
73

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

\makeatletter
\g@addto@macro\bfseries{\boldmath}
\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!)

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

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}

enter image description here

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

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

enter image description here

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

2
  • 1
    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, 2012 at 14:54
  • This solution is problematic if you are using a style where the section headers show up at the topf pf the page, like for example ``book' ', because the secion headers will display math in biold and text in normal wight. Dec 10, 2022 at 1:09
2
\usepackage{sectsty}
\allsectionsfont{\boldmath} % documentclass: not memoir; works also with KOMA-script
\setsecheadstyle{\boldmath} % documentclass: memoir

But be aware about @barbara-beeton's comment at a similiar question:

in some contexts it's not considered good practice to bold math just to follow the style of a title. if there's any chance that bold vs. non-bold has a distinct difference in meaning, introducing possible ambiguity isn't a good idea.

0

to expand on this answer by @andybuckley, the problem with amsthm discussed in the comments (I can't comment myself due to a lack of reputation) can be fixed by the following code:

\makeatletter
\g@addto@macro\bfseries{\boldmath}
\def\thmhead@plain#1#2#3{%
  \thmname{#1}\thmnumber{\@ifnotempty{#1}{ }\@upn{#2}}%
  \thmnote{ {\the\thm@notefont\unboldmath(#3)}}}
\let\thmhead\thmhead@plain
\makeatother

The reason why the problem appears is that amsthm roughly does the following:

\begin{theorem}[extra note $a$]
is compiled to
{\bfseries Theorem 1 {\fontseries\mddefault\upshape extra note $a$}.}

This is why extra note appears normal while $a$ appears in boldface if the fix is not applied.

You must log in to answer this question.

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