# \boldmath with unicode-math AND fonts with a MATH table

There are several questions on this site about \boldmath with unicode-math. As Example 1 below shows, \boldmath does not in general work for unicode fonts with a MATH table, unless there is a specific bold version of the math font. So far, I have not found one for any fonts except XITS Math; from what I have heard, Lucida has one, too, but it is not free. How can I set the \boldmath font with unicode-math? provides an ugly solution that only works for XeLaTeX. Since I am using LuaLaTeX, I am looking for something else (and prettier).

Bold italic math with unicode-math contains a solution to get it to work using unicode fonts without a proper MATH table. The solution also works for fonts with a proper MATH table, provided you replace the math font with the italic text font (see Example 2). As the example shows, the accent (\hat) does not look good in this case.

So the preferable solution would be:

• Ordinary, non-bold math should be typeset using TeX Gyre Pagella Math
• \boldmath math should be typeset using the bold italic text font TeX Gyre Pagella Bold Italic

I tried that in Example 3 below, but it seems text and math fonts do not work together in this case, for integral signs and everything else break.

Finally, I tried in Example 4 to play around with version=bold, but curiously, this makes all math bold. Is this a bug?

Can someone get this to work for me?

% !TeX program=luatex
\documentclass{article}

\usepackage{fontspec,amsmath}

\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}

\usepackage{unicode-math}

\newcommand\mytest{
$\hat f(t) = \int_0^t \hat f'(x)\, dx$
{\boldmath $\hat f(t) = \int_0^t \hat f'(x)\, dx$}
}

\begin{document}

Example 1:

\setmathfont{TeX Gyre Pagella Math}

\mytest

Example 2:

\setmathfont{TeX Gyre Pagella Math}

\setmathfont[
range=\mathit/{latin,Latin,greek,Greek,num},
BoldFont=TeX Gyre Pagella Bold Italic
]{TeX Gyre Pagella Italic}

\mytest

Example 3:

\setmathfont[BoldFont=TeX Gyre Pagella Bold Italic]{TeX Gyre Pagella Math}

\mytest

Example 4:

\setmathfont{TeX Gyre Pagella Math}
\setmathfont[
version=bold,
range=\mathit/{latin,Latin,greek,Greek,num}
]{TeX Gyre Pagella Bold Italic}

\mytest

\end{document}


• \boldmath has always made all math symbols bold. Jun 6 '15 at 17:12
• I don't think symbols like integrals are available in bold versions in TeX Gyre Pagella Math. At least there is no dedicated "TeX Gyre Pagella Math Bold" font (as far as I have understood). On the contrary, XITS Math has a bold version with all symbols in both. So in other words, XITS and Lucida (from what I've heard---I haven't bought it myself) are currently the only unicode math fonts that are useful in practice, since the lack of a bold version is an extremely severe limitation. Jun 6 '15 at 17:16
• Unicode has the latin and greek alphabet at the "Mathematical alphanumeric symbols"-range: 0x1D400–0x1D7FF, which covers, among others, the bold and bold italic. Jun 6 '15 at 18:38
• @Gaussler: Man, you're quick! :-D Please, give me just a minute... Jun 6 '15 at 18:40
• And by "one minute" I of course meant 15 minutes... ;) Hope it helps! Jun 6 '15 at 19:01

Well the logical solution is to setup a new math version, which should be independant from the normal math version.

But imho there is today not much chance to get the version key working in your use case. As long as only real math fonts are involved it works fine, but in the combination with range and text fonts it is buggy as one math version affects the other.

Currently I would implement a bold mathversion by embolden a real math font. In xelatex this can be done with FakeBold and version (as long as no other text fonts are involved). In lualatex you could use a pdfliteral:

\documentclass{article}
\usepackage{fontspec,amsmath}
\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Pagella Math}

\begin{document}

$\hat f(t) = \int_0^t \hat f'(x)\, dx$

\pdfliteral direct {2 Tr 0.2 w} %the second factor is the boldness

$\hat f(t) = \int_0^t \hat f'(x)\, dx$

\pdfliteral direct {0 Tr 0 w}%

\end{document}


• Well, it's the best solution so far, but I would still prefer if \boldmath simply activated either (preferably) the bold symbols in the standard math font (in the case they exist) or (alterantively) the bold symbols in the text font. But perhaps that requires changes in how unicode-math works. Nov 25 '15 at 19:52
• Imho there aren't suitable bold symbols in unicode. There is a bold "a", but not a bold int, sum, equal sign, prime, accents etc. What you suggest is a (not very good) patchwork. The only really good solution is a real bold math font. The second best a faked bold like in my answer. A patchwork solution should be only the last resort. So even if the unicode-math code is improved I don't think that it will a satisfactory output. Nov 25 '15 at 20:54

Unicode has defined the bold and bold italic (and others) mathematical alphanumeric characters covering latin, greek, and digit characters, so they are already included in an conforming OpenType mathematical font.

Here is an example using the bold range:

\documentclass{article}
\usepackage{fontspec,amsmath}
\usepackage{unicode-math}
\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
\begin{document}
$\hat f(t) = \int_0^t \hat f'(x)\, dx$
$\mathbf{\hat f}(t) = \int_0^t \mathbf{\hat f}'(x)\, dx$
\end{document}


A couple of notes. I remember there being an issue with accents with single-atom elements which can be fixed by prefixing the atom to be accented with an empty group (so, for example, \hat{{}f}, if need be).

Also, whether or not the bolded alphabets are in italic, can be determined by – according to the unicode-math documentation – the bold-style=ISO configuration option.

• Thanks for the answer. So there is no way to get \boldmath to automatically activate bold italic math? Jun 6 '15 at 19:01
• @Gaussler: Actually, I'm quite sure there is! It's just that I'm a little bit unfamiliar with unicode-math. (I mean, not necessarily with \boldmath, but a switch which activates the bold-italic range). I hope some of our LaTeX experts could chime in. Jun 6 '15 at 19:05
• Of course, it would be possible to redefine \boldmath to insert a hook in all math environments, activating \mathbfit. However, that seems like a hacky and undesirable solution. Jun 6 '15 at 19:07
• @Gaussler: I was thinking more in lines of defining another font-selecting command (so, say, \myboldmath would use the same font, but with the aforementioned ranges activated). The documentation speaks of [range=\mathsfit->\mathbfsfit], but for some reason I couldn't get it to work with [range=\mathit->\mathbfit]. Jun 6 '15 at 19:29
• @Gaussler \boldmath works perfectly, provided you have a bold math font. Unfortunately there is no TeX Gyre Pagella Math Bold. Jun 6 '15 at 19:38

Here is a solution that gets “\boldmath to automatically activate bold italic math,” as desired. It necessarily relies, however, as Ulrike Fischer mentioned on the presence of a “real bold math font.” (Hence, it cannot be achieved with TeX Gyre Pagella Math, because there is no TeX Gyre Pagella Math Bold, as already pointed out by egreg.)

To my knowledge, only three such fonts exist: XITSMath-Bold.otf (already mentioned in the comments, included in the standard TeX distributions, and used in my solution), the commercial font Minion Math (which even comes in four weights), and Fira Math (wich comes in as many as 12 weights but as of today has rather incomplete glyph coverage).

% !TEX program = xelatex

\documentclass{minimal}

\usepackage{ifthen}
\usepackage[svgnames]{xcolor}

\usepackage[
mathit = sym, mathup = sym, mathbf = sym,
math-style = ISO, bold-style = ISO
]{unicode-math}

\setmainfont{LinLibertine_R.otf}[
ItalicFont = LinLibertine_RI.otf,
BoldFont = LinLibertine_RB.otf,
BoldItalicFont = LinLibertine_RBI.otf
]
\setmonofont{FiraMono-Medium.otf}[
Scale = 0.81,
Color = DarkBlue
]

\setmathfont{XITSMath-Regular.otf}[
BoldFont = XITSMath-Bold.otf
]
\setmathfont{XITSMath-Regular.otf}[
version = mathitVersion
]
\setmathfont{XITSMath-Regular.otf}[
version = mathbfitVersion
]
\setmathfont{XITSMath-Bold.otf}[
version = boldmathVersion
]

\setmathfont{LinLibertine_R.otf}[
range = up/{num, latin, Latin, greek, Greek},
BoldFont = LinLibertine_RB.otf
]
\setmathfont{LinLibertine_RI.otf}[
range = it/{num, latin, Latin, greek, Greek},
BoldFont = LinLibertine_RBI.otf,
NFSSFamily = mathitalic
]
\setmathfont{LinLibertine_RBI.otf}[
range = bfit/{latin, Latin, greek, Greek},
NFSSFamily = mathbolditalic
]
\setmathfont{LinLibertine_RB.otf}[
range = bfup/{num, latin, Latin, greek, Greek}
]

\newif \ifBoldMath \BoldMathfalse
\let \mathversionorig \mathversion
\renewcommand{\mathversion}[1]{%
\ifthenelse{\equal{#1}{bold}}{%
\BoldMathtrue%
}{%
\BoldMathfalse%
}%
\mathversionorig{#1}%
}

\AtBeginDocument{%
\let \mathitorig \mathit%
\renewcommand{\mathit}[1]{%
\ifBoldMath%
\textbf{%
\SetSymbolFont{__um_fam1}{boldmathVersion}{TU}{mathbolditalic}{b}{it}%
\SetSymbolFont{__um_fam2}{boldmathVersion}{TU}{mathbolditalic}{b}{it}%
\mathversion{boldmathVersion}%
$$\mathitorig{#1}$$%
}
\else%
\textit{%
\SetSymbolFont{__um_fam1}{mathitVersion}{TU}{mathitalic}{m}{it}%
\mathversion{mathitVersion}%
$$\mathitorig{#1}$$%
}%
\fi%
}%
\let \mathbfitorig \mathbfit%
\renewcommand{\mathbfit}[1]{{%
\textbf{%
\SetSymbolFont{__um_fam1}{mathbfitVersion}{TU}{mathbolditalic}{b}{it}%
\mathversion{mathbfitVersion}%
$$\mathbfitorig{#1}$$%
}%
}}%
}

\newcommand{\testUnicodeMath}{%
\bigskip
Row
& Math version/alphabet
& Regular
& Upright
& Italic \\[\medskipamount]
1
& Text
& A = 1a
& \textup{B = 2b}
& \textit{C = 3c} \\
2 &
\texttt{\textbackslash textbf}
& \textbf{A = 1a}
& \textbf{\textup{B = 2b}}
& \textbf{\textit{C = 3c}} \\
3
& \texttt{\textbackslash mathversion\{normal\}} (default)
& $A = 1a + \beta \times \Gamma \int_{-\infty}^{\infty} \frac{\mathup{d}x}{2}$
& $\mathup{B = 2b + \beta \times \Gamma}$
& $\mathit{C = 3c + \beta \times \Gamma}$ \\
4
& \texttt{\textbackslash mathversion\{bold\}}/\texttt{\textbackslash boldmath}
& \boldmath $A = 1a + \beta \times \Gamma \int_{-\infty}^{\infty} \frac{\mathup{d}x}{2}$
& \boldmath $\mathup{B = 2b + \beta \times \Gamma}$
& \boldmath $\mathit{C = 3c + \beta \times \Gamma}$ \\
5
& \texttt{\textbackslash mathbf}/\texttt{\textbackslash mathbfup}/\texttt{\textbackslash mathbfit}
& $\mathbf{A = 1a + \beta \times \Gamma \int_{-\infty}^{\infty} \frac{\mathup{d}x}{2}}$
& $\mathbfup{B = 2b + \beta \times \Gamma}$
& $\mathbfit{C = 3c + \beta \times \Gamma}$ \\
\end{tabular}%
\bigskip
\bigskip
}

\begin{document}

\testUnicodeMath

\textit{Check that everything works properly by invoking it a~second time:}

\testUnicodeMath

\textit{Check that everything works properly by invoking it a~third time:}

\testUnicodeMath

\end{document}


This produces the following output—note that the “3” in table rows 3–5 is italic, in line with the formatting produced by the original LaTeX \mathit:

I also posted this solution in the different thread that Gaussler mentioned.

• I edited my solution because the previous version generated a “Too many symbol fonts declared” error when using \mathit repeatedly. Sep 20 '21 at 13:05
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 2, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9671196937561035, "perplexity": 5372.5085705125875}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-05/segments/1642320305242.48/warc/CC-MAIN-20220127072916-20220127102916-00557.warc.gz"}