hybrid-mathml.tex /size: 23 Kb    last modification: 2023-12-21 09:43
1% language=us
2
3% \enabletrackers[structures.export]
4% \setupbackend[export=yes]
5
6\usemodule[mathml] % also loads calcmath
7
8\startcomponent hybrid-mathml
9
10\environment hybrid-environment
11
12\startchapter[title={Exporting math}]
13
14\startsection [title={Introduction}]
15
16As \CONTEXT\ has an \XML\ export feature and because \TEX\ is often strongly
17associated with math typesetting, it makes sense to take a look at coding and
18exporting math. In the next sections some aspects are discussed. The examples
19shown are a snaphot of the possibilities around June 2011.
20
21\stopsection
22
23\startsection [title={Encoding the math}]
24
25In \CONTEXT\ there are several ways to input math. In the following example we
26will use some bogus math with enough structure to get some interesting results.
27
28The most natural way to key in math is using the \TEX\ syntax. Of course you need
29to know the right commands for accessing special symbols, but if you're familiar
30with a certain domain, this is not that hard.
31
32\startbuffer
33\startformula
34  \frac { x \geq 2 } { y \leq 4 }
35\stopformula
36\stopbuffer
37
38\typebuffer \getbuffer
39
40When you have an editor that can show more than \ASCII\ the following also works
41out well.
42
43\starttyping
44\startformula
45  \frac { x  2 } { y  4 }
46\stopformula
47\stoptyping
48
49One can go a step further and use the proper math italic alphabet but there are
50hardly any (monospaced) fonts out there that can visualize it.
51
52\starttyping[escape=yes]
53\startformula
54  \frac { /BTEX\it x/ETEX  2 } { /BTEX\it y/ETEX  4 }
55\stopformula
56\stoptyping
57
58Anyhow, \CONTEXT\ is quite capable of remapping the regular alphabets onto the
59real math ones, so you can stick to \type {x} and \type {y}.
60
61Another way to enter the same formula is by using what we call calculator math.
62We came up with this format many years ago when \CONTEXT\ had to process student
63input using a syntax similar to what the calculators they use at school accept.
64
65\startbuffer
66\startformula
67  \calcmath{(x >= 2)/(y <= 4)}
68\stopformula
69\stopbuffer
70
71\typebuffer \getbuffer
72
73As \CONTEXT\ is used in a free and open school math project, and because some of
74our projects mix \MATHML\ into \XML\ encoded sources, we can also consider using
75\MATHML. The conceptually nicest way is to use content markup, where the focus is
76on meaning and interchangability and not on rendering. However, we can render it
77quite well. OpenMath, now present in \MATHML~3 is also supported.
78
79\startbuffer
80<math xmlns='http://www.w3c.org/mathml' version='2.0'>
81  <apply> <divide/>
82    <apply> <geq/> <ci> x </ci> <cn> 2 </cn> </apply>
83    <apply> <leq/> <ci> y </ci> <cn> 4 </cn> </apply>
84  </apply>
85</math>
86\stopbuffer
87
88\typebuffer \processxmlbuffer
89
90In practice \MATHML\ will be coded using the presentational variant. In many
91aspects this way of coding is not much different from what \TEX\ does.
92
93\startbuffer
94<math xmlns='http://www.w3c.org/mathml' version='2.0'>
95  <mfrac>
96    <mrow> <mi> x </mi> <mo> &geq; </mo> <mn> 2 </mn> </mrow>
97    <mrow> <mi> y </mi> <mo> &leq; </mo> <mn> 4 </mn> </mrow>
98  </mfrac>
99</math>
100\stopbuffer
101
102\typebuffer \processxmlbuffer
103
104When we enable \XML\ export in the backend of \CONTEXT, all of the above variants
105are converted into the following:
106
107% <m:math display="block">
108%   <m:mrow>
109%     <m:mfrac>
110%       <m:mrow>
111%         <m:mi>𝑥</m:mi>
112%         <m:mo>≥</m:mo>
113%         <m:mn>2</m:mn>
114%       </m:mrow>
115%       <m:mrow>
116%         <m:mi>𝑦</m:mi>
117%         <m:mo>≤</m:mo>
118%         <m:mn>4</m:mn>
119%       </m:mrow>
120%     </m:mfrac>
121%   </m:mrow>
122% </m:math>
123
124\starttyping[escape=yes]
125<m:math display="block">
126  <m:mrow>
127    <m:mfrac>
128      <m:mrow>
129        <m:mi>/BTEX\it x/ETEX</m:mi>
130        <m:mo></m:mo>
131        <m:mn>2</m:mn>
132      </m:mrow>
133      <m:mrow>
134        <m:mi>/BTEX\it y/ETEX</m:mi>
135        <m:mo></m:mo>
136        <m:mn>4</m:mn>
137      </m:mrow>
138    </m:mfrac>
139  </m:mrow>
140</m:math>
141\stoptyping
142
143This is pretty close to what we have entered as presentation \MATHML. The main
144difference is that the (display or inline) mode is registered as attribute and
145that entities have been resolved to \UTF. Of course one could use \UTF\ directly
146in the input.
147
148\stopsection
149
150\startsection [title={Parsing the input}]
151
152In \TEX\ typesetting math happens in two stages. First the input is parsed and
153converted into a so called math list. In the following case it's a rather linear
154list, but in the case of a fraction it is a tree.
155
156\startbuffer
157\startformula
158  x = - 1.23
159\stopformula
160\stopbuffer
161
162\typebuffer \getbuffer
163
164A naive export looks as follows. The sequence becomes an \type {mrow}:
165
166\starttyping[escape=yes]
167<m:math display="block">
168  <m:mrow>
169    <m:mi>/BTEX\it x/ETEX</m:mi>
170    <m:mo>=</m:mo>
171    <m:mo></m:mo>
172    <m:mn>1</m:mn>
173    <m:mo>.</m:mo>
174    <m:mn>2</m:mn>
175    <m:mn>3</m:mn>
176  </m:mrow>
177</m:math>
178\stoptyping
179
180However, we can clean this up without too much danger of getting invalid output:
181
182\starttyping[escape=yes]
183<m:math display="block">
184  <m:mrow>
185    <m:mi>/BTEX\it x/ETEX</m:mi>
186    <m:mo>=</m:mo>
187    <m:mo></m:mo>
188    <m:mn>1.23</m:mn>
189  </m:mrow>
190</m:math>
191\stoptyping
192
193This is still not optimal, as one can argue that the minus sign is part of the
194number. This can be taken care of at the input end:
195
196\startbuffer
197\startformula
198  x = \mn{- 1.23}
199\stopformula
200\stopbuffer
201
202\typebuffer
203
204Now we get:
205
206\starttyping[escape=yes]
207<m:math display="block">
208  <m:mrow>
209    <m:mi>/BTEX\it x/ETEX</m:mi>
210    <m:mo>=</m:mo>
211    <m:mn>1.23</m:mn>
212  </m:mrow>
213</m:math>
214\stoptyping
215
216Tagging a number makes sense anyway, for instance when we use different numbering
217schemes:
218
219\startbuffer
220\startformula
221  x = \mn{0x20DF} = 0x20DF
222\stopformula
223\stopbuffer
224
225\typebuffer
226
227We get the first number nicely typeset in an upright font but the second one
228becomes a mix of numbers and identifiers:
229
230\getbuffer
231
232This is nicely reflected in the export:
233
234\starttyping[escape=yes]
235<m:math display="block">
236  <m:mrow>
237    <m:mi>/BTEX\it x/ETEX</m:mi>
238    <m:mo>=</m:mo>
239    <m:mn>0x20DF</m:mn>
240    <m:mo>=</m:mo>
241    <m:mn>0</m:mn>
242    <m:mi>/BTEX\it x/ETEX</m:mi>
243    <m:mn>20</m:mn>
244    <m:mi>/BTEX\it D/ETEX</m:mi>
245    <m:mi>/BTEX\it F/ETEX</m:mi>
246  </m:mrow>
247</m:math>
248\stoptyping
249
250In a similar fashion we can use \type {\mo} and \type {\mi} although these are
251seldom needed, if only because characters and symbols already carry these
252properties with them.
253
254\stopsection
255
256\startsection [title={Enhancing the math list}]
257
258When the input is parsed into a math list the individual elements are called
259noads. The most basic noad has pointers to a nucleus, a superscript and a
260subscript and each of them can be the start of a sublist. All lists (with more
261than one character) are quite similar to \type {mrow} in \MATHML. In the export
262we do some flattening because otherwise we would get too many redundant \type
263{mrow}s, not that it hurts but it saves bytes.
264
265\startbuffer
266\startformula
267  x_n^2
268\stopformula
269\stopbuffer
270
271\typebuffer
272
273This renders as:
274
275\getbuffer
276
277And it gets exported as:
278
279\starttyping[escape=yes]
280<m:math display="block">
281  <m:mrow>
282    <m:msubsup>
283      <m:mi>/BTEX\it x/ETEX</m:mi>
284      <m:mi>/BTEX\it n/ETEX</m:mi>
285      <m:mn>2</m:mn>
286    </m:msubsup>
287  </m:mrow>
288</m:math>
289\stoptyping
290
291As said, in the math list this looks more or less the same: we have a noad with a
292nucleus pointing to a math character (\type {x}) and two additional pointers to
293the sub- and superscripts.
294
295After this math list is typeset, we will end up with horizontal and vertical
296lists with glyphs, kerns, glue and other nodes. In fact we end up with what can
297be considered regular references to slots in a font mixed with positioning
298information. In the process the math properties gets lost. This happens between
299step~3 and~4 in the next overview.
300
301\starttabulate[|l|l|l|]
302\NC 1  \NC \XML  \NC optional alternative input \NC \NR
303\NC 2  \NC \TEX  \NC native math coding \NC \NR
304\NC 3  \NC noads \NC intermediate linked list / tree \NC \NR
305\NC 4  \NC nodes \NC linked list with processed (typeset) math \NC \NR
306\NC 5a \NC \PDF  \NC page description suitable for rendering \NC \NR
307\NC 5b \NC \XML  \NC export reflecting the final document content \NC \NR
308\stoptabulate
309
310In \CONTEXT\ \MKIV\ we intercept the math list (with noads) and apply a couple of
311manipulations to it, most noticeably relocation of characters. Last in the
312(currently some 10) manipulation passes over the math list comes tagging. This
313only happens when the export is active or when we produce tagged pdf. \footnote
314{Currently the export is the benchmark and the tagged \PDF\ implementation
315follows, so there can be temporary incompatibilities.}
316
317By tagging the recognizable math snippets we can later use those persistent
318properties to reverse engineer the \MATHML\ from the input.
319
320\stopsection
321
322\startsection [title={Intercepting the typeset content}]
323
324When a page gets shipped out, we also convert the typeset content to an
325intermediate form, ready for export later on. Version 0.22 of the exporter has a
326rather verbose tracing mechanism and the simple example with sub- and superscript
327is reported as follows:
328
329\starttyping[escape=yes]
330<math-8 trigger='268' index='1'>
331  <mrow-20 trigger='268' index='1'>
332    <msubsup-1 trigger='268' index='1'>
333      <mi-15 trigger='268' index='1'>
334        <!-- processing glyph 2 (tag 270) -->
335        <!-- moving from depth 11 to 11 (mi-15) -->
336        <!-- staying at depth 11 (mi-15) -->
337        <!-- start content with length 4 -->
338        /BTEX\it x/ETEX
339        <!-- stop content -->
340        <!-- moving from depth 11 to 11 (mn-13) -->
341      </mi-15>
342      <mn-13 trigger='270' index='2'>
343        <!-- processing kern > threshold (tag 270 => 267)
344        <!-- moving from depth 11 to 11 (mn-13) -->
345        <!-- staying at depth 11 (mn-13) -->
346        <!-- start content with length 1 -->
347        2
348        <!-- stop content -->
349        <!-- injecting spacing 9 -->
350        <!-- moving from depth 11 to 10 (msubsup-1) -->
351      </mn-13>
352    </msubsup-1>
353    <!-- processing glyph 𝑛 (tag 269) -->
354    <!-- moving from depth 9 to 10 (msubsup-1) -->
355    <msubsup-1 trigger='267' index='2'>
356      <!-- start content with length 1 -->
357
358      <!-- stop content -->
359    </msubsup-1>
360    <!-- moving from depth 9 to 11 (mi-16) -->
361    <msubsup-1 trigger='269' index='3'>
362      <mi-16 trigger='269' index='1'>
363        <!-- processing glue > threshold (tag 269 => 262) -->
364        <!-- moving from depth 11 to 11 (mi-16) -->
365        <!-- staying at depth 11 (mi-16) -->
366        <!-- start content with length 4 -->
367        /BTEX\it n/ETEX
368        <!-- stop content -->
369        <!-- injecting spacing 6 -->
370        <!-- moving from depth 11 to 6 (formula-8) -->
371      </mi-16>
372    </msubsup-1>
373  </mrow-20>
374</math-8>
375\stoptyping
376
377This is not yet what we want so some more effort is needed in order to get proper
378\MATHML.
379
380\stopsection
381
382\startsection [title={Exporting the result}]
383
384The report that we showed before representing the simple example with super- and
385subscripts is strongly related to the visual rendering. It happens that \TEX\
386first typesets the superscript and then deals with the subscript. Some spacing is
387involved which shows up in the report between the two scripts.
388
389In \MATHML\ we need to swap the order of the scripts, so effectively we need:
390
391\starttyping[escape=yes]
392<math-8 trigger='268' index='1'>
393  <mrow-20 trigger='268' index='1'>
394    <msubsup-1 trigger='268' index='1'>
395      <mi-15 trigger='268' index='1'>
396        /BTEX\it x/ETEX
397      </mi-15>
398      <mi-16 trigger='269' index='2'>
399        /BTEX\it n/ETEX
400      </mi-16>
401      <mn-13 trigger='270' index='3'>
402        2
403      </mn-13>
404    </msubsup-1>
405  </mrow-20>
406</math-8>
407\stoptyping
408
409This swapping (and some further cleanup) is done before the final tree is written
410to a file. There we get:
411
412\starttyping[escape=yes]
413<m:math display="block">
414  <m:mrow>
415    <m:msubsup>
416      <m:mi>/BTEX\it x/ETEX</m:mi>
417      <m:mi>/BTEX\it n/ETEX</m:mi>
418      <m:mn>2</m:mn>
419    </m:msubsup>
420  </m:mrow>
421</m:math>
422\stoptyping
423
424This looks pretty close to the intermediate format. In case you wonder with how
425much intermediate data we end up, the answer is: quite some. The reason will be
426clear: we intercept typeset output and reconstruct the input from that, which
427means that we have additional information travelling with the content. Also, we
428need to take crossing pages into account and we need to reconstruct paragraphs.
429There is also some overhead in making the \XML\ look acceptable but that is
430neglectable. In terms of runtime, the overhead of an export (including tagging)
431is some 10\% which is not that bad, and there is some room for optimization.
432
433\stopsection
434
435\startsection[title={Special treatments}]
436
437In content \MATHML\ the \type {apply} tag is the cornerstone of the definition.
438Because there is enough information the rendering mechanism can deduce when a
439function is applied and act accordingly when it comes to figuring out the right
440amount of spacing. In presentation \MATHML\ there is no such information and
441there the signal is given by putting a character with code \type {U+2061} between
442the function identifier and the argument. In \TEX\ input all this is dealt with
443in the macro that specifies a function but some ambiguity is left.
444
445Compare the following two formulas:
446
447\startbuffer
448\startformula
449  \tan = \frac { \sin } { \cos }
450\stopformula
451\stopbuffer
452
453\typebuffer \getbuffer
454
455In the export this shows up as follows:
456
457\starttyping
458<m:math display="block">
459  <m:mrow>
460    <!-- begin function -->
461      <m:mi>tan</m:mi>
462    <!-- end function -->
463    <m:mo>=</m:mo>
464    <m:mrow>
465      <m:mfrac>
466        <m:mrow>
467          <!-- begin function -->
468            <m:mi>sin</m:mi>
469          <!-- end function -->
470        </m:mrow>
471        <m:mrow>
472          <!-- begin function -->
473            <m:mi>cos</m:mi>
474          <!-- end function -->
475        </m:mrow>
476      </m:mfrac>
477    </m:mrow>
478  </m:mrow>
479</m:math>
480\stoptyping
481
482Watch how we know that \type {tan} is a function and not a multiplication of the
483variables \type {t}, \type{a} and~\type {n}.
484
485In most cases functions will get an argument, as in:
486
487\startbuffer
488\startformula
489  \tan (x) = \frac { \sin (x) } { \cos (x) }
490\stopformula
491\stopbuffer
492
493\typebuffer \getbuffer
494
495\starttyping[escape=yes]
496<m:math display="block">
497  <m:mrow>
498    <!-- begin function -->
499      <m:mi>tan</m:mi>
500    <!-- end function -->
501    <m:mo>(</m:mo>
502    <m:mi>/BTEX\it x/ETEX</m:mi>
503    <m:mo>)</m:mo>
504    <m:mo>=</m:mo>
505    <m:mrow>
506      <m:mfrac>
507        <m:mrow>
508          <!-- begin function -->
509            <m:mi>sin</m:mi>
510          <!-- end function -->
511          <m:mo>(</m:mo>
512          <m:mi>/BTEX\it x/ETEX</m:mi>
513          <m:mo>)</m:mo>
514        </m:mrow>
515        <m:mrow>
516          <!-- begin function -->
517            <m:mi>cos</m:mi>
518          <!-- end function -->
519          <m:mo>(</m:mo>
520          <m:mi>/BTEX\it x/ETEX</m:mi>
521          <m:mo>)</m:mo>
522        </m:mrow>
523      </m:mfrac>
524    </m:mrow>
525  </m:mrow>
526</m:math>
527\stoptyping
528
529As expected we now see the arguments but it is still not clear that the function
530has to be applied.
531
532\startbuffer
533\startformula
534  \apply \tan {(x)} = \frac {
535    \apply \sin {(x)}
536  } {
537    \apply \cos {(x)}
538  }
539\stopformula
540\stopbuffer
541
542\typebuffer \getbuffer
543
544This time we get the function application signal in the output. We could add it
545automatically in some cases but for the moment we don't do so. Because this
546trigger has no visual rendering and no width it will not be visible in an editor.
547Therefore we output an entity.
548
549\starttyping[escape=yes]
550<m:math display="block">
551  <m:mrow>
552    <m:mi>tan</m:mi>
553    <m:mo>&#x2061;</m:mo>
554    <m:mo>(</m:mo>
555    <m:mi>/BTEX\it x/ETEX</m:mi>
556    <m:mo>)</m:mo>
557    <m:mo>=</m:mo>
558    <m:mrow>
559      <m:mfrac>
560        <m:mrow>
561          <m:mi>sin</m:mi>
562          <m:mo>&#x2061;</m:mo>
563          <m:mo>(</m:mo>
564          <m:mi>/BTEX\it x/ETEX</m:mi>
565          <m:mo>)</m:mo>
566        </m:mrow>
567        <m:mrow>
568          <m:mi>cos</m:mi>
569          <m:mo>&#x2061;</m:mo>
570          <m:mo>(</m:mo>
571          <m:mi>/BTEX\it x/ETEX</m:mi>
572          <m:mo>)</m:mo>
573        </m:mrow>
574      </m:mfrac>
575    </m:mrow>
576  </m:mrow>
577</m:math>
578\stoptyping
579
580In the future, we will extend the \type {\apply} macro to also deal with
581automatically managed fences. Talking of those, fences are actually supported
582when explicitly coded:
583
584\startbuffer
585\startformula
586  \apply \tan {\left(x\right)} = \frac {
587    \apply \sin {\left(x\right)}
588  } {
589    \apply \cos {\left(x\right)}
590  }
591\stopformula
592\stopbuffer
593
594\typebuffer \getbuffer
595
596This time we get a bit more structure because delimiters in \TEX\ can be
597recognized easily. Of course it helps that in \CONTEXT\ we already have the
598infrastructure in place.
599
600\starttyping[escape=yes]
601<m:math display="block">
602  <m:mrow>
603    <m:mi>tan</m:mi>
604    <m:mo>&#x2061;</m:mo>
605    <m:mrow>
606      <m:mfenced left="(" right=")">
607        <m:mi>/BTEX\it x/ETEX</m:mi>
608      </m:mfenced>
609    </m:mrow>
610    <m:mo>=</m:mo>
611    <m:mrow>
612      <m:mfrac>
613        <m:mrow>
614          <m:mi>sin</m:mi>
615          <m:mo>&#x2061;</m:mo>
616          <m:mfenced left="(" right=")">
617            <m:mi>/BTEX\it x/ETEX</m:mi>
618          </m:mfenced>
619        </m:mrow>
620        <m:mrow>
621          <m:mi>cos</m:mi>
622          <m:mo>&#x2061;</m:mo>
623          <m:mfenced left="(" right=")">
624            <m:mi>/BTEX\it x/ETEX</m:mi>
625          </m:mfenced>
626        </m:mrow>
627      </m:mfrac>
628    </m:mrow>
629  </m:mrow>
630</m:math>
631\stoptyping
632
633Yet another special treatment is needed for alignments. We use the next example
634to show some radicals as well.
635
636\startbuffer
637\startformula
638  \startalign
639    \NC  a^2 \EQ \sqrt{b}    \NR
640    \NC  c   \EQ \frac{d}{e} \NR
641    \NC      \EQ f           \NR
642  \stopalign
643\stopformula
644\stopbuffer
645
646\typebuffer
647
648It helps that in \CONTEXT\ we use a bit of structure in math alignments. In fact,
649a math alignment is just a regular alignment, with math in its cells. As with
650other math, eventually we end up with boxes so we need to make sure that enough
651information is passed along to reconstuct the original.
652
653\getbuffer
654
655\starttyping[escape=yes]
656<m:math display="inline">
657  <m:mtable detail='align'>
658    <m:mtr>
659      <m:mtd>
660        <m:mrow>
661          <m:msup>
662            <m:mi>/BTEX\it a/ETEX</m:mi>
663            <m:mn>2</m:mn>
664          </m:msup>
665        </m:mrow>
666      </m:mtd>
667      <m:mtd>
668        <m:mrow>
669          <m:mo>=</m:mo>
670          <m:mroot>
671            <m:mi>/BTEX\it b/ETEX</m:mi>
672          </m:mroot>
673        </m:mrow>
674      </m:mtd>
675    </m:mtr>
676    <m:mtr>
677      <m:mtd>
678        <m:mrow>
679          <m:mi>/BTEX\it c/ETEX</m:mi>
680        </m:mrow>
681      </m:mtd>
682      <m:mtd>
683        <m:mrow>
684          <m:mo>=</m:mo>
685          <m:mfrac>
686            <m:mrow>
687              <m:mi>/BTEX\it d/ETEX</m:mi>
688            </m:mrow>
689            <m:mrow>
690              <m:mi>/BTEX\it e/ETEX</m:mi>
691            </m:mrow>
692          </m:mfrac>
693        </m:mrow>
694      </m:mtd>
695    </m:mtr>
696    <m:mtr>
697      <m:mtd>
698        <m:mrow>
699          <m:mo>=</m:mo>
700          <m:mi>/BTEX\it f/ETEX</m:mi>
701        </m:mrow>
702      </m:mtd>
703    </m:mtr>
704  </m:mtable>
705</m:math>
706\stoptyping
707
708Watch how the equal sign ends up in the cell. Contrary to what you might expect,
709the relation symbols (currently) don't end up in their own column. Keep in mind
710that these tables look structured but that presentational \MATHML\ does not
711assume that much structure. \footnote {The spacing could be improved here but
712it's just an example, not something real.}
713
714\stopsection
715
716\startsection[title=Units]
717
718Rather early in the history of \CONTEXT\ we had support for units and the main
719reason for this was that we wanted consistent spacing. The input of the old
720method looks as follows:
721
722\starttyping
72310 \Cubic \Meter \Per \Second
724\stoptyping
725
726This worked in regular text as well as in math and we even have an \XML\ variant.
727A few years ago I played with a different method and the \LUA\ code has been
728laying around for a while but never made it into the \CONTEXT\ core. However,
729when playing with the export, I decided to pick up that thread. The verbose
730variant can now be coded as:
731
732\starttyping
73310 \unit{cubic meter per second}
734\stoptyping
735
736but equally valid is:
737
738\starttyping
73910 \unit{m2/s}
740\stoptyping
741
742and also
743
744\starttyping
745\unit{10 m2/s}
746\stoptyping
747
748is okay. So, one can use the short (often official) symbols as well as more
749verbose names. In order to see what gets output we cook up some bogus units.
750
751\startbuffer
75230 \unit{kilo pascal square meter / kelvin second}
753\stopbuffer
754
755\typebuffer
756
757This gets rendered as: \getbuffer. The export looks as follows:
758
759\starttyping
76030 <unit>kPa⋅m<sup>2</sup>/K⋅s</unit>
761\stoptyping
762
763\startbuffer
764\unit{30 kilo pascal square meter / kelvin second}
765\stopbuffer
766
767You can also say:
768
769\typebuffer
770
771and get: \getbuffer. This time the export looks like this:
772
773\starttyping
774<quantity>
775  <number>30</number>
776  <unit>kPa⋅m<sup>2</sup>/K⋅s</unit>
777</quantity>
778\stoptyping
779
780\startbuffer
781$30 \unit{kilo pascal square meter / kelvin second }$
782\stopbuffer
783
784When we use units in math, the rendering is mostly the same. So,
785
786\typebuffer
787
788Gives: \getbuffer, but the export now looks different:
789
790\starttyping
791<m:math display="inline">
792  <m:mrow>
793  <m:mn>30</m:mn>
794  <m:maction actiontype="unit">
795    <m:mrow>
796      <m:mi mathvariant="normal">k</m:mi>
797      <m:mi mathvariant="normal">P</m:mi>
798      <m:mi mathvariant="normal">a</m:mi>
799      <m:mo></m:mo>
800      <m:msup>
801          <m:mi mathvariant="normal">m</m:mi>
802          <m:mn>2</m:mn>
803      </m:msup>
804      <m:mo>/</m:mo>
805      <m:mi mathvariant="normal">K</m:mi>
806      <m:mo></m:mo>
807      <m:mi mathvariant="normal">s</m:mi>
808    </m:mrow>
809  </m:maction>
810  </m:mrow>
811</m:math>
812\stoptyping
813
814Watch how we provide some extra information about it being a unit and how the
815rendering is controlled as by default a renderer could turn the \type {K} and
816other identifiers into math italic. Of course the subtle spacing is lost as we
817assume a clever renderer that can use the information provided in the \type
818{maction}.
819
820\stopsection
821
822\startsection[title=Conclusion]
823
824So far the results of the export look quite acceptable. It is to be seen to what
825extent typographic detail will be added. Thanks to \UNICODE\ math we don't need
826to add style directives. Because we carry information with special spaces, we
827could add these details if needed but for the moment the focus is on getting the
828export robust on the one end, and extending \CONTEXT's math support with some
829additional structure.
830
831The export shows in the previous sections was not entirely honest: we didn't show
832the wrapper. Say that we have this:
833
834\startbuffer
835\startformula
836  e = mc^2
837\stopformula
838\stopbuffer
839
840\typebuffer
841
842This shows up as:
843
844\getbuffer
845
846and exports as:
847
848\starttyping[escape=yes]
849<formula>
850  <formulacontent>
851    <m:math display="block">
852      <m:mrow>
853        <m:mi>/BTEX\it e/ETEX</m:mi>
854        <m:mo>=</m:mo>
855        <m:mi>/BTEX\it m/ETEX</m:mi>
856        <m:msup>
857          <m:mi>/BTEX\it c/ETEX</m:mi>
858          <m:mn>2</m:mn>
859        </m:msup>
860      </m:mrow>
861    </m:math>
862  </formulacontent>
863</formula>
864\stoptyping
865
866\startbuffer
867\placeformula
868  \startformula
869     e = mc^2
870  \stopformula
871\stopbuffer
872
873\typebuffer
874
875This becomes:
876
877\getbuffer
878
879and exports as:
880
881\starttyping[escape=yes]
882<formula>
883  <formulacontent>
884    <m:math display="block">
885      <m:mrow>
886        <m:mi>/BTEX\it e/ETEX</m:mi>
887        <m:mo>=</m:mo>
888        <m:mi>/BTEX\it m/ETEX</m:mi>
889        <m:msup>
890          <m:mi>/BTEX\it c/ETEX</m:mi>
891          <m:mn>2</m:mn>
892        </m:msup>
893      </m:mrow>
894    </m:math>
895  </formulacontent>
896  <formulacaption>
897    (<formulanumber detail='formula'>1.1</formulanumber>)
898  </formulacaption>
899</formula>
900\stoptyping
901
902The caption can also have a label in front of the number. The best way to deal
903with this still under consideration. I leave it to the reader to wonder how we
904get the caption at the same level as the content while in practice the number is
905part of the formula.
906
907Anyway, the previous pages have demonstrated that with version 0.22 of the
908exporter we can already get a quite acceptable math export. Of course more will
909follow.
910
911\stopsection
912
913\stopchapter
914
915\stopcomponent
916