# \bm no longer works with \mathrm when loading several fonts

This is a follow up to my previous question. I want listings to be able to handle valid python identifiers in listings using pdfLaTeX.

The MWE literate_mwe.tex produces the following output from the input file literate_test.txt: The commands to listings to produce these glyphs can be seen here. Notably, the third and last lines are supposed to have bold upright serif characters (latin letters and numbers resp.) but instead they only produce upright serif characters. The command to produce bold upright "g", for example, is almost exactly that given in the bm documentation (see section 3.1): \bm{{\mathrm{g}}}.

However, clearly this works for the other font families: for example, \bm{{\mathsf{0}}} works just fine in the last row to produce an upright bold sans-serif zero.

How can I diagnose what's happened to \bm? How can I avoid or fix this behavior to obtain upright bold serif math?

EDIT: changing the bolding command for just the upright serif characters to e.g. \mathbf{g} causes pdfLaTeX to choke and emit Too many math alphabets used in version normal when it hits the first monospace character. Could \bm be silently swallowing the error then?

• It would be better to include your MWE inline, rather than link to an external site. Some questions here are still being used as references more than a decade later, so they should ideally be self-contained. Mar 15 at 7:44
• I’d also recommend that you use unicode-math for this unless your publisher is forcing you not to. Otherwise, you might have a look at isomath, which defines \mathbfit, \mathsfbfit, etc. as alternatives to \bm{\mathrm}, and also lets you load an OML-encoded upright version of Utopia as your \mathrm font. Mar 15 at 7:47
• pdftex only allows you to define 16 font families. You have posted no example code only an image but I suspect you are trying to use more. At its simplest bm doubles the use of font families making a bold version of each existing family. the bm manual documents how to control this and what it does when it runs out. Mar 15 at 8:15

Legacy LaTeX is limited to sixteen math alphabets. I would strongly, strongly recommend that you port your code to unicode-math and LuaLaTeX if you can. The OpenType successor to fourier is fourier-otf.

Even if someone is forcing you to use PDFTeX, you might want to use isomath, which defines the alphabets \mathbfit, \mathsfbfit, and so on, and also allows you to load an upright, OML-encoded Utopia math alphabet with Greek letters as \mathrm and \mathbf.

Even if you don’t do that, I’d highly recommend using standard names like \mathbf and \mathbfsfit in your source, rather than \bm{\mathrm{x}}. You can still define them as \providecommand\mathbfsfit[1]{\boldsymbol{\mathsfit{#1}}} and so on. (It is preferable to use \boldsymbol in this context instead of \bm, as it takes up fewer math alphabets and is compatible with more packages.) Note that mathalpha defines bolder versions of many other math alphabets, including calligraphic and blackboard-bold, so you should use those if you load it.

The minimal fix for your MWE is to re-declare \mathrm and \mathbf as the mdput family (Math Design Utopia, compatible with the Utopia used by fourier) in the OML encoding, which supports Greek letters. I likewise define \mathsfit to use the math alphabet from sansmathfonts, which contains Greek.

When you set your math font to an encoding that supports Greek, you can use \mathrm{\alpha}, \mathbf{\beta} or \boldsymbol{\mathrm{\gamma}}, but I also define \upalpha and the rest for backward-compatibility with upgreek. (The fourier package has its own versions, but you had been using upgreek.) In order to be able to use \DeclareMathSymbol on them, I redefine \mathrm as an alias for the new upletters symbol font, saving a few math alphabets.

\documentclass{article}
\tracinglostchars=2
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{fontaxes}
\usepackage{amsmath, amsthm, amssymb}
\usepackage[bb=boondox, cal=boondox]{mathalpha}

\providecommand\bm[1]{\boldsymbol{#1}}

\let\mathbbold\mathbb
\AtBeginDocument{% thanks fourier
\let\mathbb\relax
\newcommand{\mathbb}[1]{\mathbbold{#1}}
}

\let\mathsfit\undef
\DeclareMathAlphabet{\mathsfit}{OML}{cmssm}{m}{it}
\SetMathAlphabet{\mathsfit}{bold}{OML}{cmssm}{b}{it}
\DeclareSymbolFont{upletters}{OML}{mdput}{m}{n}
\SetSymbolFont{upletters}{bold}{OML}{mdput}{b}{n}
\DeclareSymbolFontAlphabet{\mathrm}{upletters}
\DeclareMathAlphabet{\mathbf}{OML}{mdput}{b}{n}

\DeclareMathSymbol{\upGamma}{\mathalpha}{upletters}{"00}
\DeclareMathSymbol{\upDelta}{\mathalpha}{upletters}{"01}
\DeclareMathSymbol{\upTheta}{\mathalpha}{upletters}{"02}
\DeclareMathSymbol{\upLambda}{\mathalpha}{upletters}{"03}
\DeclareMathSymbol{\upXi}{\mathalpha}{upletters}{"04}
\DeclareMathSymbol{\upPi}{\mathalpha}{upletters}{"05}
\DeclareMathSymbol{\upSigma}{\mathalpha}{upletters}{"06}
\DeclareMathSymbol{\upUpsilon}{\mathalpha}{upletters}{"07}
\DeclareMathSymbol{\upPhi}{\mathalpha}{upletters}{"08}
\DeclareMathSymbol{\upPsi}{\mathalpha}{upletters}{"09}
\DeclareMathSymbol{\upOmega}{\mathalpha}{upletters}{"0A}
\DeclareMathSymbol{\upalpha}{\mathalpha}{upletters}{"0B}
\DeclareMathSymbol{\upbeta}{\mathalpha}{upletters}{"0C}
\DeclareMathSymbol{\upgamma}{\mathalpha}{upletters}{"0D}
\DeclareMathSymbol{\updelta}{\mathalpha}{upletters}{"0E}
\DeclareMathSymbol{\upepsilon}{\mathalpha}{upletters}{"0F}
\DeclareMathSymbol{\upzeta}{\mathalpha}{upletters}{"10}
\DeclareMathSymbol{\upeta}{\mathalpha}{upletters}{"11}
\DeclareMathSymbol{\uptheta}{\mathalpha}{upletters}{"12}
\DeclareMathSymbol{\upiota}{\mathalpha}{upletters}{"13}
\DeclareMathSymbol{\upkappa}{\mathalpha}{upletters}{"14}
\DeclareMathSymbol{\uplambda}{\mathalpha}{upletters}{"15}
\DeclareMathSymbol{\upmu}{\mathalpha}{upletters}{"16}
\DeclareMathSymbol{\upnu}{\mathalpha}{upletters}{"17}
\DeclareMathSymbol{\upxi}{\mathalpha}{upletters}{"18}
\DeclareMathSymbol{\uppi}{\mathalpha}{upletters}{"19}
\DeclareMathSymbol{\uprho}{\mathalpha}{upletters}{"1A}
\DeclareMathSymbol{\upsigma}{\mathalpha}{upletters}{"1B}
\DeclareMathSymbol{\uptau}{\mathalpha}{upletters}{"1C}
\DeclareMathSymbol{\upupsilon}{\mathalpha}{upletters}{"1D}
\DeclareMathSymbol{\upphi}{\mathalpha}{upletters}{"1E}
\DeclareMathSymbol{\upchi}{\mathalpha}{upletters}{"1F}
\DeclareMathSymbol{\uppsi}{\mathalpha}{upletters}{"20}
\DeclareMathSymbol{\upomega}{\mathalpha}{upletters}{"21}
\DeclareMathSymbol{\upvarepsilon}{\mathalpha}{upletters}{"22}
\DeclareMathSymbol{\upvartheta}{\mathalpha}{upletters}{"23}
\DeclareMathSymbol{\upvarpi}{\mathalpha}{upletters}{"24}
\DeclareMathSymbol{\upvarrho}{\mathalpha}{upletters}{"25}
\DeclareMathSymbol{\upvarsigma}{\mathalpha}{upletters}{"26}
\DeclareMathSymbol{\upvarphi}{\mathalpha}{upletters}{"27}

\usepackage{listings}
\input{literate.tex}

\pagestyle{empty}

\begin{document}

\lstinputlisting{literate_test.txt}

\end{document}


Rather than load bm and nearly-disabling it by setting \bmmax to 0, I here redefine it as \boldsymbol. (Don’t do this in your source! Use meaningful names like \vectorsym and \tensorsym.)

There are also a few other tweaks, such as mathalpha for mathalfa, removing a few packages that I made redundant, and replacing fix-cm with fontaxes when you are not using Computer Modern.

• \boldsymbol always uses an \hbox and a nested math list so doesn't double the font families but is slower than bm and loses per-character kerning information. (that said, if you want this many families in pdftex you need to do something like that.) bm drops down to a method equivalent to boldsymbol once it gets past bmmax familiy allocations (which is why the too many falilies error was avoided as observed by the OP) Mar 15 at 8:24
• @DavidCarlisle When I tweak the definitions here, particularly by using OML alphabets for both Latin and Greek, I don’t run out on this MWE even using \boldsymbol. That is an important consideration, though. Mar 15 at 8:31
• @DavidCarlisle Thanks for the correction on \bm and \boldsymbol being “equivalent.” I changed my wording. Mar 15 at 8:32
• note you have \newcommand\bmmax{0} so bm is all but disabled in any case here. Mar 15 at 8:32
• Yes choosing just by hand the bold alphabets you need is the correct answer here. bm makes everything bold in particular all the symbol fonts that are not used here get allocated at both weights so you probably run out of math fam before you start on the math alphabets that are actually in this example. Mar 15 at 8:35