# How can I set the \boldmath font with unicode-math?

I am trying to typeset a document in Linux Libertine, and I want to use the font for alphanumeric characters in math mode as well. I am using unicode-math, not mathspec, because I need to specify alphabets like mathup explicitly for compatibility with packages like hepnames. I did not find a way to do that with mathspec. The engine I use is XeLaTeX. I initially specify TeX Gyre Pagella Math as a complete math font for symbols etc. I want the subsequent declarations to overwrite the font for text characters as Libertine.

I find several problems, illustrated by the MWE below:

1. The \boldmath switch doesn't do anything.
2. Numbers are not rendered correctly in bold and italic, only letters.
3. \mathbf (the last "A = 1" in the MWE) is rendered in Pagella, not in Libertine at all.

I already tried adding /{latin,Latin,num} to the range declarations, without success. What am I missing here?

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont[%
Ligatures=TeX,
BoldFont=LinLibertine_RB.otf,
ItalicFont=LinLibertine_RI.otf,
BoldItalicFont=LinLibertine_RBI.otf]
{LinLibertine_R.otf}

\setmathfont{texgyrepagella-math.otf}

\setmathfont[range=\mathup]{LinLibertine_R.otf}
\setmathfont[range=\mathbf]{LinLibertine_RB.otf}
\setmathfont[range=\mathit]{LinLibertine_RI.otf}
\setmathfont[range=\mathbfit]{LinLibertine_RBI.otf}

\begin{document}
\begin{tabular}{lll}
Text     & A = 1               & \textit{B = 2}\\
Textbf   & \textbf{A = 1}      & \textbf{\textit{B = 2}}\\
Math     & $A = 1$             & $\mathit{B = 2}$\\
Boldmath & {\boldmath $A = 1$} & {\boldmath $\mathit{B = 2}$}\\
Mathbf   & $\mathbf{A = 1}$    & $\mathbfit{B = 2}$\\
\end{tabular}
\end{document}


Without font changes, everything looks OK:

\documentclass{minimal}

\begin{document}
\begin{tabular}{lll}
Text     & A = 1               & \textit{B = 2}\\
Textbf   & \textbf{A = 1}      & \textbf{\textit{B = 2}}\\
Math     & $A = 1$             & $\mathit{B = 2}$\\
Boldmath & {\boldmath $A = 1$} & {\boldmath $\mathit{B = 2}$}\\
\end{tabular}
\end{document}


• by default unicode-math sets up \boldmath not to switch fonts but rather to switch characters into the bold math alphabet range starting at U+1d400 in the current math font, so the appearance depends on the glyphs in that range. Apr 30 '15 at 11:06
• \boldmath is shorthand for \mathversion{bold} unicode-math allows a version=bold key in its font setting option to specify fonts to use in that case. Apr 30 '15 at 11:08
• I already looked ad version=bold. If I add lines like \setmathfont[range=\mathup,version=bold]{LinLibertine_RB.otf} or \setmathfont[range=\mathit,version=bold]{LinLibertine_RBI.otf}, all characters in math mode become bold/italic. I guess I'm doing it wrong.
– tg85
Apr 30 '15 at 11:26

I found a solution that works for me. I started playing around with version=bold, as David suggested, but that kept turning all my math mode text bold. In the end I switched from file names to system font names. (I wanted to avoid that initially, since I am working on the same document from several different machines.) With system font names, \boldmath worked immediately and automatically. I also found that \mathbfup must be specified instead of \mathbf.

Another problem with using system font names is that, for some reason, the semi-bold Libertine style is loaded per default as bold. That is the reason for the BoldFont specification in the MWE below.

The solution is not perfect. You can see that all numbers in math mode are rendered upright, even in \mathit. Also, the number in \mathbfit is not bold. All fonts are correct though, and it's good enough for my purposes.

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{unicode-math}

\setmainfont[
BoldFont=Linux Libertine Bold,
BoldItalicFont=Linux Libertine Bold Italic]
{Linux Libertine}

\setmathfont{TeX Gyre Pagella Math}

\setmathfont[range=\mathup,
BoldFont=Linux Libertine Bold,
BoldItalicFont=Linux Libertine Bold Italic]
{Linux Libertine}
\setmathfont[range=\mathit,
BoldFont=Linux Libertine Bold Italic]
{Linux Libertine Italic}
\setmathfont[range=\mathbfup]{Linux Libertine Bold}
\setmathfont[range=\mathbfit]{Linux Libertine Bold Italic}

\begin{document}
\begin{tabular}{lll}
~        &\ Regular             &\ Italic\\
Text     &\ A = 1               &\ \textit{A = 1}\\
Textbf   &\ \textbf{A = 1}      &\ \textbf{\textit{A = 1}}\\
Math     &\ $A = 1$             &\ $\mathit{A = 1}$\\
Boldmath &\ {\boldmath $A = 1$} &\ {\boldmath $\mathit{A = 1}$}\\
Mathbf   &\ $\mathbf{A = 1}$    &\ $\mathbfit{A = 1}$\\
\end{tabular}
\end{document}


tg85 noted two issues with the answer that he provided to his own question:

The solution is not perfect. You can see that all numbers in math mode are rendered upright, even in \mathit. Also, the number in \mathbfit is not bold. All fonts are correct though, and it's good enough for my purposes.

The unicode-math manual (Figure 7) states that the upright shape is actually chosen on purpose for numerals because they “should always be taken from the upright font even in the italic style.” In line with this statement, it seems that Unicode does not define slots for italic digits—while it does define slots for bold digits, sans-serif digits, etc. See https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode#Mathematical_Alphanumeric_Symbols_block.

I can understand, however, that one still wishes the typesetting produced by unicode-math to be consistent with the original LaTeX-based typesetting, where \mathit{1} produces an italic digit one. Therefore, inspired by tg85’s code, I came up with a solution that fixes these two issues (also works with LuaLaTeX):

% !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 row 3 is now italic, and it is bold italic in table rows 4 and 5:

Also note the contrast between \mathversion{bold}/\boldmath and \mathbf/\mathbfup/\mathbfit: the former also makes all operators, integrals, etc. bold, while the latter only affects letters and digits.

• I edited my solution because the previous version generated a “Too many symbol fonts declared” error when using \mathit repeatedly. Sep 20 at 13:04

Using the package amsbsy and \pmb seems to get the boldmath.

\documentclass{minimal}

\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{amsbsy}

\setmainfont[%
Ligatures=TeX,
BoldFont=LinLibertine_RB.otf,
ItalicFont=LinLibertine_RI.otf,
BoldItalicFont=LinLibertine_RBI.otf]
{LinLibertine_R.otf}

\setmathfont{texgyrepagella-math.otf}

\setmathfont[range=\mathup]{LinLibertine_R.otf}
\setmathfont[range=\mathbf]{LinLibertine_RB.otf}
\setmathfont[range=\mathit]{LinLibertine_RI.otf}
\setmathfont[range=\mathbfit]{LinLibertine_RBI.otf}

\begin{document}
\begin{tabular}{lll}
Text     & A = 1               & \textit{B = 2}\\
Textbf   & \textbf{A = 1}      & \textbf{\textit{B = 2}}\\
Math     & $A = 1$             & $\mathit{B = 2}$\\
Boldmath & { $\pmb{A = 1}$} & {$\pmb{\mathit{B = 2}}$}\\
Mathbf   & $\mathbf{A = 1}$    & $\mathbfit{B = 2}$\\
\end{tabular}
\end{document}


• This seems to produce a different (third?) font. imgur.com/Bw48aZc
– tg85
Apr 30 '15 at 11:20
• I do not see any different font! Apr 30 '15 at 11:24
• \pmb just prints three copies of the glyph slightly offset, it is not a true bold font. Apr 30 '15 at 12:14
• don't use \pmb unless you are desperate (and then consider not using it:-) Apr 30 '15 at 12:20

With xelatex or lualatex

\documentclass{standalone}
\usepackage{hepparticles}
\usepackage{hepnames}
\usepackage[math-style=ISO]{unicode-math}
\usepackage{libertinus}
\usepackage{array}
\begin{document}

\tabcolsep=10pt
\begin{tabular}{>{\ttfamily\textbackslash}lll}
normalfont & Regular           &\itshape Italic\\
textnormal & A = 1             & \textit{A = 1}\\
textbf     & \textbf{A = 1}    & \textbf{\textit{A = 1}}\\
mathup     & $\mathup{A} = 1$  & $A = 1$\\
symbf      & $\symbfup{A = 1}$ & $\symbfit{A = 1}$\\
mathbf     & $\mathbf{A = 1}$  & $\mathbfit{A = 1}$\\
\end{tabular}

\end{document}