Newsgroups: comp.text.tex
Subject: Re: NFSS: How change mathcodes?
From: jeremy@cs.aukuni.ac.nz (Jeremy Gibbons)
Date: Tue, 26 Jan 1993 11:13:23 GMT
Organization: Computer Science Dept. University of Auckland
Keywords: NFSS, mathcode
Cc: jeremy@cs.aukuni.ac.nz

In <1993Jan20.161638.7096@uts.uni-c.dk> kubipdal@uts.uni-c.dk (Peter Dalg}rd) writes:

>b) Math alphabets do not seem to be defined unless there is reference 
>	to them in a formula (??) As it is now, $X_i$ gives me an
>	error that textfont 4 is undefined, but $X_i+\mathbf{z}$
>	works. How do I enforce definition of the relevant fonts?

Maths fonts in nfss come in two categories: those that are used purely as
alphabets, via macros succ as \mathbf, and those that are also (or purely)
used via mathcodes and mathchardefs. The former can be demand loaded, but
the latter have to be preloaded (because there is no obvious macro on which
to stick a "demand loading" hook). Hence you have to define the fonts
differently. 

Here is a reminder I wrote myself a while back, when I was trying to work
out how it all worked.


              --- Using new fonts in maths with the NFSS ---

Fonts used in maths fall into three categories: `symbol fonts', which are
used only via \mathchardefs (such as the AMS font `msam', which contains
only symbols), `character fonts', which are used only via `alphabet
identifiers' (such as the Euler script font `eusm', in its usual usage,
which contains only letters), and `mixed fonts', which used in both ways
(such as the AMS font `msbm', which contains both symbols and blackboard
bold letters).

Each font that you want to be able to use in maths mode---where symbols
should get smaller in subscripts and so on---must be allocated one of TeX's
16 precious maths `groups', as they are called in the terminology of the
NFSS. Maths groups are set aside by the macro \new@mathgroup, so we might
say

   \new@mathgroup\msa@group

to set aside a group \msa@group. If you find you have run out of maths
groups, you will have to decide which of the groups assigned by LaTeX you
can do without. LaTeX traditionally sets up 11 groups:

           0  roman (cmr)
           1  math italic (cmmi)
           2  symbols (cmsy)
           3  extensible characters (cmex)
   \bffam  4  bold (cmbx)
   \sffam  5  sans serif (cmss)
   \ttfam  6  typewriter (cmtt)
   \itfam  7  text italic (cmti)
   \scfam  8  small caps (cmcsc)
   \slfam  9  slanted (cmsl)
   \lyfam  10 LaTeX symbols (lasy)

The first four of these shouldn't be changed, unless you really know what
you're doing: TeX expects the first four groups to have certain properties.
However, it is unlikely that you will want all of the rest for one
document; if we decided that we never want typewriter text in maths mode,
we could reuse family 6 for the msa symbols by saying

   \let\msa@group\ttfam

Be warned: macros expecting typewriter text in family 6 will probably
produce msa symbols without generating any error messages!

Having chosen a maths group for a maths fonts, you need to tell TeX which
fonts to use for that group. This is done in different ways, depending on
whether the font is a symbol, character or mixed font.

The NFSS includes the concept of maths `versions'; this provides another
dimension along which fonts can be varied. Standard LaTeX sets up two
versions, `normal' and `bold'; these correspond to \unboldmath and
\boldmath in the old font selection scheme. Versions can be switched
mid-document, in which case all maths symbols also switch, from one version
to another.

Symbol fonts should be assigned to groups using the \define@mathgroup
macro. Suppose you want to use fonts from the `msa' family, `n' shape for
the group \msa@group, using the `m' series for the `normal' version and the
`b' series for the `bold' version; you should say

   \define@mathgroup\mv@normal\msa@group{msa}{m}{n}
   \define@mathgroup\mv@bold\msa@group{msa}{b}{n}

(The control sequence \mv@normal is the internal sequence corresponding to
the math version `normal'; this really should have been hidden, but Frank
and Rainer have not separated out the style-designer's interface from the
internal details.)

Most symbol fonts, though, including the msa fonts, come in only one
version; in this case the same family/series/shape combination should be
assigned for all versions:

   \define@mathgroup\mv@normal\msa@group{msa}{m}{n}
   \define@mathgroup\mv@bold\msa@group{msa}{m}{n}

All that remains to be done is to define macros to access the symbols from
such a font. The accepted way to do this is to temporarily define a control
sequence returning the number of the maths group in hexadecimal:

   \edef\@tempa{\hexnumber@\msa@group}
   \mathchardef\boxdot="2\@tempa00

That covers symbol fonts; character fonts are treated differently. They
should be assigned to maths groups using the macro \define@mathalphabet.
Suppose you want the macro \script to select the Euler script font, family
`eus', series `m', shape `n', in the normal maths version, and suppose that
Euler script has been assigned to group \eus@group; you should say

   \define@mathalphabet\mv@normal\script\eus@group{eus}{m}{n}

Actually, with this definition \script will have no effect and produce no
warning when used in text mode; really it should check whether it is being
used in maths mode:

   \def\script{\protect\pscript}
   \def\pscript{\relax\protect\ifmmode\@script\else\nonmatherr@\script\fi}
   \define@mathalphabet\mv@normal\@script\eus@group{eus}{m}{n}

Mixed fonts which are used both for symbols and for characters, such as the
AMS symbol font `msb' which contains more symbols and blackboard bold
letters, are slightly more complicated. You could just use *both*
\define@mathgroup and \define@mathalphabet:

   \define@mathgroup\mv@normal\msb@group{msb}{m}{n}
   \define@mathalphabet\mv@normal\bbold\msb@group{msb}{m}{n}

but this involves giving the family/series/shape information twice, with
all the problems of duplication of information that that entails. A
slightly more elegant solution is provided by \use@mathgroup:

   \edef\bbold{\noexpand\use@mathgroup
      \expandafter\noexpand\csname =msb\endcsname\msb@group}

(The \noexpands and \expandafters are just hacks to get the effect that the
definition 

   \def\bbold{\use@mathgroup\=msb\msb@group}

would have if `=' were a letter.)

Again, such a definition should check whether it is being used in maths
mode, and hence should be protected:

   \def\bbold{\protect\pbbold}
   \def\pbbold{\relax\protect\ifmmode\@bbold\else\nonmatherr@\bbold\fi}
   \edef\@bbold{\noexpand\use@mathgroup
      \expandafter\noexpand\csname =msb\endcsname\msb@group}

Here, \nonmatherr@ is a useful macro taken from amsfonts.sty:

   \@ifundefined{nonmatherr@}{
     \def\nonmatherr@#1{\err@{\string#1\space allowed only in math mode}\@ehd}
     \def\err@{\@latexerr}
   }{}


Jeremy

---
Jeremy Gibbons <jeremy@cs.aukuni.ac.nz>         tel: +64 9 373 7599
   Department of Computer Science,              fax: +64 9 373 7453
   University of Auckland, Private Bag 92019, Auckland, New Zealand.