% content=tex
%
% copyright=pragma-ade readme=readme.pdf licence=cc-by-nc-sa

\startcomponent text-104

\chapter{Tuning math formulas}

\index {grid snapping+math}
\index {math+grid snapping}
\index {formulas}

Once a design gets driven by typesetting on a grid, a bit
more complex math becomes a problem. A naive designer may
test a design using some simple psuedo math, using a few
predefined superior characters |<|many fonts have \high a
superior {\txx 2} available or \frac {1} {2}|>| but even
simple school math is more than that. 

So, in order to typeset math at all, we have to choose a
proper math font collection. On the \TEX live distribution
you find the Computer Modern Roman (cmr), Times (tx) and
Palatino (px) math fonts. The first problem you need to
solve is the relative font scaling. If a font collection is
used, this is normally taken care of by the designer.
However, if you combine different shape designs, some more
work is needed. 

\startbuffer
\definetypeface [myface] [rm] [serif] [times]     [default]
\definetypeface [myface] [ss] [sans]  [helvetica] [default]
\definetypeface [myface] [tt] [mono]  [modern]    [default]
\definetypeface [myface] [mm] [math]  [palatino]  [default]
\stopbuffer

\typebuffer \getbuffer

Fonts scaled in a similar way (say to 10 points) seldom
look similar in size. You can test this with the macro:

\startbuffer
\showfontstrip[myface]
\stopbuffer

\typebuffer

This produced the table:

\getbuffer

A comfortable way out of this problem is relative font
scaling, as demonstrated in:

\startbuffer
\definetypeface
  [myface] [rm] [serif] [times]     [default]
\definetypeface
  [myface] [ss] [sans]  [helvetica] [default] [rscale=0.85]
\definetypeface
  [myface] [tt] [mono]  [modern]    [default] [rscale=1.05]
\definetypeface
  [myface] [mm] [math]  [palatino]  [default]
\stopbuffer

\typebuffer \getbuffer

We now get a comparable x||height, which is a reasonable
guarantee that the look and feel of a mixed font document
will be acceptable.

\showfontstrip[myface]

A next step is to determine the optimal baseline distance.
A good starting point is a distance of 120\% of the body
font size. This works out all right for in line math
typeset as \tform {1/2} or \tform {1/\sqrt{2}} but can be
disastrous when fractions are used: \gform {\frac {1}
{\sqrt{2}}}. Normally \TEX\ will take care of this by
increasing the baseline distance locally, but how to
explain this to grid thinking people. Here we keyed in:

\starttyping
... \tform {1/2} or \tform {1/\sqrt{2}} but can be disastrous
when fractions are used: \gform {\frac {1} {\sqrt{2}}} ...
\stoptyping

The \type {\tform} macro takes care of switching to math
mode, and \type {\gform} adds grid snapping to this. If you
cannot convince the designer of the style (or if you are in
control yourself) you may consider to increase the baseline
distance with a few points. In that case, a logical
question is: \quotation {How much should the baseline
distance be?}.

The answer is, ask \CONTEXT:

\startbuffer
\showminimalbaseline
\stopbuffer

\typebuffer

This produces:

\getbuffer

You can set the baseline distance of a typeface by adapting
its environment:

\starttyping
\setupbodyfontenvironment[myface][line=15.5pt]
\stoptyping

From now on, let's assume that everything is set up to our
needs. In the following examples, the third line is too high
to fit into the normal height and depth of a line. As a
result, when \type {\gform} is used, additional spacing is
applied.

\startbuffer
math \gform{\frac{1}{2}}
more math \gform{x^{\frac{1}{2}} + x^{2} + x^{2}}
nothing but math \gform{x^{\frac{1}{2}}_{\frac{1}{2}} + x^{2}}
\stopbuffer

\typebuffer

This gives:

\start 
\page[bigpreference]
\setuplines
  [before={\blank\starttextbackground[grid]},
   after=\stoptextbackground\blank]
\startlines
\getbuffer 
\stoplines
\stop

Because it is nearly impossible to determine how good or
bad things will look, additional space is added in line
quantities:

\startbuffer
In educational math books (for the lower grades) authors like
to use fractions, for example \gform {x^{\frac{1}{2}}_{\frac{1}{2}}
+ x^{2} + 2}, and although it may improve readability, it
definitely increases the amount of whitespace when using grids.
\stopbuffer

\typebuffer

\starttextbackground[grid] \getbuffer \stoptextbackground

\startbuffer
So, if this additional white space is too much for your
taste, you can instruct \CONTEXT\ to use half lines, as in:
\gform [-] {x\supsub{\frac{1}{2}}{\frac{1}{2}} + x\super{2}
+ 2}. You can turn on this feature for a whole document,
but in practice this kind of optimization is handy work.
\stopbuffer

\typebuffer

\starttextbackground[grid] \getbuffer \stoptextbackground

\starttyping
\setuptextformulas[step=halfline]
\stoptyping

This grid snap mechanism is only active when document grid
snapping is turned on. You can disable math snapping with:

\starttyping
\setuptextformulas[grid=no]
\stoptyping

An inline formula typeset with \type {\gform} will not
break across lines. However, you can compose a more complex
formula from multiple \type {\gform}. The following code
was used when developing this feature:

\startbuffer
Crazy math \gform {1+x} or \gform {\dorecurse {10} {1+} 1 =
11} and even more crazy \gform {2^{2^2}_{1_1}}
again\dorecurse {20} { and again} \gform {\sqrt {\frac
{x^{5^5}} {\frac {1} {2}}}} even more\dorecurse {10} { and
more} \tform {\dorecurse {12} {\gform {\sqrt {\frac
{x^{5^5}} {3}}}+\gform {\sqrt {\frac {x^{5^5}} {\frac {1}
{2}}}}+}x=10}\dorecurse {10} { super crazy math}: \tform
{\dorecurse {15} {\gform {\sqrt {\frac {x^{5^5}} {3}}}+
\gform {\sqrt {\frac {x^{5^5}} {\frac {1} {2}}}}+ }x = 10},
and we're\dorecurse {20} { done}!
\stopbuffer

\typebuffer

\starttextbackground[grid] \getbuffer \stoptextbackground

The space saving alternative, typeset with \type {step} set
to \type {halfline}, looks as follows: 

\starttextbackground[grid]
\setuptextformulas [step=halfline] \getbuffer
\stoptextbackground

So far we dealt with in||line math. For display math, we
can fall back on the predefined math environments, or
define our own. We distinguish four different kinds of
display math. These are defined with \type {\defineformula}
and can be setup with \type {\setupformula}.

\starttabulate[||||||]
\NC \NC single par \NC single disp \NC multipar \NC multi disp \NC \NR
\TB
\NC tag                \NC sp            \NC sd            \NC 
                           mp            \NC md            \NC \NR
\NC \type{spacebefore} \NC \type{none}   \NC \type{none}   \NC 
                                         \NC               \NC \NR
\NC \type{spaceafter}  \NC \type{none}   \NC \type{none}   \NC
                                         \NC               \NC \NR
\NC \type{indentnext}  \NC \type{no}     \NC \type{yes}    \NC 
                           \type{no}     \NC \type{yes}    \NC \NR
\NC \type{alternative} \NC \type{single} \NC \type{single} \NC 
                           \type{multi}  \NC \type{multi}  \NC \NR
\stoptabulate

These environments are used like:

\starttyping
\startsdformula
  x = 2 + 4x
\stopsdformula
\stoptyping

So, the environments are charcterized by a short two
character tag. The next examples demonstrate how they
differ in spacing and indentation. 


Single line display math: \fakewords {10} {15}

\startsdformula 
x = 2 + 4x 
\stopsdformula

\fakewords {10} {15} looks like this.

Single line paragraph math: \fakewords {10} {15}

\startspformula 
x = 2 + 4x 
\stopspformula

\fakewords {10} {15} looks like this.

Multi line display math: \fakewords {10} {15}

\startmdformula 
x = {{2 + 4x}\over{\sqrt{135}}} 
\stopmdformula

\fakewords {10} {15} looks like this.

Multi line paragraph math: \fakewords {10} {15}

\startmpformula 
x = {{2 + 4x} \over {\sqrt{135}}} 
\stopmpformula

\fakewords {10} {15} looks like this.

It is possible to influence grid placement with the
\type {\moveformula} command. 

You can for instance move the formula one line up by using
the \type {-top} directive. The previous multi line paragraph
example was codes as: 

\starttyping
\startmpformula
x = {{2 + 4x} \over {\sqrt{135}}}
\stopmpformula
\stoptyping

We now change the definition to:

\starttyping
\moveformula[-top]
\startmpformula
x = {{2 + 4x} \over {\sqrt{135}}}
\stopmpformula
\stoptyping

This time we get:

\moveformula[-top]

Multi line paragraph math: \fakewords {10} {15}
\startmpformula 
x = {{2 + 4x} \over {\sqrt{135}}} 
\stopmpformula
\fakewords {10} {15} looks like this.

\stopcomponent
