# \IfBold-command not working with fontspec anymore

Following this example I created a command to check, if the bold font is currently selected.

This definitely used to work with fontspec some time ago (with TeXlive2018, if pdf-metadata is to be trusted), but not anymore (just ran tlmgr on my TeXlive2020 installation), as the following MWE shows:

%!TEX program = lualatex
\documentclass{scrbook}
\usepackage{etoolbox}
\usepackage{hyperref}

%-- Font setup
\usepackage[euler-digits]{eulervm}
\usepackage[no-math]{fontspec}
\defaultfontfeatures{Extension=.otf}
\setmainfont{XCharter} % <-- COMMENT THIS LINE!

%-- IfBold Command
\makeatletter
\newcommand*{\IfBold}{%
\ifx\f@series\my@test@bx
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\newcommand*{\my@test@bx}{bx}
\makeatother

%-- Define a command for K-theory
\newcommand{\Kay}{\IfBold{\ensuremath{\mathbold{K}}}{\ensuremath{K}}}
\robustify{\Kay}
\newcommand{\K}{\texorpdfstring{\Kay}{K}}

\begin{document}

\section*{I like \K-Theory}
This is the regular \K.
\textbf{This is \IfBold{bold}{non-bold} text.}
This is \IfBold{bold}{non-bold} text.

\end{document}


Is this a newly introduced bug or am I missing something?

• it is not a bug, it is a change in LaTeX, but I have no time now ... – Ulrike Fischer May 14 '20 at 9:33
• the easiest for now is to add \DeclareFontSeriesDefault[sf]{bf}{b} and to test for b: \newcommand*{\my@test@bx}{b}, but I will discuss a more generic solution to test for "bold context". – Ulrike Fischer May 14 '20 at 12:40

I have added \IfFontSeriesContextTF to the set of NFSS commands in the kernel so that one can reliably determine the font series context even if, say in one font "bold" means semi-bold or ...

The first argument is either bf (am I typsetting in \bfseries?) or md(am I typsetting in \mdseries?). It is an argument because one day there may be more series contexts available.

This is matching \DeclareFontSeriesDefault with which one can specify what "bold" means in \sffamilyand so forth.

The code will appear during the next days in a new upload of the latex-dev format and will appear officially in the fall release 2020.

For now you can use the code below in place of the one you had in your example (somewhat longer, I know). Once latex-dev is out all you need is the definition for \IfBold, i.e., one line and then use

lualatex-dev  myfile


to compile it.

%-- IfBold Command
\makeatletter

\DeclareRobustCommand\IfFontSeriesContextTF[1]{%
\expand@font@defaults
\@font@series@contextfalse
\def\requested@test@context{#1}%
\expandafter\edef\csname ??def@ult\endcsname{\f@family}%
\let\@elt\test@font@series@context
\@meta@family@list
\@elt{??}%
\let\@elt\relax
\if@font@series@context
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}

\def\test@font@series@context#1{%
\edef\reserved@a{\csname #1def@ult\endcsname}%
\ifx\f@family\reserved@a
\let\@elt\@gobble
\expandafter\ifx
\csname\requested@test@context series@#1\endcsname\f@series
\@font@series@contexttrue
\else
\expandafter\ifx
\csname\requested@test@context def@ult\endcsname\f@series
\@font@series@contexttrue
\fi\fi\fi
}

\newif\if@font@series@context

\newcommand\IfBold{\IfFontSeriesContextTF{bf}}

\makeatother


The result is:

Happy Texing

### Update 2020/05

An implementation of this is now added to the development format of LaTeX and can be tested with lualatex-dev etc. It will be available in the next main release in fall 2020.

• Sorry for an unrelated comment here. I would be really grateful if you could take a look at this question: tex.stackexchange.com/q/546846 I think you, as a xparse` developer can help. – antshar Jun 1 '20 at 20:12