Skip to content

feat: Add support for TikZJax diagrams.#337

Draft
LukeBriton wants to merge 3 commits intooleeskild:mainfrom
LukeBriton:main
Draft

feat: Add support for TikZJax diagrams.#337
LukeBriton wants to merge 3 commits intooleeskild:mainfrom
LukeBriton:main

Conversation

@LukeBriton
Copy link

As requested in

utilizing prinsss/node-tikzjax, now Digital Garden supports TikZJax diagrams:

image image

Check out https://mathematics-deployed.vercel.app/TikZJax/ for more examples, code of which can be found in https://github.com/LukeBriton/mathematics-deployed/blob/main/src/site/notes/TikZJax.md.

PS: Some commutative diagrams from the package tikz-cd are just that small, see the case of artisticat1/obsidian-tikzjax#42. I have tried to zoom them bigger, however that would result in other TikZ diagrams being overlapped or partly invisible.

@LukeBriton
Copy link
Author

I haven't accomplished the function to invert font/arrow color (or at least black ones?) for dark themes as MathJax formulae would be, maybe that's some TODO.

Copy link
Owner

@oleeskild oleeskild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you! I've made comments about some stuff I'd like to see fixed before merging it in.

.eleventy.js Outdated
const { parse } = require("node-html-parser");
const htmlMinifier = require("html-minifier-terser");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const tex2svg = require("node-tikzjax").default;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider lazy-loading this inside the transform on first use, so the TeX engine isn't loaded on builds with no TikZ blocks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return content;
});

eleventyConfig.addTransform("render-tikzjax", (() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is registered after htmlMinifier, so in production the minifier runs first. Consider moving it before htmlMinifier to avoid fragile ordering.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

eleventyConfig.addTransform("render-tikzjax", (() => {
let tikzQueue = Promise.resolve();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining why rendering is serialized (TeX engine likely isn't concurrency-safe), since the promise chain pattern is non-obvious.
SOmething like: // Serialize renders — the TeX engine doesn't support concurrent invocations

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.eleventy.js Outdated
const svg = await run;

const svgElement = parse(svg)
.querySelector("svg") // after zooming some pictures could go wrong,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

querySelector("svg") can return null if tex2svg produces unexpected output. Add a null check before calling .setAttribute().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.eleventy.js Outdated
console.warn("\n[TikZJax] render failed at:", outputPath);
console.warn("[TikZJax] TeX source (first 400 chars):\n", texSource.slice(0, 400));
console.warn("[TikZJax] Warn:", e);
block.replaceWith(`<pre><code class="language-tikz">TikZ render failed. See build log.\n\n${(texSource)}</code></pre>`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

texSource is inserted into HTML unescaped. If the TeX source contains < or & and rendering fails, they'll be interpreted as HTML. Either escape it, or log it to the console instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukeBriton LukeBriton marked this pull request as draft March 1, 2026 16:00
@LukeBriton LukeBriton requested a review from oleeskild March 5, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants