On Thu, Apr 18 2024, "Fraga, Eric" <e.fr...@ucl.ac.uk> wrote:

> Hello all,
>
> I am having (for my sins) to write some Word documents.  I don't even
> have Word on my system but that's by the by.  The documents have some
> mathematical expressions.  In the past, I have used LaTeX to create
> images of these expressions (and hence my earlier post today).  But I'm
> told this is no longer good enough.  Sigh.
>
> So I've started playing with MathML and ODT export.  This is actually
> working quite nicely although writing equations in MathML is ponderous
> [1].
>
> The question I have is: can I replace the link to a Math ML file (which
> does work, as noted in the org info manual) with the actual Math ML code
> inline within the org file?  If so, how?  I would rather not have
> separate files lying around.
>
> Thank you,
> eric
>
> Footnotes:
> [1] exporting LaTeX fragments directly, via latexmlmath, does not
>      actually work very well at all.  The text passed by org is wrong
>      basically.  And latexmlmath gets easily confused, it seems.  I'd
>      rather work with the MathML directly in the end. 

Eric,
I think there is a bug in the way the exporter handles odt fragments.

Explanation: Maxima can print output in mathml. Your question inspired
me to try to craft an example. I came up with the attached org file.

The first two subsections of it exports fine to html (replace "export
odt" with "export html"). But when I export to odt, something weird
happens: contents.xml contains the fragment of mathml, but it is not
displayed in libreoffice. On the other hand, the snippet in the third
sub-section, that is saved to file, is handled correctly (the somewhat
mangled formatting is a bug in Maxima's mathml printer).

Leo

#+TITLE: Demo of mathml and maxima

* An example

** Set up of the printer
If you have version =5.46+= of Maxima, this Maxima code can be used.

#+name: startup
#+begin_src maxima :tangle ./startup.mac :exports code :results none
  (load("alt-display"),
    set_alt_display(1,lambda([x],false)),
    set_alt_display(2,mathml_display),
    display2d:true, linenum:0);
#+end_src

Otherwise, for older versions, this Lisp-ified Maxima code can be used.

#+begin_src maxima :tangle ./startup.lisp  :exports code :results none :noweb yes
  #$
  <<startup>>
  #$
#+end_src

The code loads the =alt-display= package.
It sets the 1d printer to print nothing, so input is not echoed; it sets the 2d printer to use the =mathml_display= function, which is provided by =alt-display=.

** A simple example

The code block

#+name: example.org
#+begin_src org :exports code :results replace
  ,#+name: example.mac
  ,#+header: :exports both
  ,#+header: :results raw
  ,#+header: :wrap "export odt"
  ,#+header: :batch batch
  ,#+header: :cmdline --no-init --very-quiet --preload ./startup.lisp
  ,#+begin_src maxima
    genmatrix(lambda([i,j], random(100)/random(100)), 4,4);
  ,#+end_src
#+end_src

#+RESULTS: example.org
#+name: example.mac
#+header: :exports both
#+header: :results raw
#+header: :wrap "export odt"
#+header: :batch batch
#+header: :cmdline --no-init --very-quiet --preload ./startup.lisp
#+begin_src maxima
  genmatrix(lambda([i,j], random(100)/random(100)), 4,4);
#+end_src

produces the following output:

#+RESULTS: example.mac
#+begin_export odt
<math xmlns="http://www.w3.org/1998/Math/MathML";> <mi>mlabel</mi> 
 <mfenced separators=""><msub><mi>%o</mi> <mn>1</mn></msub> <mo>,</mo>
 <mfenced separators="" open="(" close=")"><mtable><mtr><mtd><mn>6</mn> 
 </mtd><mtd><mfrac><mrow><mn>2</mn> </mrow> <mrow><mn>5</mn> </mrow></mfrac> 
 </mtd><mtd><mfrac><mrow><mn>4</mn> </mrow> <mrow><mn>91</mn> </mrow></mfrac> 
 </mtd><mtd><mfrac><mrow><mn>29</mn> </mrow> <mrow><mn>85</mn> 
 </mrow></mfrac> </mtd></mtr> <mtr><mtd><mfrac><mrow><mn>98</mn> 
 </mrow> <mrow><mn>3</mn> </mrow></mfrac> </mtd><mtd><mfrac><mrow><mn>7</mn> 
 </mrow> <mrow><mn>13</mn> </mrow></mfrac> </mtd><mtd><mfrac><mrow>
 <mn>20</mn> </mrow> <mrow><mn>13</mn> </mrow></mfrac> </mtd><mtd>
 <mfrac><mrow><mn>39</mn> </mrow> <mrow><mn>20</mn> </mrow></mfrac> 
 </mtd></mtr> <mtr><mtd><mfrac><mrow><mn>19</mn> </mrow> <mrow><mn>4</mn> 
 </mrow></mfrac> </mtd><mtd><mfrac><mrow><mn>97</mn> </mrow> <mrow><mn>6</mn> 
 </mrow></mfrac> </mtd><mtd><mn>1</mn> </mtd><mtd><mfrac><mrow><mn>43</mn> 
 </mrow> <mrow><mn>39</mn> </mrow></mfrac> </mtd></mtr> <mtr><mtd>
 <mfrac><mrow><mn>23</mn> </mrow> <mrow><mn>13</mn> </mrow></mfrac> 
 </mtd><mtd><mfrac><mrow><mn>36</mn> </mrow> <mrow><mn>53</mn> 
 </mrow></mfrac> </mtd><mtd><mfrac><mrow><mn>60</mn> </mrow> <mrow>
 <mn>71</mn> </mrow></mfrac> </mtd><mtd><mfrac><mrow><mn>4</mn> </mrow> <mrow>
 <mn>3</mn> </mrow></mfrac> </mtd></mtr> </mtable></mfenced> </mfenced> </math>
#+end_export

A small amount of hand-editing will give something better.

** A re-think
The code seems correct, so let's save the results of the code block to file and use the exporter to correctly handle it.

#+name: rethink.org
#+begin_src org :exports code :results replace
  ,#+name: rethink.mac
  ,#+header: :exports both
  ,#+header: :results file
  ,#+header: :file ./snippet.mml
  ,#+header: :batch batch
  ,#+header: :cmdline --no-init --very-quiet --preload ./startup.lisp
  ,#+begin_src maxima
    genmatrix(lambda([i,j], random(100)/random(100)), 4,4);
  ,#+end_src
#+end_src

#+RESULTS: rethink.org
#+name: rethink.mac
#+header: :exports both
#+header: :results file
#+header: :file ./snippet.mml
#+header: :batch batch
#+header: :cmdline --no-init --very-quiet --preload ./startup.lisp
#+begin_src maxima
  genmatrix(lambda([i,j], random(100)/random(100)), 4,4);
#+end_src

produces the following output:

#+RESULTS: rethink.mac
[[file:./snippet.mml]]



Attachment: mathml.odt
Description: mathml.odt

Reply via email to