Converting LaTeX math to ConTeXt
Hello, I'm trying to provide more support for LaTeX math for an Org Mode exporter for ConTeXt. LaTeX is the de facto standard for mathematics in Org Mode. However, the syntax is quite different between LaTeX and ConTeXt. I have a few ideas, but none that I'm happy about. 1. Export LaTeX to MathML then import that into ConTeXt. I don't know how to get this to work. latexmlmath only returns the first row of `align` environments (for me anyway). Also, this would require users to install another program. 2. Typeset equations with LaTeX as standalone pages and then include them as images. This will typeset everything with LaTeX defaults. Also, either equation alignment or equation numbering will break. 3. Hack every math environment I can think of to accept LaTeX syntax (see https://wiki.contextgarden.net/LaTeX_Math_in_ConTeXt). Lots of work, lots of problems as described in the article. End-users could add their own hacks to the template preamble for cases I don't get to. 4. Manually parse and convert every LaTeX math environment to ConTeXt before exporting. Basically a non-starter. I'm wondering what's the best way to go about this. Surely others have run into the same problem? Thanks, Jason
On 10/10/2021 5:45 AM, Jason Ross via ntg-context wrote:
Hello,
I'm trying to provide more support for LaTeX math for an Org Mode exporter for ConTeXt. LaTeX is the de facto standard for mathematics in Org Mode. However, the syntax is quite different between LaTeX and ConTeXt. I have a few ideas, but none that I'm happy about.
1. Export LaTeX to MathML then import that into ConTeXt. I don't know how to get this to work. latexmlmath only returns the first row of `align` environments (for me anyway). Also, this would require users to install another program. 2. Typeset equations with LaTeX as standalone pages and then include them as images. This will typeset everything with LaTeX defaults. Also, either equation alignment or equation numbering will break. 3. Hack every math environment I can think of to accept LaTeX syntax (see https://wiki.contextgarden.net/LaTeX_Math_in_ConTeXt). Lots of work, lots of problems as described in the article. End-users could add their own hacks to the template preamble for cases I don't get to. 4. Manually parse and convert every LaTeX math environment to ConTeXt before exporting. Basically a non-starter.
I'm wondering what's the best way to go about this. Surely others have run into the same problem? what do you mean with 'quite different'
Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 10/10/21 3:23 AM, Hans Hagen wrote:
what do you mean with 'quite different'
Hans
Hi Hans, My current stumbling block is dealing with the `align` environment. I replace `\begin{align}/\end{align}` with `\startmathalignment/\stopmathalignment`, and replace all `&` characters with `\NC` and `\\` with `\NR`, which is simple enough. However, the two commands work differently - `align` doesn't care how many columns you have, but `mathalignment` does. This makes dealing with aligned equations containing matrices hard - you can't just count how many `&`s you see before you get to a `\\`, because you might be inside of a matrix in which case they don't count. Also, `mathalignment` and `matrix` want a leading `\NC` to start each column, while LaTeX `align` and `matrix` just want `&` as a column separator. These are just a couple examples, but in general it's not enough to do naive string substitution in LaTeX and get valid ConTeXt back, and my patchwork solutions are starting to smell. Thanks, Jason
On Sun, 10 Oct 2021, Jason Ross via ntg-context wrote:
On 10/10/21 3:23 AM, Hans Hagen wrote:
These are just a couple examples, but in general it's not enough to do naive string substitution in LaTeX and get valid ConTeXt back, and my patchwork solutions are starting to smell.
You can use pandoc to parse latex and convert it to mathml: $ cat math.tex \documentclass{amsart} \begin{document} \begin{align} a &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, b &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, \\ c &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, d &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, \end{align} \end{document} $ pandoc -f latex -t html --mathml math.tex <p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mtable><mtr><mtd columnalign="right"><mi>a</mi></mtd><mtd columnalign="left"><mo>=</mo><mrow><mo stretchy="true" form="prefix">(</mo><mtable><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr></mtable><mo stretchy="true" form="postfix">)</mo></mrow><mo>,</mo><mi>b</mi></mtd><mtd columnalign="right"><mo>=</mo><mrow><mo stretchy="true" form="prefix">(</mo><mtable><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr></mtable><mo stretchy="true" form="postfix">)</mo></mrow><mo>,</mo></mtd></mtr><mtr><mtd columnalign="right"><mi>c</mi></mtd><mtd columnalign="left"><mo>=</mo><mrow><mo stretchy="true" form="prefix">(</mo><mtable><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr></mtable><mo stretchy="true" form="postfix">)</mo></mrow><mo>,</mo><mi>d</mi></mtd><mtd columnalign="right"><mo>=</mo><mrow><mo stretchy="true" form="prefix">(</mo><mtable><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr><mtr><mtd columnalign="center"><mn>1</mn></mtd><mtd columnalign="center"><mn>2</mn></mtd><mtd columnalign="center"><mn>3</mn></mtd></mtr></mtable><mo stretchy="true" form="postfix">)</mo></mrow><mo>,</mo></mtd></mtr></mtable><annotation encoding="application/x-tex">\begin{aligned} a &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, b &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, \\ c &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, d &= \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix},\end{aligned}</annotation></semantics></math></p> Processing this in context (see attached files), gives a partially working solution. It doesn't look okay, but should be easily fixable. Do note that this is almost same as compiling latex output to PDF because editing presentation mathml by hand is no fun. Aditya
On 10/11/21 4:02 PM, Aditya Mahajan wrote:
You can use pandoc to parse latex and convert it to mathml:
Processing this in context (see attached files), gives a partially working solution. It doesn't look okay, but should be easily fixable.
Do note that this is almost same as compiling latex output to PDF because editing presentation mathml by hand is no fun.
Hmm. Looks like converting to MathML isn't going to work just yet. Is this an issue with ConTeXt? The MathML seems to render correctly in LibreOffice but gets mangled when exporting to ConTeXt. I'll explore some of the alternatives you suggested. A simple parser will be fine for now. Thank you the help. Best Regards, Jason
On Tue, 12 Oct 2021, Jason Ross wrote:
On 10/11/21 4:02 PM, Aditya Mahajan wrote:
You can use pandoc to parse latex and convert it to mathml:
Processing this in context (see attached files), gives a partially working solution. It doesn't look okay, but should be easily fixable.
Do note that this is almost same as compiling latex output to PDF because editing presentation mathml by hand is no fun.
Hmm. Looks like converting to MathML isn't going to work just yet. Is this an issue with ConTeXt? The MathML seems to render correctly in LibreOffice but gets mangled when exporting to ConTeXt.
This is an issue with ConTeXt, but most of these issues are minor are easy to fix. Just send minimal examples showing the incorrect rendering. Aditya
On 10/12/21 8:31 PM, Aditya Mahajan wrote:
This is an issue with ConTeXt, but most of these issues are minor are easy to fix. Just send minimal examples showing the incorrect rendering.
Aditya
An example is attached. MathML generated with pandoc -f latex -t html --mathml sample-math.tex \ | xmlstarlet ed -d "//*[local-name()='annotation']" \ | xmlstarlet sel -t -c "/p/*" \ | xmlstarlet ed -m "//*[local-name()='semantics']/*" \ "//*[local-name()='math']" \ | xmlstarlet ed -d "//*[local-name()='semantics']"
participants (3)
-
Aditya Mahajan
-
Hans Hagen
-
Jason Ross