QuantEcon / QuantEcon/quantecon-plugins.mystmd
MyST TikZ Plugin: Render TikZ/PGFPlots/tikzcd diagrams at build time
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 0
- Forks
- 0
- Avg merge
- 2h 52m
- Merged PRs (30d)
- 7
Description
MyST TikZ Plugin: Render TikZ/PGFPlots/tikzcd diagrams at build time
Summary
Create a MyST Markdown plugin that renders TikZ, PGFPlots, and tikzcd LaTeX code into SVG images at build time. This would allow TikZ diagrams to be authored natively in MyST Markdown documents rather than requiring a separate pre-rendering step.
Background
The DP Volume II book conversion from LaTeX to MyST Markdown includes 9 TikZ/PGFPlots/tikzcd figures. Currently these are handled via a pre-rendering script (render_tikz.py) that:
- Wraps TikZ code in a
standaloneLaTeX document - Compiles with XeLaTeX
- Converts PDF → SVG via
pdf2svg - Outputs SVG files to
mystmd/figures/
This works but requires a manual step outside the MyST build pipeline.
Proposed Plugin
A MyST plugin providing a {tikz} directive:
```{tikz}
:label: fig-my-diagram
:caption: My TikZ diagram
\begin{tikzpicture}
\draw (0,0) circle (1cm);
\end{tikzpicture}
```
Features
- Directive types:
{tikz}for tikzpicture,{pgfplot}for pgfplots,{tikzcd}for commutative diagrams - Build-time compilation: XeLaTeX + pdf2svg (or dvisvgm) to produce SVG
- Caching: Hash-based caching to avoid recompilation when source hasn't changed
- Configurable preamble: Custom packages, TikZ libraries, and macros via
myst.yml - Error reporting: Surface LaTeX compilation errors as MyST build warnings
- Font support: Configurable font setup (e.g., STIX Two for academic books)
Plugin Architecture
Following the MyST plugin system:
- Type: Executable plugin (shells out to XeLaTeX)
- Registration:
project.pluginsinmyst.yml - Output: Replaces directive AST node with an image node pointing to the generated SVG
- Cache location:
.myst/tikz-cache/with hash-indexed entries
Configuration
# myst.yml
project:
plugins:
- myst-tikz-plugin
tikz:
engine: xelatex # or pdflatex, lualatex
converter: pdf2svg # or dvisvgm
packages:
- tikz-cd
- pgfplots
libraries:
- positioning
- arrows.meta
- decorations.pathreplacing
preamble: |
\usepackage{fontspec}
\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}
macros:
\RR: \mathbb{R}
\EE: \mathbb{E}
Alternatives Considered
- Pre-rendering script (current approach) — Works but adds a manual step outside the MyST build
- TikZJax (browser-side WebAssembly) — Compiles TikZ in the browser via tikzjax. Increases page load time, adds ~5MB WASM dependency, and doesn't support all TikZ libraries
Related
- MyST Plugin System
- TikZJax — TikZ via WebAssembly in browser
- sphinxcontrib-tikz — Similar plugin for Sphinx
- QuantEcon/book-dp2#140 — MyST conversion PR with TikZ pre-rendering
Priority
Low — the pre-rendering approach works well for the current project. This would be valuable as a general-purpose MyST extension for the broader community.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the existing render_tikz.py pre-rendering script and the MyST plugin system documentation. The work is done when a registered plugin supports the tikz, pgfplot, and tikzcd directives with build-time SVG rendering, caching, configurable LaTeX settings, and surfaced compilation errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, latex
- Domain
- build-system, documentation
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100