TransWikia.com

ConTeXt: Mix XHTML and TeX commands

TeX - LaTeX Asked by Dave Jarvis on May 9, 2021

Background

A document, having inline math statements, is being written in Markdown. The document is converted to XHTML for subsequent processing using ConTeXt.

Problem

The XHTML contains XML elements with inline TeX commands. The inline TeX commands are not interpreted by ConTeXt, resulting in output similar to the following:

Output document

SSCCE

Consider the following buffered XHTML snippet (e.g., generated from Markdown):

startbuffer[document]
<body>
<p>$N = R_{*} cdot f_{p} cdot n_{e} cdot f_{l} cdot f_{i} cdot f_{c} cdot L$</p>
</body>
stopbuffer

startxmlsetups xml:xhtml
  xmlsetsetup{xmldocument}{*}{-}
  xmlsetsetup{xmldocument}{body|p}{xml:*}
stopxmlsetups

startxmlsetups xml:body xmlflush{#1} stopxmlsetups

startxmlsetups xml:p
  xmlflush{#1}par
stopxmlsetups

xmlregistersetup{xml:xhtml}

starttext
  xmlprocessbuffer{xhtml}{document}{}
stoptext

Idea 1 – Syntax

It’s possible to produce documents that export new XHTML elements, such as:

<tex>$N = R_{*} cdot f_{p} cdot n_{e} cdot f_{l} cdot f_{i} cdot f_{c} cdot L$</tex>

Or even without the $ sigil:

<tex>N = R_{*} cdot f_{p} cdot n_{e} cdot f_{l} cdot f_{i} cdot f_{c} cdot L</tex>

However, TeX allows for both inline $ and block-level $$ syntax. These could be exported as tex_inline and tex_block elements, respectively, but that seems like duplication because ConTeXt already contains the parsing logic.

Idea 2 – Setups

The problem is likely with the setups. Variations, such as those from the steps document, did not result in typeset math:

startbuffer[document]
<body>
<tex>$N = R_{*} cdot f_{p} cdot n_{e} cdot f_{l} cdot f_{i} cdot f_{c} cdot L$</tex>
</body>
stopbuffer

startxmlsetups xml:xhtml
  xmlsetsetup{xmldocument}{*}{-}
  xmlsetsetup{xmldocument}{body|p|tex}{xml:*}
stopxmlsetups

% ...

startxmlsetups xml:tex
  mathematics{xmlflush{#1}}
stopxmlsetups

Even if it worked, this approach may prove problematic because users can insert (inline) TeX into any element: p, blockquotes, emphasis, strong, td, li, etc.

Idea 3 – Convert to MathML

itex2MML and TeX4ht both convert TeX to MathML, but such a step should not be needed.

Question

How would you embed inline/block-level TeX macros anywhere inside a well-formed XHTML document such that the macros will be typeset by ConTeXt?

(MathML is not a viable option for writing inside Markdown; conversion afterwards may be feasible.)

References

One Answer

The following works:

% ...
  <tex>$N = R_{*} cdot f_{p} cdot n_{e} cdot f_{l} cdot f_{i} cdot f_{c} cdot L$</tex>  
% ...

startxmlsetups xml:tex
  xmlflushcontext{#1}
stopxmlsetups

Using $$ also works.

Correct answer by Dave Jarvis on May 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP