[Dev-luatex] Math styles

Javier Múgica javier at digi21.eu
Mon Feb 23 10:53:44 CET 2009


Here goes a long message...

Some time ago I had the following idea about math styles. I'll first
like to point that I was not just elucubratig about possible
extensions, but I felt the need for it with an actual example.

I was trying to imitate the way a book set its math. It is a common one I think:

-- The style within paragraphs is the same as that of displayed portions.
-- The numerator and denomiator of fractions come out in a smaller
size than the normal one, approximately what would be the \smallsize
of LaTeX.
-- Fractions within fractions appear at same size that the main fraction.
-- Exponents are set in a size that would be the script size of the
fractions text, and the same size is used bor both text-size basis and
fraction-size basis.
-- There is not a subsubscript size; i. e., all levels of
sub/superindexes are displayed at the same size.
-- There are two sizes for operators like \sum, but the selection of
one or the other is not based on display/text style, but on applying
to a big or small formula.

The main problem to solve here is that of the different sizes
normal/fraction/script. I found it impossible with current TeX. The
solution I devised is as follows:


TeX would be at every moment in a current style, as it does now (when
a math list is being converted into a horizontal list), but the set of
possible styles would not be a closed one. So TeX would at every
moment be in a current style in the same way as it has at every moment
outside math a current \font; and as fonts do, styles would also be
declared, but the '= file' part is not necessary. For example

\mathstyle\mystyle

Furthermore, neither the style changing would be hardcoded, and it
would be defined for every style to what style change for fractions
and to what style change for scripts. This would be two among other
"parameters" associated to each style (the others I explain below), in
the same way that each font has some parameters associated to it. For
example

\fractionstyle\mytextstyle=\myfracstyle  %Here \fractionstyle is the primitive
\indexstyle\mytextstyle=\myscriptstyle   %And here it obviously is \indexstyle

This parallels \fontdimen3\myfont= 3pt (say).

The set of triples \textfont<x>, \scriptfont<x>, \scriptscriptfont<x>
is now replaced by a set of singletons, namely, the fonts for the
style:

\mathstylefont0\mytextstyle=\tenrm
\mathstylefont1\mytextstyle=\teni
\mathstylefont2\mytextstyle=\tensy
\mathstylefont3\mytextstyle=\tenex
\newfam\itfam \def\it{\fam\itfam\tenit} % \it is family 4
\mathstylefont\itfam\mytextslyte=\tenit
etc.

There only remains to think, besides compatibiliy, what other
parameters are associated to every style. Here is the three ones I
found.

\displayedlimits\mytextstyle=1 %1=yes, 0=no. Whether limits are placed
at display position
                               %for operators marked with \displaylimits

\usesuccesorop\mutextstyle=0 %Use the successor (bigger) glyph for Op
symbols, if present.

\exponentshigher\mytextstyle=0 %Raise the exponents as in TeX display
sytle or ar in text style.
                               %There is a slight difference. Does not
apply if we are at a cramped style.

There need also be defined the initial style after $, after $$, and
for the equation tag:

\initialtextstyle=\mytextstyle
\initialdisplaystyle=\mydisplaystyle
\initialeqtagstyle=\mtextstyle  %or \mytagstyle if it is a different one

and the primitive \mathstyle would be analogous to \font when used as
argument to another command and when placed after \the; that is, it
would mean the current mathstyle. But unfortunately it would not get
expanded till TeX is converting the math list into a horizontal list,
since till that moment TeX does not know which is the current
style---I think Don's decision about the syntax of \over and its
relatives was not a fortunate one.
But I will use \currentmathstyle instead of \mathstyle for these uses,
in this message, because I find it clearer to read, even if it may
just be a question of getting used to it, as I had to get used to
\font meaning the current font.

The primitives \displaystyle and \textstyle, to which \eqtagstyle
could be added, would make the style change to the current values of
\initialtextstyle, \initialdisplaystyle and \initialeqtagstyle. For
\scriptstyle and \scriptscriptstyle see the note about compatibility
near the end of this message.

Finally, I had thought that the syntax of \mathstyle could allow an
optional equal sign to follow the style, and if it were present then
an existing style would have to follow it and the definitions of the
later would be copied to those of the former:

\mathstyle\tablemathstyle=\textstyle
%
% And now make a some modifications to \tablemathstyle

But rejected the idea because it can be done with \let, which would
still be needed anyway if we want to make the copy later, not at the
time the style is defined.



Appart from suiting my particular need, this also opens the ground to
easy and powerful configurations. For example, a \tablemathsytle could
be defined, with its two corresponding style-changes (for fractions
and indexes). As another example, LaTeX math versions would just be
different styles that the user may simply specify at the beginning of
the formula:

$$\BoldMathStyle \sum\frac{\fib(n)}{2^{n+1}}=1$$

(The actual LaTeX macros would be smarter, and BoldMathStyle would
change to a display or a text style according as the current style
would be a display-one or a text-one, of which LaTeX would also keep
track).


Now follows the complete defintions that mimic the default TeX
settings (omitting the definitions for font families >=4), that I
would place in the hypothetical luaplain.tex; next the definition for
the style I described at the beginning of this message, and finally
some notes addressing compatibility and another suggestion.

%%% TeX default settings for math %%%

\mathstyle\plaindisplaystyle
\mathstyle\plaintextstyle
\mathstyle\plainscriptstyle
\mathstyle\plainsscriptstyle

%Style changes.
\fractionstyle\plaindisplaystyle=\plaintextstyle
\indexstyle\plaindisplaystyle=\plainscriptstyle
%\fractionstyle\plaintextstyle=\plainscriptstyle  %These two will be/get defined
%\indexstyle\plaintextstyle=\plainscriptstyle     %some lines below
\fractionstyle\plainscriptstyle=\plainsscriptstyle
\indexstyle\plaindisplaystyle=\plainsscriptstyle
\fractionstyle\plainsscriptstyle=\plainsscriptstyle
\indexstyle\plaindissplaystyle=\plainsscriptstyle

%Fonts
\mathstylefont0\plaindisplaystyle=\tenrm
\mathstylefont1\plaindisplaystyle=\teni
\mathstylefont2\plaindisplaystyle=\tensy
\mathstylefont3\plaindisplaystyle=\tenex
%etc.

\let\plaintextstyle\plaindisplaystyle  %Thus we avoid the need to
write again the same set of fonts
\fractionstyle\plaintextstyle=\plainscriptstyle

\mathstylefont0\plainscriptstyle=\sevenrm
\mathstylefont1\plainscriptstyle=\seveni
\mathstylefont2\plainscriptstyle=\seveny
\mathstylefont3\plainscriptstyle=\tenex
%etc.

\mathstylefont0\plainsscriptstyle=\sevenrm
\mathstylefont1\plainsscriptstyle=\seveni
\mathstylefont2\plainsscriptstyle=\seveny
\mathstylefont3\plainsscriptstyle=\tenex
%etc.

%Parameters

\displayedlimits\plaindisplaystyle=1
\usesuccesorop\plaindisplaystyle=1
\exponentshigher\plaindisplaystyle=1

\displayedlimits\plaintextstyle=0
\usesuccesorop\plaintextstyle=0
\exponentshigher\plaintextstyle=0

\displayedlimits\plainscriptstyle=0
\usesuccesorop\plainscriptstyle=0
\exponentshigher\plainscriptstyle=0

\displayedlimits\plainsscriptstyle=0
\usesuccesorop\plainsscriptstyle=0
\exponentshigher\plainsscriptstyle=0

%Initial sytles, and we are done

\initialdisplaystyle=\plaindisplaystyle
\initialtextstyle=\plaintextstyle
\initialeqtagstyle=\plaintextstyle


%%% Another common setting for math %%%

\mathstyle\mathmain
\mathstyle\mfracstyle
\mathstyle\mscriptstyle

%Style changes.
\fractionstyle\mathmain=\mfracstyle
\indexstyle\mathmain=\mscriptstyle
\fractionstyle\mfracstyle=\mfracstyle
\indexstyle\mfracstyle=\mscriptstyle
\fractionstyle\mscriptstyle=\mscriptstyle
\indexstyle\mscriptstyle=\mscriptstyle

%Fonts
\mathstylefont0\mathmain=\tenrm
\mathstylefont1\mathmain=\teni
\mathstylefont2\mathmain=\tensy
\mathstylefont3\mathmain=\tenex
%etc.

\mathstylefont0\mfracstyle=\ninerm
\mathstylefont1\mfracstyle=\ninei
\mathstylefont2\mfracstyle=\ninesy
\mathstylefont3\mfracstyle=\tenex
%etc.

\mathstylefont0\mscriptstyle=\sixrm
\mathstylefont1\mscriptstyle=\sisxi
\mathstylefont2\mscriptstyle=\sixsy
\mathstylefont3\mscriptstyle=\tenex
%etc.

%Parameters

\displayedlimits\mathmain=1
\usesuccesorop\mathmain=0
\exponentshigher\mathmain=1

\displayedlimits\mfracstyle=1
\usesuccesorop\mfracstyle=0
\exponentshigher\mfracstyle=0

\displayedlimits\mscriptstyle=0
\usesuccesorop\mscriptstyle=0
\exponentshigher\mscriptstyle=0

%And a style for runing text, equal by default to mathmain

\mathstyle\mathintext=\mathamin
%\exponentshigher\mathintext=0    %Uncomment if your math inside
paragraphs do not usually
                                  %include fractions, it will look better.

%Initial sytles

\initialdisplaystyle=\mathmain
\initialtextstyle=\mathintext
\initialeqtagstyle=\mathmain

%Some additional definitions not present in plain but needed here

\mathchardef\Sum="1358	%Big Sum
\mathchardef\Prod="1359 %Big prod
%etc.


%%% Notes on backward compatibility %%%

The 0th measure taken for compatibility is that of not choosing for
the primitives some other names because they eather are alrady taken:
\displaystyle instead of \initialdisplaystyle, etc., or are likely to
have been definded by the users.
To the later may belong \mathstyle. But I thought better to keep
symmetry with respect to the \font primitive. The other alternative is
to split \mathstyle into \newmathstyle, for the declaration of styles,
and \currentmathstyle, anywhere else.

The 1st one is keeping the primitives \scriptstlye and
\scriptscriptstyle. Indeed, were we deriving the math typing anew, I
may define \scriptstyle as

\def\scriptstyle{\the\indexstyle\currentmathstyle}

and have \scriptscriptstyle not to exist at all.

But precisely because in the new scheme they are not necessary their
definition should be thought just in terms of backwards compatibility.
This concerns both their meaning and they being nonexpandable
primitives. The right meaning for \scriptstyle is to switch to the
\indexstyle of \intialdisplaystyle or \intialtextstyle, according as
TeX is at display or inner math mode; \scriptscriptstyle would switch
to the \initial(display/text)style's \indexstyle's \indexstyle, and
just exist for compatibility.

Giving to \scriptstyle the \the\indexstyle\currentmathstyle meaning,
even if still being a primitive, would work for old code only if this
style were required while TeX were at \displaystyle or \textstyle
(that now would be \plaindisplaystyle and \plaintextstyle), but would
fail if required from scriptstyle itsef or from the scriptscriptstyle.
Note that \scriptscriptstyle would not create any like problem if
defined like \the \currentstyle's \indexstyle's \indexstyle, for it
would always work with the default settings, wherever it is called
from.

Now regarding \textfont<x>, \scriptfont<x> and \scriptscriptfont<x>.
Again for compatibility, the meaning of the first would be to set the
font for both \initialdisplaystyle and \initialtextstyle, i. e.,
\mathstylefont<x>\initialdisplaystyle and \initialtextstyle; that of
the second to set \initial(display & text)style's \indexstyle
\mathstylefont<x>, and for the third the obvious analog.


%%% Style Stack %%%

Thinking about \scriptstyle, I found useful a macro that changed to
the \fracstyle, and also a \mainmathstyle:

\def\mainmathstyle{\ifinner\the\initialtextstyle
\else\the\initaldisplaystyle \fi}
\def\fracstyle{\the\fractionstyle\mainmathstyle}

But this is too rigid. It would not give the desired result if the user writes

$$\myotherstyle ... $$

this applies also to the meaning of \scriptsylte I suggested above.
For high level macro packages, like LaTeX or ConTeXt, this would most
certainly not be a problem since their respective teams would make the
high level user commands, like \BoldMathStyle above, to be smart
enough to redefine the necessary macros.
But I think that a solution that would be welcome by package writers
would be that TeX kept track of a small style stack, just for the
purposes of querying. For example, at the '2' of

\frac{n^2}{k^n}

the stack for the default plain settings would be

\plaindisplaystyle \plaintextstyle \plainscriptstyle %if in dysplayed math
\plaintextstyle \plainscriptstyle \plainsscriptstyle %if in in-text math

while for the other style whose definition I gave would be

\mathmain \mfracstyle \mscriptstyle

If a mathstyle command appears, thus causing TeX to switch to a
different style, it would replace the stack entry corresponding to the
where we are, which would always be the uppermost. Therefore it
appears to me that it would be easy to implement.

Now the definitions of \mainmathstyle... would be

\def\mainmathstyle{\the\mathstackstyle0 }
\def\fracstyle{\the\fractionstyle\mainmathstyle}  %But
\the\fractionstyle\mathstackstyle0
                                                  %would also work, in
the same way that
%\fontdimen\font is the same as \fontdimen\the\font. Furthermore, even
if \the\font is
%expandable while \font is not, both definitions would expand to the
same, if expanded,
%because the first \the causes complete expansion, just as
\edef\a{\the\fontdimen2\font}
%is the same as \edef\b{\the\fontdimen2\the\font}.

and the meaning of \scriptstyle (if not its defintion, for it need be
a nonexpandable primitive)

\the\scriptstyle\mainmathstyle

and the analogous one for \scriptscriptstyle, which preserves
compatibility while still producing the desired result.


If the number following \mathstackstyle were possitive it would refer
to the n'th style from the bottom, starting at the 0th; if it were
negative the count would start from the top of the stack, starting at
1.
So \the\mathstylestack-1 would be an elaborate form of \the\currentmathstyle.

If we won't let the styles in the stack to be changed then the
primitive \mathstylestack would only be allowed to appear after \the.
For coherence this would also apply to \mathstylestack-1, even if $
... \mathstylestack-1=\mystyle ... $ would mean the same as $ ...
\mystyle ... $, which does change de uppermost entry in the stack.



I hope that among all of the above you have found something worth considering

--Javier A.


More information about the dev-luatex mailing list