What does \mathgroup mean? How to use it?

I have a paragraph of lots of vectors symbols, so I need to type something like:

$\mathbf{a}\cdot\mathbf{b}=\frac{\mathbf{a}+\mathbf{b}}{\mathbf{a}\cdot\mathbf{v}}$

(just an example).

It is so inconvenient to write a \mathbf command everywhere. So I want to define a command that automatically distribute a \mathbf head into every atom symbol. How can I do this?

I seached a solution that close to it; everymath and set font for math used a \mathgroup:

\everymath{\mathbf{\xdef\mysf{\mathgroup\the\mathgroup\relax}}\displaystyle\mysf}

But I don't understand what it means and how it works. I also googled the usage and mechanism of \mathgroup, but can't get one. Could someone explain this commands to me? And how can I turn this command into a more flexible one, that allows me to disable the automatic \mathbf if I temporarily don't want it?

• \mathgroup is a synonym for \family. You definitely don't want to do like that, anyway. Define \vv to mean \mathbf (or whatever) and use \vv{a}\cdot\vv{b}. You don't want that $2x$ has a boldface “2”, do you? Feb 19 '17 at 17:20
• @egreg Can I use an if-else structure to filter only alphabets? Just works whenever the object is A-Z and a-z.
– Eric
Feb 19 '17 at 17:29
• you also (almost) never want \displaystyle in \everymath Feb 19 '17 at 17:36
• If you are willing to accept a reference to The TeXbook, the core of the mechanism is explained on pp. 154–155; \mathgroup is the LaTeX name for the \fam primitive. Keep in mind, however, that LaTeX2e adds the complexity of dynamic font allocation.
– GuM
Feb 19 '17 at 19:23

TeX allows one to say

$a+{\fam4 x}$

which will print a in the default math italic and x in the font assigned to math family 4. In plain TeX such assignments are static and family 4 is assigned the text italic font.

In LaTeX the assignments are dynamic, depending on the document and the math font package loaded. So one doesn't know in advance what family is assigned the boldface font; the assignment is made once and for all when the first \mathbf command is performed. This is in common with all macros defined with \DeclareMathAlphabet.

LaTeX also calls \family in a different way, namely \mathgroup; they are synonyms.

You could do

\AtBeginDocument{%
\sbox0{$\mathbf{\xdef\mathbfgroup{\the\mathgroup}}$}%
}

and the macro \mathbfgroup will hold the math group (family) number corresponding to \mathbf.

However, doing also \everymath{\mathgroup\mathbfgroup} will make all letters in math to appear boldface, together with numbers. This is definitely not what you want, I believe.

Example code.

\documentclass{article}

\AtBeginDocument{%
\sbox0{$\mathbf{\xdef\mathbfgroup{\the\mathgroup}}$}%
\everymath{\mathgroup\mathbfgroup\relax}%
}

\begin{document}

$2v+cw=u$

\end{document} As you see, you cannot distinguish between vectors and scalars; the correct formula should be typeset as

$\mathrm{2}v+\mathnormal{c}w=u$

and I'm very dubious whether you really want to do it.

Define a macro for vectors, such as

\newcommand{\vv}{\mathbf{#1}}

and type in your formula as

$2\vv{v}+c\vv{w}=\vv{u}$

The input is clearer and you can redefine \vv at will, in case you decide that, after all, vectors should be boldface italic.

If you really want all letters in math to be boldface by default, the strategy is

\DeclareSymbolFont{boldfaceletters}{OT1}{cmr}{bx}{n}
\DeclareSymbolFontAlphabet{\mathbf}{boldfaceletters}
\DeclareMathSymbol{a}{\mathalpha}{boldfaceletters}{a}
[...all letters...]
\DeclareMathSymbol{z}{\mathalpha}{boldfaceletters}{z}
\DeclareMathSymbol{A}{\mathalpha}{boldfaceletters}{A}
[...all letters...]
\DeclareMathSymbol{Z}{\mathalpha}{boldfaceletters}{Z}

with perhaps also the uppercase Greek, depending on your preferences. You can use \mathnormal or the other similar commands to change the shape.

• Unfortunately, I cannot upvote again after the edit… :-)
– GuM
Feb 19 '17 at 19:16
• +1 for the sentence: This is definitely not what you want Feb 19 '17 at 19:54

@egreg’s answer has already given the best possible advice concerning this problem; anyway, it might be worthwhile to remark that one can shield the digits from being boldfaced when \fam/\mathgroup is set to the value used by \mathbf simply by moving them from class 7 (\mathalpha, which is where the LaTeX kernel puts them) to class 0 (\mathord). For example:

\documentclass{article}

\newcommand*{\DeactivateMathAlpha}{%
% Use of e-TeX extension could also be avoided here:
\mathcode#1=\numexpr\mathcode#1-"7000 \relax
}
\newenvironment*{automathbf}{%
% A loop could also be used here:
\DeactivateMathAlpha{0}%
\DeactivateMathAlpha{1}%
\DeactivateMathAlpha{2}%
\DeactivateMathAlpha{3}%
\DeactivateMathAlpha{4}%
\DeactivateMathAlpha{5}%
\DeactivateMathAlpha{6}%
\DeactivateMathAlpha{7}%
\DeactivateMathAlpha{8}%
\DeactivateMathAlpha{9}%
\everymath{\mathgroup\mathbfgroup\relax}%
}{}
\AtBeginDocument{%
\sbox0{$\mathbf{\xdef\mathbfgroup{\the\mathgroup}}$}%
}

\begin{document}

This is the normal setting: $$2v+cw=u$$.

\begin{automathbf}
We are now inside an \texttt{automathbf} environment: $$2v+cw=u$$.
\end{automathbf}

Back to the normal setting: $$2v+cw=u$$.

\end{document}

Of course, one still has the problem of letters intended to denote scalars; however, it seems to me that the above code does literally what the question asked for.