[Tex/LaTex] What does \mathgroup mean? How to use it

math-mode

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?

Best Answer

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}

enter image description here

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}[1]{\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.