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 doesn’t seem to work.

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

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.

Issues