
\setupinteraction[state=start]

\setuphead[subject][
    style={\bfa\WORD},
    align=middle,
]

% \showglyphs
% \showmakeup[mathglue]
% \showboxes

\setupbodyfont[bonum]

\definereferenceformat[eqref][left=(,right=)]
% \showframe


\definemathsimplealign[collectedrightnumbers][
    left={\startmathfenced[sesac]},
    right=\stopmathfenced,
    align={1:right,2:left},
    strut=yes,
]

\definemathsimplealign[collectedleftnumbers][
    left={\startmathfenced[cases]},
    right=\stopmathfenced,
    align={1:right,2:left},
    strut=yes,
]


\starttext


\startbuffer[samples]

Just a simple formula (as before):

\startplaceformula
\startformula
f(x) = \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n
\stopformula
\stopplaceformula

A chain formula that runs over several lines. One formula, numbered at the end.

\startplaceformula
\startformula
f(x) \alignhere = \exp(x) \breakhere
                = 1 + x + x^2/2 + \ldots \breakhere
                = \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \breakhere
                = f'(x)
\stopformula
\stopplaceformula

As above, but with \tex{skiphere} at one place:
\startplaceformula
\startformula
f(x) \alignhere = \exp(x) \breakhere
                = 1 + x + x^2/2 + x^3/6 + x^4/24 \breakhere
                \skiphere[1em] + x^5/120 + x^6/720 \ldots \breakhere
                = \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \breakhere
                = f'(x)
\stopformula
\stopplaceformula

With \typ{align=slanted} we get first line flush left, last one flush right and the rest midaligned:
\startplaceformula
\startformula[align=slanted]
f(x) = \exp(x) = 1 + x + x^2/2 + x^3/6 + x^4/24 + \ldots \breakhere
                = \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n = f'(x)
\stopformula
\stopplaceformula


Don't do this (unclear what the number points to):
\startplaceformula[eq:all]
\startformula
\startalign
  \NC f(x) \EQ \exp(x)                                          \NR
  \NC      \EQ 1 + x + x^2/2 + \ldots                           \NR
  \NC      \EQ \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \NR
  \NC      \EQ f'(x)                                            \NR
\stopalign
\stopformula
\stopplaceformula

Better to brace the formula and give one number:


\doifmodeelse{leftnumbers}{
\startplaceformula
\startformula
\startcollectedleftnumbers
  \NC f(x) = \NC \exp(x)                                          \NR
  \NC      = \NC 1 + x + x^2/2 + \ldots                           \NR
  \NC      = \NC \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \NR
  \NC      = \NC f'(x)                                            \NR
\stopcollectedleftnumbers
\stopformula
\stopplaceformula
}{
\startplaceformula
\startformula
\startcollectedrightnumbers
  \NC f(x) = \NC \exp(x)                                          \NR
  \NC      = \NC 1 + x + x^2/2 + \ldots                           \NR
  \NC      = \NC \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \NR
  \NC      = \NC f'(x)                                            \NR
\stopcollectedrightnumbers
\stopformula
\stopplaceformula
}

% When the formula number is to the left, we should put the brace to the left):



If one needs to number several lines:
\startplaceformula
\startformula
\startalign
  \NC f(x) \EQ \exp(x)                                          \NR[eq:X]
  \NC      \EQ 1 + x + x^2/2 + \ldots                           \NR[eq:Y]
  \NC      \EQ \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \NR
  \NC      \EQ f'(x)                                            \NR[eq:Z]
\stopalign
\stopformula
\stopplaceformula






\stopbuffer


\subject[title={Numbers to the right}]
\getbuffer[samples]

\subject[title={Numbers to the left}]
\bgroup
\enablemode[leftnumbers]
We reuse the same formulas.
\setupformula[location=left]
\getbuffer[samples]
\disablemode[leftnumbers]
\egroup


\subject[title={Subformulas}]
We can get subnumbering with letters:

\startplaceformula
\startformula
\startalign
  \NC f(x) \EQ \exp(x)                                          \NR[eq:a][a]
  \NC      \EQ 1 + x + x^2/2 + \ldots                           \NR[eq:b][b]
  \NC      \EQ \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \NR[eq:c][c]
  \NC      \EQ f'(x)                                            \NR
\stopalign
\stopformula
\stopplaceformula

If we want no dot between we can do

\startbuffer
\defineseparatorset[none][][]
\setupformulas[numberseparatorset=none]
\stopbuffer

\getbuffer
\typebuffer

\startplaceformula
\startformula
\startalign
  \NC f(x) \EQ \exp(x)                                          \NR[eq:A][a]
  \NC      \EQ 1 + x + x^2/2 + \ldots                           \NR[eq:B][b]
  \NC      \EQ \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \NR[eq:C][c]
  \NC      \EQ f'(x)                                            \NR
\stopalign
\stopformula
\stopplaceformula

See \eqref[eq:B].

\subject[title={Breaking fences}]
Fences can break over lines:

\startbuffer[brokenfences]
\startplaceformula
\startformula[textdistance=2em]
  \alignhere 
    P'(iy_1, \ldots, iy_n, i\eta_k) 
  \breakhere
  = (ir)^{k-1} \left[ 
    P_k' \left( z_ 1, \ldots, z_n, \frac{\eta_k}{r} \right) 
    \breakhere
    \skiphere[3] 
    + \frac{1}{ir}P_{k-1}'
      \left( z_ 1, \ldots, z_n, \frac{\eta_k}{r} \right)
    + \ldots
    \right] 
  \breakhere
  = (ir)^{k-1} 
      P_k'\left( z_ 1, \ldots, z_n, \frac{\eta_k}{r} \right) 
    + O(r^{k-2})
\stopformula
\stopplaceformula
\stopbuffer

\getbuffer[brokenfences]
Note that the alignment point is to the left of \im{P}. Then all lines are moved in the amount of \typ{textdistance}, which is set to 2em.
Here the \typ{\skiphere[3]} skips four units of \typ{textmargin}, which by default is set to 2em. In this case we could have used \typ{\skiphere[6em]}


\subject[title={Comments}]

We can add comments in the margin:

\startformula
\startalign[text:1={Foo},text:2={Bar}]
\NC x \EQ r \cos\theta \NR
\NC y \EQ r \sin\theta \NR
\stopalign
\stopformula

Likewise,

\startformula
\texthere[left]{Foo} 
f(x) \alignhere = \exp(x) \breakhere[left]{Bar}
                = 1 + x + x^2/2 + \ldots \breakhere
                = \lim_{n\to+\infty}\left(1 + \frac{x}{n}\right)^n \breakhere[right]{Oh no!}
                = f'(x)
\stopformula

\subject[title={Aligns with several columns}]

\startbuffer[aligns]
\startformula
\startalign[m=3, n=2,distance=2em]
\NC 1 \EQ 2 \NC 3 \EQ 1 \NC 2 \EQ 3 \NR
\NC 4 \EQ 5 \NC 6 \EQ 4 \NC 5 \EQ 6 \NR
\stopalign
\stopformula

\startformula
\mframed[offset=overlay,frame=off,leftframe=on,bottomframe=on]{
\startalign[m=3, n=2,distance=2em]
\NC 1 \EQ 2 \NC 3 \EQ 1 \NC 2 \EQ 3 \NR
\NC 4 \EQ 5 \NC 6 \EQ 4 \NC 5 \EQ 6 \NR
\stopalign
}
\stopformula

\startformula
\startalign[m=3, n=2,distance=0em plus 1 fil]
\NC 1 \EQ 2 \NC 3 \EQ 1 \NC 2 \EQ 3 \NR
\NC 4 \EQ 5 \NC 6 \EQ 4 \NC 5 \EQ 6 \NR
\stopalign
\stopformula


\startformula
\mframed[offset=overlay,frame=off,leftframe=on,bottomframe=on]{
\startalign[m=3, n=2,distance=0em plus 1 fil]
\NC 1 \EQ 2 \NC 3 \EQ 1 \NC 2 \EQ 3 \NR
\NC 4 \EQ 5 \NC 6 \EQ 4 \NC 5 \EQ 6 \NR
\stopalign
}
\stopformula
\stopbuffer

\getbuffer[aligns]

\subject[title={Vertical distance in formulas}]
\startbuffer[spacesetup]
\setupformula[
    spaceinbetween=line,
]
\stopbuffer
We can do
\typebuffer[spacesetup]
\getbuffer[spacesetup]

Then the formula above with broken fences look like this:

\getbuffer[brokenfences]

This also affects the align and simplealign:
\getbuffer[aligns]


\startbuffer[alignspacesetup]
\setupmathalignment[
    spaceinbetween=2cm,
]
\stopbuffer
We can set the vertical space in aligns independently:
\typebuffer[alignspacesetup]
\getbuffer[alignspacesetup]

Then the aligns above look like this:

\getbuffer[aligns]
\stoptext
