To start with, I am only interested in solutions that work with pdfLaTeX
, as opposed to, say, those that only work with LuaLaTex
.
The 'narrow' problem
First, a preliminary note: some packages redefine the value that \f@series
assumes for bold series font: the 'standard value' is bx
, but if, for example, we load newtxtext
, then it is b
.
My narrow question is, how do I programmatically set a macro to the value that \f@series
has for bold text. But there is a caveat: the solution should work when plugged into certain other macros, which may or may not complicate things. So let me now describe these other macros.
An MWE in the context of which the solution should work
I have a macro, called \OptBm
, that applies \bm
to a math expression if the surrounding text is bold, and doesn't apply it if it the surrounding text is not bold.
Well, more precisely, it applies \bm
if the surrounding text is not in the default text weight for the document, and doesn't apply it if the surrounding text is in the default text weight for the document. My question is actually about how to make it so that the macro really does apply \bm
if and only if the surrounding text is truly bold; more on that below.
Here is an MWE that includes my macro:
\documentclass{memoir}
\usepackage{newtxtext}
\usepackage{bm}
\makeatletter
\newcommand{\Fseries}{\f@series} % for easier access to the content of \f@series
\edef\CharBx{\f@series}
% this means that \CharBx will be the string returned by \f@series for
% the default weight of the document
\newcommand{\OptBm}[1]{\ifx\f@series\CharBx#1\else\bm{#1}\fi}
\makeatother
\begin{document}
\tableofcontents
\section{Case $\protect\OptBm{\alpha>1}$}
\noindent This is when the surrounding text is not bold: \Fseries\ $\OptBm{\alpha}$
\noindent
{\bfseries This is when the surrounding text is bold: \Fseries\ $\OptBm{\alpha}$}
Note that $\alpha>1$ is boldfaced in the section title, but not boldfaced in the Table of Contents.
\end{document}
This is not an X-Y problem
You may complain that for the purposes of what I seem to want, it would be enough to use the optional argument to the \section
command. But sometimes this is not possible: for example, under RevTex4-2
, this optional argument is ignored (see this question).
Thus, my \OptBm
macro may be useful in cases where the following four circumstances hold: 1. a math expression appears in a section title, 2. the section title is bold in the body of the document, 3. the section title is not bold in the Table of Contents, and 4. we cannot use the optional argument of the \section
command to separately specify the title that goes to the Table of Contents .
Again, all four of these can happen, for example, if we use RevTex4-2
.
What I want the macro to do
Back to my macro. I admit that for what I actually need to do at this particular time, the \OptBm
macro as it is now is sufficient. Nevertheless, please bear with me.
What I had originally was this: I didn't load the newtxtext
package, and my macro was
\edef\CharBx{bx}
\newcommand{\OptBm}[1]{\ifx\f@series\CharBx\bm{#1}\else#1\fi}
But then when I loaded newtxtext
, I discovered that the \CharBx
definition had to be modified to \edef\CharBx{b}
. This is annoying, so I wanted to make it so that \CharBx
always holds the appropriate letter or letters. But no matter what I tried, I couldn't make that work. For example, \edef\CharBx{\bfseries \f@series}
doesn't work, nor does {\bfseries\edef\CharBx{\f@series}}
, nor anything else I tried.
In the end, I came up with the code in the MWE. However, as I said, what the code in the MWE does is apply \bm
if and only if the surrounding text is not in the default text weight for the document. But I want it to apply \bm
if and only if the surrounding text is bold. And these two are not equivalent if there are additional weights besides bold and medium. Note that, in general, it is not possible to know in advance (i.e. before the document preamble is loaded) which weights might be available; it all depends on what font we use.
Moreover, it is just annoying on its own that I don't know how to programmatically set a macro to the value that \f@series
has for bold text.
Summary
- Is there a solution to my 'narrow question'? Would it work in the context of my MWE?
- If there is no solution to my narrow question such that it still works in the context of my MWE, is there a different way to write the
\OptBm
macro such that it indeed applies\bm
if and only if the surrounding text is bold, even when we don't know in advance what other font weights are available?
Best Answer
If you only want to cover
b
versusm
thenwill do the job: you'll have a single letter which you can then test using for example
Things can be a lot more complex: you might have a variety of both text and math mode weights in play. For
siunitx
I need to cover all of this, allowing for the full range of text weights, etc., as listed in the linked question. That's a question of checking for the special cases (where the weight ism
but it's not in\f@series
), then taking the weight you find and matching it to a math version. That can involve a two-letter weight, of course. You just have to work systematically.