jxxcarlson/etex/1.1.0

ETeX.Transform

ETeX is a lighter notation for mathematics that can be mixed freely with ordinary TeX. Where TeX has \frac{1}{n+1}, ETeX has frac(1, n+1); bare names such as alpha or int become \alpha and \int. Plain TeX passes through unchanged:

transformETeX Dict.empty "frac(1, n+1)" == "\\frac{1}{ n+1}"


transformETeX Dict.empty "\\frac{1}{n+1}" == "\\frac{1}{n+1}"

Input that cannot be parsed is returned with an [ETeX error] marker in front, so the problem is visible rather than silently dropped.

Transforming math

transformETeX : MathMacroDict -> String -> String

Transform ETeX to TeX for export: ETeX notation becomes TeX, and calls to user-defined macros are kept as macro calls (\name{...}), to be defined by the \newcommands that toLaTeXNewCommands produces.

evalStr : MathMacroDict -> String -> String

Transform ETeX to TeX for display (e.g. by KaTeX): like transformETeX, but calls to user-defined macros are expanded in place, since the renderer does not know their definitions.

User-defined macros

type alias MathMacroDict =
MathMacroDict

User-defined macros, as built by makeMacroDict.

makeMacroDict : String -> Dict String MacroBody

Build a MathMacroDict from macro definitions, one per line, in either form:

nat: mathbb N
\newcommand{\reals}{\mathbb{R}}

In the ETeX form, #1, #2, ... are the arguments. Lines that are not definitions are ignored.

toLaTeXNewCommands : String -> String

Turn macro definitions (the same lines makeMacroDict reads) into LaTeX \newcommands, for a LaTeX preamble. \newcommand and \renewcommand lines pass through unchanged.

LET ... IN

reduceLet : String -> String

Expand a LET ... IN block: each single-letter variable defined between LET and IN is replaced by its definition in the body. Input without a LET block is returned unchanged. transformETeX and evalStr do this themselves; use reduceLet for math that is not passed through them.

reduceLet "LET\n  A = x^2\nIN\n  A + 1" == "x^2 + 1"

LaTeX back to ETeX

inverseTransformETeX : MathMacroDict -> String -> String

Partial inverse of transformETeX. Parses LaTeX-flavored input and renders it back in ETeX form: braces become parentheses, multi-argument macros use comma-separated arguments, and bare macros drop the leading backslash.

inverseTransformETeX Dict.empty "\\sin{x^2}"   == "sin(x^2)"
inverseTransformETeX Dict.empty "\\frac{1}{2}" == "frac(1,2)"
inverseTransformETeX Dict.empty "\\alpha"      == "alpha"

This is a partial inverse: it handles the simple cases above and passes through expressions it does not know how to re-express in ETeX form.