JMM’s notes on

ConTeXt

Nix

Nix package is texliveConTeXt.

For setting up fonts, I have an environment loaded through direnv. In a default.nix I’ve got:

{ pkgs ? import <nixpkgs> {} }:

with pkgs; {
  myenv = buildEnv {
    name = "MyEnv";
    paths = [
      texliveConTeXt
      noto-fonts
      dejavu_fonts
    ];
  };
}

Then in a .envrc I have:

#!/usr/bin/env bash
# .envrc files are run in bash,
# They just set any variables that are exported.

if [ ! -d "myenv" ]; then
    echo "You should build the myenv directory"
    echo "Probably using: nix build -vL -f . myenv -o myenv"
else
    :
fi

PATH_add myenv/bin

export OSFONTDIR=$PWD/myenv/share/fonts

# Local Variables:
# mode: sh
# sh-shell: bash
# End:

The export OSFONTDIR=$PWD/myenv/share/fonts line here is important if you want DejaVu Sans as a font.

XML

ConTeXt looks interesting because it can do XML typesetting (see https://wiki.contextgarden.net/XML). I’m kinda having trouble with WeasyPrint (see my WeasyPrint notes), so I thought I’d try ConTeXt.

You really should look at the PDF manuals instead of looking for web pages on documentation. Dealing with XML in ConTeXt MkIV has way more info on \xmlfilter than the wiki.

\startxmlsetups xml:presets:all
\xmlsetsetup {#1} {*} {xml:*}
\xmlsetsetup{#1}
    {div[@aria-hidden='true']}
    {xml:ariahidden:div}
\xmlsetsetup{#1}
    {span[@aria-hidden='true']}
    {xml:ariahidden:span}
\xmlsetsetup{#1}
    {span[@class='citation']}
    {xml:span:citation}
\stopxmlsetups

\xmlregistersetup{xml:presets:all}

\setupexternalfigures[location={local,default}]

\startxmlsetups xml:html
  \mainlanguage[\xmlatt{#1}{lang}]
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:body
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:main
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:section
  \startchapter[title=\xmltext{#1}{h2}]
    \xmlflush{#1}
  \stopchapter
\stopxmlsetups

\startxmlsetups xml:h3
  \startsection[title=\xmltext{#1}{.}]
  \stopsection
\stopxmlsetups

\startxmlsetups xml:p
  \startparagraph
    \xmlflush{#1}
  \stopparagraph
\stopxmlsetups

\startxmlsetups xml:span
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:ol
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:li
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:div
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:a
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:span:citation
  \bgroup\sc\xmlflush{#1}\egroup
\stopxmlsetups

\startxmlsetups xml:figcaption
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:fig-title
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:figure
\placefigure{\xmltext{#1}{figcaption/fig-title}}{\externalfigure[\xmlattribute{#1}{img}{src}]}
\stopxmlsetups
context --environment=environment.tex source.xml

It seems to be important for the source.xml to have an .xml extension. If you have a .xhtml extension, it won’t work unless you add the --forcexml argument.

Mapping a command on matching nodes

Use \xmlfilter. (Dunno where documentation or code is.)

\startxmlsetups xml:figure
  \placefigure[][\xmlatt{#1}{id}]{\xmltext{#1}{figcaption/fig-title}}{\xmlfilter{#1}{/img/command(xml:img)}}
\stopxmlsetups

\startxmlsetups xml:img
\externalfigure[\xmlatt{#1}{src}]
\stopxmlsetups

HTML tables

Another thing that makes ConTeXt cool is that it’s pretty easy to use HTML tables. See https://wiki.contextgarden.net/TABLE.

Here’s a setup I’m using for my thesis:

\startxmlsetups xml:table:hasnumber
\placetable[][\xmlatt{#1}{id}]{\xmltext{#1}{caption/tab-title}}\bgroup\tfx
\bTABLE[option=stretch]\xmlflush{#1}\eTABLE
\egroup
\stopxmlsetups

\startxmlsetups xml:table
\bgroup\tfx
\bTABLE[option=stretch]\xmlflush{#1}\eTABLE
\egroup
\stopxmlsetups

\startxmlsetups xml:thead
\bTABLEhead\xmlflush{#1}\eTABLEhead
\stopxmlsetups

\startxmlsetups xml:tbody
\bTABLEbody\xmlflush{#1}\eTABLEbody
\stopxmlsetups

\startxmlsetups xml:tr
\bTR\xmlflush{#1}\eTR
\stopxmlsetups

\startxmlsetups xml:th
\bTH\xmlflush{#1}\eTH
\stopxmlsetups

\startxmlsetups xml:td
\bTD\xmlflush{#1}\eTD
\stopxmlsetups

Math

TeX math

If you want to put TeX math formulas in a <tex-math/> element, put something like:

\startxmlsetups xml:tex-math
  \mathematics{\xmlflushcontext{#1}}
\stopxmlsetups

MathML

ConTeXt has support for MathML (see also my MathML notes), but I need to figure out how to set it up. Maybe see https://wiki.contextgarden.net/MathML_code_examples and http://www.pragma-ade.nl/general/manuals/mmlprime.pdf. It seems like you need to include \usemodule[mathml], but I’m not sure how to actually process MathML. Maybe see http://www.pragma-ade.nl/general/manuals/math-mkiv.pdf.

Maybe there’s less motivation these days to support MathML because it changes and deprecates things, leading to instability? See http://www.pragma-ade.nl/general/manuals/mathincontext-paper.pdf.

See https://source.contextgarden.net/tex/context/modules/mkiv/x-mathml.mkiv?search=mathml. Haven’t figured it out yet.

Hugo Landau (who’s got this great blog), showed that ConTeXt doesn’t do XML namespacing correctly for MathML.

Ah, I think I maybe figured it out kinda by staring at https://source.contextgarden.net/tex/context/modules/mkiv/x-mathml.mkiv?search=mathml for a while. Normally, after you use \usemodule[mathml], MathML will actually work. But if you (like me) have something like

\startxmlsetups xml:presets:all
\xmlsetsetup {#1} {*} {xml:*}
…
\stopxmlsetups

it’ll actually stop working. Probably something with the MathML definitions getting overridden. So, what you need to do is add \xmlsetsetup {#1}{mml:*}{mml:*} after the \xmlsetsetup {#1} {*} {xml:*}. Thus:

\usemodule[mathml]
\startxmlsetups xml:presets:all
\xmlsetsetup {#1} {*} {xml:*}
\xmlsetsetup {#1}{mml:*}{mml:*}
…
\stopxmlsetups

As you can probably tell, I don’t really understand \xmlsetsetup, or if you can select by namespacing or what. I have no clue how it knows (if it does at all) if something’s in the proper XML namespace. I don’t think it actually does, because I can change the xmlns="http://www.w3.org/INVALID_INVALID/MathML" to an invalid value and it’ll still work. Seems like ConTeXt is not really all that namespace-aware.

MathML example

Adapted from https://wiki.contextgarden.net/MathML_Code_Examples.

\usemodule[mathml]
\starttext
\xmlprocessdata{}{
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
 <mtable style="math-style: normal">
  <mtr>
   <mtd>
    <mrow>
     <mi>RU</mi>
     <mo>⁡</mo>
    </mrow>
    <mrow>
     <mo fence="true" form="prefix" rspace="0.10em">(</mo>
     <mi>lottery</mi>
     <mo>|</mo>
     <mi>ρ</mi>
     <mo separator="true">,</mo>
     <mi>κ</mi>
     <mo fence="true" form="postfix">)</mo>
    </mrow>
   </mtd>
   <mtd>
    <mo>=</mo>
   </mtd>
   <mtd>
    <msup>
     <mrow>
      <mrow>
       <mi>V</mi>
       <mo>⁡</mo>
      </mrow>
      <mrow>
       <mo>(</mo>
       <mi>lottery</mi>
       <mo>)</mo>
      </mrow>
     </mrow>
     <mi>ρ</mi>
    </msup>
    <mo>−</mo>
    <mrow>
     <mi>κ</mi>
     <mo>⁢</mo>
     <msqrt>
      <mrow>
       <mrow>
	<mi>Var</mi>
	<mo>⁡</mo>
	<mrow>
	 <mo>(</mo>
	 <mi>lottery</mi>
	 <mo>)</mo>
	</mrow>
       </mrow>
      </mrow>
     </msqrt>
    </mrow>
   </mtd>
  </mtr>
  <mtr>
   <mtd>
    <mi>RU</mi>
    <mo>⁡</mo>
    <mrow>
     <mo fence="true" form="prefix" rspace="0.10em">(</mo>
     <mi>surebet</mi>
     <!-- <mo fence="true" form="prefix" lspace="0.5em" rspace="0.5em">|</mo> -->
     <mo>|</mo>
     <mi>ρ</mi>
     <mo separator="true">,</mo>
     <mi>κ</mi>
     <mo fence="true" form="postfix">)</mo>
    </mrow>
   </mtd>
   <mtd>
    <mo>=</mo>
   </mtd>
   <mtd>
    <msup>
     <mrow>
      <mrow>
       <mi>V</mi>
       <mo>⁡</mo>
      </mrow>
      <mrow>
       <mo>(</mo>
       <mi>surebet</mi>
       <mo>)</mo>
      </mrow>
     </mrow>
     <mi>ρ</mi>
    </msup>
    <mo>−</mo>
    <mrow>
     <mi>κ</mi>
     <mo>⁢</mo>
     <msqrt>
      <mrow>
       <mrow>
	<mi>Var</mi>
	<mo>⁡</mo>
	<mrow>
	 <mo>(</mo>
	 <mi>surebet</mi>
	 <mo>)</mo>
	</mrow>
       </mrow>
      </mrow>
     </msqrt>
    </mrow>
   </mtd>
  </mtr>
 </mtable>
</math>
}{}
\stoptext

Some issues with rendering

  • 2025-03-18: <annotation> wasn’t working for me, tried removing it.
  • 2025-03-18: Looks like <semantics> block makes math disappear for me.
  • ConTeXt seems like it’ll try to find a TeX <annotation/>, but doesn’t fall back to semantics otherwise?
    Patching that out with “document.querySelectorAll("math:has(semantics):not(:has(annotation)) > semantics").forEach(x => { x.replaceWith(...x.childNodes); });
  • Need to figure out how to get “\begin{cases}” recognized. 2025-03-18 11:27: Never mind, you probably can’t.
  • ConTeXt supports asciimath annotation encodings, but I need to import it.
    Use \usemodule[asciimath]. Find out that it weirdly renders quotes in “(2 + "cool")/"dude"
  • ConTeXt doesn’t seem to support “\operatorname{}
    See https://www.mail-archive.com/ntg-context@ntg.nl/msg78287.html
  • Also no “\mathsf
    See https://www.mail-archive.com/ntg-context@ntg.nl/msg83667.html and https://tex.stackexchange.com/questions/506992/setting-up-math-fonts-in-context.
    Solution: Use \let\mathsf=\mathss
  • Did have some issue with the “INVISIBLE TIMES” character. Just removing it for now.

Emacs

AUCTeX has a context-mode. Haven’t really looked into it, but I’ll probably need to undo a lot of my LaTeX setup in my .emacs.

Citations and References

See

Issues