summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/hybrid/hybrid-mathml.tex
blob: 3510f5321e6ea418dd6ccd35c663c59bdc34dc87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
% language=uk

% \enabletrackers[structures.export]
% \setupbackend[export=yes]

\usemodule[mathml] % also loads calcmath

\startcomponent hybrid-mathml

\environment hybrid-environment

\startchapter[title={Exporting math}]

\startsection [title={Introduction}]

As \CONTEXT\ has an \XML\ export feature and because \TEX\ is often strongly
associated with math typesetting, it makes sense to take a look at coding and
exporting math. In the next sections some aspects are discussed. The examples
shown are a snaphot of the possibilities around June 2011.

\stopsection

\startsection [title={Encoding the math}]

In \CONTEXT\ there are several ways to input math. In the following example we
will use some bogus math with enough structure to get some interesting results.

The most natural way to key in math is using the \TEX\ syntax. Of course you need
to know the right commands for accessing special symbols, but if you're familiar
with a certain domain, this is not that hard.

\startbuffer
\startformula
  \frac { x \geq 2 } { y \leq 4 }
\stopformula
\stopbuffer

\typebuffer \getbuffer

When you have an editor that can show more than \ASCII\ the following also works
out well.

\starttyping
\startformula
  \frac { x ≥ 2 } { y ≤ 4 }
\stopformula
\stoptyping

One can go a step further and use the proper math italic alphabet but there are
hardly any (monospaced) fonts out there that can visualize it.

\starttyping[escape=yes]
\startformula
  \frac { /BTEX\it x/ETEX ≥ 2 } { /BTEX\it y/ETEX ≤ 4 }
\stopformula
\stoptyping

Anyhow, \CONTEXT\ is quite capable of remapping the regular alphabets onto the
real math ones, so you can stick to \type {x} and \type {y}.

Another way to enter the same formula is by using what we call calculator math.
We came up with this format many years ago when \CONTEXT\ had to process student
input using a syntax similar to what the calculators they use at school accept.

\startbuffer
\startformula
  \calcmath{(x >= 2)/(y <= 4)}
\stopformula
\stopbuffer

\typebuffer \getbuffer

As \CONTEXT\ is used in a free and open school math project, and because some of
our projects mix \MATHML\ into \XML\ encoded sources, we can also consider using
\MATHML. The conceptually nicest way is to use content markup, where the focus is
on meaning and interchangability and not on rendering. However, we can render it
quite well. OpenMath, now present in \MATHML~3 is also supported.

\startbuffer
<math xmlns='http://www.w3c.org/mathml' version='2.0'>
  <apply> <divide/>
    <apply> <geq/> <ci> x </ci> <cn> 2 </cn> </apply>
    <apply> <leq/> <ci> y </ci> <cn> 4 </cn> </apply>
  </apply>
</math>
\stopbuffer

\typebuffer \processxmlbuffer

In practice \MATHML\ will be coded using the presentational variant. In many
aspects this way of coding is not much different from what \TEX\ does.

\startbuffer
<math xmlns='http://www.w3c.org/mathml' version='2.0'>
  <mfrac>
    <mrow> <mi> x </mi> <mo> &geq; </mo> <mn> 2 </mn> </mrow>
    <mrow> <mi> y </mi> <mo> &leq; </mo> <mn> 4 </mn> </mrow>
  </mfrac>
</math>
\stopbuffer

\typebuffer \processxmlbuffer

When we enable \XML\ export in the backend of \CONTEXT, all of the above variants
are converted into the following:

% <m:math display="block">
%   <m:mrow>
%     <m:mfrac>
%       <m:mrow>
%         <m:mi>𝑥</m:mi>
%         <m:mo>≥</m:mo>
%         <m:mn>2</m:mn>
%       </m:mrow>
%       <m:mrow>
%         <m:mi>𝑦</m:mi>
%         <m:mo>≤</m:mo>
%         <m:mn>4</m:mn>
%       </m:mrow>
%     </m:mfrac>
%   </m:mrow>
% </m:math>

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mfrac>
      <m:mrow>
        <m:mi>/BTEX\it x/ETEX</m:mi>
        <m:mo>≥</m:mo>
        <m:mn>2</m:mn>
      </m:mrow>
      <m:mrow>
        <m:mi>/BTEX\it y/ETEX</m:mi>
        <m:mo>≤</m:mo>
        <m:mn>4</m:mn>
      </m:mrow>
    </m:mfrac>
  </m:mrow>
</m:math>
\stoptyping

This is pretty close to what we have entered as presentation \MATHML. The main
difference is that the (display or inline) mode is registered as attribute and
that entities have been resolved to \UTF. Of course one could use \UTF\ directly
in the input.

\stopsection

\startsection [title={Parsing the input}]

In \TEX\ typesetting math happens in two stages. First the input is parsed and
converted into a so called math list. In the following case it's a rather linear
list, but in the case of a fraction it is a tree.

\startbuffer
\startformula
  x = - 1.23
\stopformula
\stopbuffer

\typebuffer \getbuffer

A naive export looks as follows. The sequence becomes an \type {mrow}:

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mo>=</m:mo>
    <m:mo>−</m:mo>
    <m:mn>1</m:mn>
    <m:mo>.</m:mo>
    <m:mn>2</m:mn>
    <m:mn>3</m:mn>
  </m:mrow>
</m:math>
\stoptyping

However, we can clean this up without too much danger of getting invalid output:

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mo>=</m:mo>
    <m:mo>−</m:mo>
    <m:mn>1.23</m:mn>
  </m:mrow>
</m:math>
\stoptyping

This is still not optimal, as one can argue that the minus sign is part of the
number. This can be taken care of at the input end:

\startbuffer
\startformula
  x = \mn{- 1.23}
\stopformula
\stopbuffer

\typebuffer

Now we get:

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mo>=</m:mo>
    <m:mn>−1.23</m:mn>
  </m:mrow>
</m:math>
\stoptyping

Tagging a number makes sense anyway, for instance when we use different numbering
schemes:

\startbuffer
\startformula
  x = \mn{0x20DF} = 0x20DF
\stopformula
\stopbuffer

\typebuffer

We get the first number nicely typeset in an upright font but the second one
becomes a mix of numbers and identifiers:

\getbuffer

This is nicely reflected in the export:

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mo>=</m:mo>
    <m:mn>0x20DF</m:mn>
    <m:mo>=</m:mo>
    <m:mn>0</m:mn>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mn>20</m:mn>
    <m:mi>/BTEX\it D/ETEX</m:mi>
    <m:mi>/BTEX\it F/ETEX</m:mi>
  </m:mrow>
</m:math>
\stoptyping

In a similar fashion we can use \type {\mo} and \type {\mi} although these are
seldom needed, if only because characters and symbols already carry these
properties with them.

\stopsection

\startsection [title={Enhancing the math list}]

When the input is parsed into a math list the individual elements are called
noads. The most basic noad has pointers to a nucleus, a superscript and a
subscript and each of them can be the start of a sublist. All lists (with more
than one character) are quite similar to \type {mrow} in \MATHML. In the export
we do some flattening because otherwise we would get too many redundant \type
{mrow}s, not that it hurts but it saves bytes.

\startbuffer
\startformula
  x_n^2
\stopformula
\stopbuffer

\typebuffer

This renders as:

\getbuffer

And it gets exported as:

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:msubsup>
      <m:mi>/BTEX\it x/ETEX</m:mi>
      <m:mi>/BTEX\it n/ETEX</m:mi>
      <m:mn>2</m:mn>
    </m:msubsup>
  </m:mrow>
</m:math>
\stoptyping

As said, in the math list this looks more or less the same: we have a noad with a
nucleus pointing to a math character (\type {x}) and two additional pointers to
the sub- and superscripts.

After this math list is typeset, we will end up with horizontal and vertical
lists with glyphs, kerns, glue and other nodes. In fact we end up with what can
be considered regular references to slots in a font mixed with positioning
information. In the process the math properties gets lost. This happens between
step~3 and~4 in the next overview.

\starttabulate[|l|l|l|]
\NC 1  \NC \XML  \NC optional alternative input \NC \NR
\NC 2  \NC \TEX  \NC native math coding \NC \NR
\NC 3  \NC noads \NC intermediate linked list / tree \NC \NR
\NC 4  \NC nodes \NC linked list with processed (typeset) math \NC \NR
\NC 5a \NC \PDF  \NC page description suitable for rendering \NC \NR
\NC 5b \NC \XML  \NC export reflecting the final document content \NC \NR
\stoptabulate

In \CONTEXT\ \MKIV\ we intercept the math list (with noads) and apply a couple of
manipulations to it, most noticeably relocation of characters. Last in the
(currently some 10) manipulation passes over the math list comes tagging. This
only happens when the export is active or when we produce tagged pdf. \footnote
{Currently the export is the benchmark and the tagged \PDF\ implementation
follows, so there can be temporary incompatibilities.}

By tagging the recognizable math snippets we can later use those persistent
properties to reverse engineer the \MATHML\ from the input.

\stopsection

\startsection [title={Intercepting the typeset content}]

When a page gets shipped out, we also convert the typeset content to an
intermediate form, ready for export later on. Version 0.22 of the exporter has a
rather verbose tracing mechanism and the simple example with sub- and superscript
is reported as follows:

\starttyping[escape=yes]
<math-8 trigger='268' index='1'>
  <mrow-20 trigger='268' index='1'>
    <msubsup-1 trigger='268' index='1'>
      <mi-15 trigger='268' index='1'>
        <!-- processing glyph 2 (tag 270) -->
        <!-- moving from depth 11 to 11 (mi-15) -->
        <!-- staying at depth 11 (mi-15) -->
        <!-- start content with length 4 -->
        /BTEX\it x/ETEX
        <!-- stop content -->
        <!-- moving from depth 11 to 11 (mn-13) -->
      </mi-15>
      <mn-13 trigger='270' index='2'>
        <!-- processing kern > threshold (tag 270 => 267)
        <!-- moving from depth 11 to 11 (mn-13) -->
        <!-- staying at depth 11 (mn-13) -->
        <!-- start content with length 1 -->
        2
        <!-- stop content -->
        <!-- injecting spacing 9 -->
        <!-- moving from depth 11 to 10 (msubsup-1) -->
      </mn-13>
    </msubsup-1>
    <!-- processing glyph 𝑛 (tag 269) -->
    <!-- moving from depth 9 to 10 (msubsup-1) -->
    <msubsup-1 trigger='267' index='2'>
      <!-- start content with length 1 -->

      <!-- stop content -->
    </msubsup-1>
    <!-- moving from depth 9 to 11 (mi-16) -->
    <msubsup-1 trigger='269' index='3'>
      <mi-16 trigger='269' index='1'>
        <!-- processing glue > threshold (tag 269 => 262) -->
        <!-- moving from depth 11 to 11 (mi-16) -->
        <!-- staying at depth 11 (mi-16) -->
        <!-- start content with length 4 -->
        /BTEX\it n/ETEX
        <!-- stop content -->
        <!-- injecting spacing 6 -->
        <!-- moving from depth 11 to 6 (formula-8) -->
      </mi-16>
    </msubsup-1>
  </mrow-20>
</math-8>
\stoptyping

This is not yet what we want so some more effort is needed in order to get proper
\MATHML.

\stopsection

\startsection [title={Exporting the result}]

The report that we showed before representing the simple example with super- and
subscripts is strongly related to the visual rendering. It happens that \TEX\
first typesets the superscript and then deals with the subscript. Some spacing is
involved which shows up in the report between the two scripts.

In \MATHML\ we need to swap the order of the scripts, so effectively we need:

\starttyping[escape=yes]
<math-8 trigger='268' index='1'>
  <mrow-20 trigger='268' index='1'>
    <msubsup-1 trigger='268' index='1'>
      <mi-15 trigger='268' index='1'>
        /BTEX\it x/ETEX
      </mi-15>
      <mi-16 trigger='269' index='2'>
        /BTEX\it n/ETEX
      </mi-16>
      <mn-13 trigger='270' index='3'>
        2
      </mn-13>
    </msubsup-1>
  </mrow-20>
</math-8>
\stoptyping

This swapping (and some further cleanup) is done before the final tree is written
to a file. There we get:

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:msubsup>
      <m:mi>/BTEX\it x/ETEX</m:mi>
      <m:mi>/BTEX\it n/ETEX</m:mi>
      <m:mn>2</m:mn>
    </m:msubsup>
  </m:mrow>
</m:math>
\stoptyping

This looks pretty close to the intermediate format. In case you wonder with how
much intermediate data we end up, the answer is: quite some. The reason will be
clear: we intercept typeset output and reconstruct the input from that, which
means that we have additional information travelling with the content. Also, we
need to take crossing pages into account and we need to reconstruct paragraphs.
There is also some overhead in making the \XML\ look acceptable but that is
neglectable. In terms of runtime, the overhead of an export (including tagging)
is some 10\% which is not that bad, and there is some room for optimization.

\stopsection

\startsection[title={Special treatments}]

In content \MATHML\ the \type {apply} tag is the cornerstone of the definition.
Because there is enough information the rendering mechanism can deduce when a
function is applied and act accordingly when it comes to figuring out the right
amount of spacing. In presentation \MATHML\ there is no such information and
there the signal is given by putting a character with code \type {U+2061} between
the function identifier and the argument. In \TEX\ input all this is dealt with
in the macro that specifies a function but some ambiguity is left.

Compare the following two formulas:

\startbuffer
\startformula
  \tan = \frac { \sin } { \cos }
\stopformula
\stopbuffer

\typebuffer \getbuffer

In the export this shows up as follows:

\starttyping
<m:math display="block">
  <m:mrow>
    <!-- begin function -->
      <m:mi>tan</m:mi>
    <!-- end function -->
    <m:mo>=</m:mo>
    <m:mrow>
      <m:mfrac>
        <m:mrow>
          <!-- begin function -->
            <m:mi>sin</m:mi>
          <!-- end function -->
        </m:mrow>
        <m:mrow>
          <!-- begin function -->
            <m:mi>cos</m:mi>
          <!-- end function -->
        </m:mrow>
      </m:mfrac>
    </m:mrow>
  </m:mrow>
</m:math>
\stoptyping

Watch how we know that \type {tan} is a function and not a multiplication of the
variables \type {t}, \type{a} and~\type {n}.

In most cases functions will get an argument, as in:

\startbuffer
\startformula
  \tan (x) = \frac { \sin (x) } { \cos (x) }
\stopformula
\stopbuffer

\typebuffer \getbuffer

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <!-- begin function -->
      <m:mi>tan</m:mi>
    <!-- end function -->
    <m:mo>(</m:mo>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mo>)</m:mo>
    <m:mo>=</m:mo>
    <m:mrow>
      <m:mfrac>
        <m:mrow>
          <!-- begin function -->
            <m:mi>sin</m:mi>
          <!-- end function -->
          <m:mo>(</m:mo>
          <m:mi>/BTEX\it x/ETEX</m:mi>
          <m:mo>)</m:mo>
        </m:mrow>
        <m:mrow>
          <!-- begin function -->
            <m:mi>cos</m:mi>
          <!-- end function -->
          <m:mo>(</m:mo>
          <m:mi>/BTEX\it x/ETEX</m:mi>
          <m:mo>)</m:mo>
        </m:mrow>
      </m:mfrac>
    </m:mrow>
  </m:mrow>
</m:math>
\stoptyping

As expected we now see the arguments but it is still not clear that the function
has to be applied.

\startbuffer
\startformula
  \apply \tan {(x)} = \frac {
    \apply \sin {(x)}
  } {
    \apply \cos {(x)}
  }
\stopformula
\stopbuffer

\typebuffer \getbuffer

This time we get the function application signal in the output. We could add it
automatically in some cases but for the moment we don't do so. Because this
trigger has no visual rendering and no width it will not be visible in an editor.
Therefore we output an entity.

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mi>tan</m:mi>
    <m:mo>&#x2061;</m:mo>
    <m:mo>(</m:mo>
    <m:mi>/BTEX\it x/ETEX</m:mi>
    <m:mo>)</m:mo>
    <m:mo>=</m:mo>
    <m:mrow>
      <m:mfrac>
        <m:mrow>
          <m:mi>sin</m:mi>
          <m:mo>&#x2061;</m:mo>
          <m:mo>(</m:mo>
          <m:mi>/BTEX\it x/ETEX</m:mi>
          <m:mo>)</m:mo>
        </m:mrow>
        <m:mrow>
          <m:mi>cos</m:mi>
          <m:mo>&#x2061;</m:mo>
          <m:mo>(</m:mo>
          <m:mi>/BTEX\it x/ETEX</m:mi>
          <m:mo>)</m:mo>
        </m:mrow>
      </m:mfrac>
    </m:mrow>
  </m:mrow>
</m:math>
\stoptyping

In the future, we will extend the \type {\apply} macro to also deal with
automatically managed fences. Talking of those, fences are actually supported
when explicitly coded:

\startbuffer
\startformula
  \apply \tan {\left(x\right)} = \frac {
    \apply \sin {\left(x\right)}
  } {
    \apply \cos {\left(x\right)}
  }
\stopformula
\stopbuffer

\typebuffer \getbuffer

This time we get a bit more structure because delimiters in \TEX\ can be
recognized easily. Of course it helps that in \CONTEXT\ we already have the
infrastructure in place.

\starttyping[escape=yes]
<m:math display="block">
  <m:mrow>
    <m:mi>tan</m:mi>
    <m:mo>&#x2061;</m:mo>
    <m:mrow>
      <m:mfenced left="(" right=")">
        <m:mi>/BTEX\it x/ETEX</m:mi>
      </m:mfenced>
    </m:mrow>
    <m:mo>=</m:mo>
    <m:mrow>
      <m:mfrac>
        <m:mrow>
          <m:mi>sin</m:mi>
          <m:mo>&#x2061;</m:mo>
          <m:mfenced left="(" right=")">
            <m:mi>/BTEX\it x/ETEX</m:mi>
          </m:mfenced>
        </m:mrow>
        <m:mrow>
          <m:mi>cos</m:mi>
          <m:mo>&#x2061;</m:mo>
          <m:mfenced left="(" right=")">
            <m:mi>/BTEX\it x/ETEX</m:mi>
          </m:mfenced>
        </m:mrow>
      </m:mfrac>
    </m:mrow>
  </m:mrow>
</m:math>
\stoptyping

Yet another special treatment is needed for alignments. We use the next example
to show some radicals as well.

\startbuffer
\startformula
  \startalign
    \NC  a^2 \EQ \sqrt{b}    \NR
    \NC  c   \EQ \frac{d}{e} \NR
    \NC      \EQ f           \NR
  \stopalign
\stopformula
\stopbuffer

\typebuffer

It helps that in \CONTEXT\ we use a bit of structure in math alignments. In fact,
a math alignment is just a regular alignment, with math in its cells. As with
other math, eventually we end up with boxes so we need to make sure that enough
information is passed along to reconstuct the original.

\getbuffer

\starttyping[escape=yes]
<m:math display="inline">
  <m:mtable detail='align'>
    <m:mtr>
      <m:mtd>
        <m:mrow>
          <m:msup>
            <m:mi>/BTEX\it a/ETEX</m:mi>
            <m:mn>2</m:mn>
          </m:msup>
        </m:mrow>
      </m:mtd>
      <m:mtd>
        <m:mrow>
          <m:mo>=</m:mo>
          <m:mroot>
            <m:mi>/BTEX\it b/ETEX</m:mi>
          </m:mroot>
        </m:mrow>
      </m:mtd>
    </m:mtr>
    <m:mtr>
      <m:mtd>
        <m:mrow>
          <m:mi>/BTEX\it c/ETEX</m:mi>
        </m:mrow>
      </m:mtd>
      <m:mtd>
        <m:mrow>
          <m:mo>=</m:mo>
          <m:mfrac>
            <m:mrow>
              <m:mi>/BTEX\it d/ETEX</m:mi>
            </m:mrow>
            <m:mrow>
              <m:mi>/BTEX\it e/ETEX</m:mi>
            </m:mrow>
          </m:mfrac>
        </m:mrow>
      </m:mtd>
    </m:mtr>
    <m:mtr>
      <m:mtd>
        <m:mrow>
          <m:mo>=</m:mo>
          <m:mi>/BTEX\it f/ETEX</m:mi>
        </m:mrow>
      </m:mtd>
    </m:mtr>
  </m:mtable>
</m:math>
\stoptyping

Watch how the equal sign ends up in the cell. Contrary to what you might expect,
the relation symbols (currently) don't end up in their own column. Keep in mind
that these tables look structured but that presentational \MATHML\ does not
assume that much structure. \footnote {The spacing could be improved here but
it's just an example, not something real.}

\stopsection

\startsection[title=Units]

Rather early in the history of \CONTEXT\ we had support for units and the main
reason for this was that we wanted consistent spacing. The input of the old
method looks as follows:

\starttyping
10 \Cubic \Meter \Per \Second
\stoptyping

This worked in regular text as well as in math and we even have an \XML\ variant.
A few years ago I played with a different method and the \LUA\ code has been
laying around for a while but never made it into the \CONTEXT\ core. However,
when playing with the export, I decided to pick up that thread. The verbose
variant can now be coded as:

\starttyping
10 \unit{cubic meter per second}
\stoptyping

but equally valid is:

\starttyping
10 \unit{m2/s}
\stoptyping

and also

\starttyping
\unit{10 m2/s}
\stoptyping

is okay. So, one can use the short (often official) symbols as well as more
verbose names. In order to see what gets output we cook up some bogus units.

\startbuffer
30 \unit{kilo pascal square meter / kelvin second}
\stopbuffer

\typebuffer

This gets rendered as: \getbuffer. The export looks as follows:

\starttyping
30 <unit>kPa⋅m<sup>2</sup>/K⋅s</unit>
\stoptyping

\startbuffer
\unit{30 kilo pascal square meter / kelvin second}
\stopbuffer

You can also say:

\typebuffer

and get: \getbuffer. This time the export looks like this:

\starttyping
<quantity>
  <number>30</number>
  <unit>kPa⋅m<sup>2</sup>/K⋅s</unit>
</quantity>
\stoptyping

\startbuffer
$30 \unit{kilo pascal square meter / kelvin second }$
\stopbuffer

When we use units in math, the rendering is mostly the same. So,

\typebuffer

Gives: \getbuffer, but the export now looks different:

\starttyping
<m:math display="inline">
  <m:mrow>
  <m:mn>30</m:mn>
  <m:maction actiontype="unit">
    <m:mrow>
      <m:mi mathvariant="normal">k</m:mi>
      <m:mi mathvariant="normal">P</m:mi>
      <m:mi mathvariant="normal">a</m:mi>
      <m:mo>⋅</m:mo>
      <m:msup>
          <m:mi mathvariant="normal">m</m:mi>
          <m:mn>2</m:mn>
      </m:msup>
      <m:mo>/</m:mo>
      <m:mi mathvariant="normal">K</m:mi>
      <m:mo>⋅</m:mo>
      <m:mi mathvariant="normal">s</m:mi>
    </m:mrow>
  </m:maction>
  </m:mrow>
</m:math>
\stoptyping

Watch how we provide some extra information about it being a unit and how the
rendering is controlled as by default a renderer could turn the \type {K} and
other identifiers into math italic. Of course the subtle spacing is lost as we
assume a clever renderer that can use the information provided in the \type
{maction}.

\stopsection

\startsection[title=Conclusion]

So far the results of the export look quite acceptable. It is to be seen to what
extent typographic detail will be added. Thanks to \UNICODE\ math we don't need
to add style directives. Because we carry information with special spaces, we
could add these details if needed but for the moment the focus is on getting the
export robust on the one end, and extending \CONTEXT's math support with some
additional structure.

The export shows in the previous sections was not entirely honest: we didn't show
the wrapper. Say that we have this:

\startbuffer
\startformula
  e = mc^2
\stopformula
\stopbuffer

\typebuffer

This shows up as:

\getbuffer

and exports as:

\starttyping[escape=yes]
<formula>
  <formulacontent>
    <m:math display="block">
      <m:mrow>
        <m:mi>/BTEX\it e/ETEX</m:mi>
        <m:mo>=</m:mo>
        <m:mi>/BTEX\it m/ETEX</m:mi>
        <m:msup>
          <m:mi>/BTEX\it c/ETEX</m:mi>
          <m:mn>2</m:mn>
        </m:msup>
      </m:mrow>
    </m:math>
  </formulacontent>
</formula>
\stoptyping

\startbuffer
\placeformula
  \startformula
     e = mc^2
  \stopformula
\stopbuffer

\typebuffer

This becomes:

\getbuffer

and exports as:

\starttyping[escape=yes]
<formula>
  <formulacontent>
    <m:math display="block">
      <m:mrow>
        <m:mi>/BTEX\it e/ETEX</m:mi>
        <m:mo>=</m:mo>
        <m:mi>/BTEX\it m/ETEX</m:mi>
        <m:msup>
          <m:mi>/BTEX\it c/ETEX</m:mi>
          <m:mn>2</m:mn>
        </m:msup>
      </m:mrow>
    </m:math>
  </formulacontent>
  <formulacaption>
    (<formulanumber detail='formula'>1.1</formulanumber>)
  </formulacaption>
</formula>
\stoptyping

The caption can also have a label in front of the number. The best way to deal
with this still under consideration. I leave it to the reader to wonder how we
get the caption at the same level as the content while in practice the number is
part of the formula.

Anyway, the previous pages have demonstrated that with version 0.22 of the
exporter we can already get a quite acceptable math export. Of course more will
follow.

\stopsection

\stopchapter

\stopcomponent