bananamark-tikz

中文文档
0.2.1发布于 2026/11/03MITES Module

TikZ diagrams for bananamark: compiled to SVG with node-tikzjax in a worker thread (Node only)

原始使用文档
文档目录9 节
  1. Status
  2. Use
  3. Options
  4. Output
  5. What diagrams can use
  6. How diagrams are drawn
  7. The cache
  8. Errors
  9. Contributing

bananamark-tikz

TikZ diagrams for bananamark. With TikZ on (the dialect's tikz: true), TikZ diagrams (```tikz blocks, and tikzcd and tikzpicture environments in formulas) are placeholders in bananamark's HTML, elements holding their source; this package compiles them with node-tikzjax (a TeX engine in WebAssembly) in a worker thread and puts the SVG in their place. Node only.

Status

Version 0.2, released with bananamark (see its status): until 1.0, a minor version may change the interface. Peer dependency: bananamark of the same minor version. ES modules only; MIT licence.

npm install bananamark bananamark-tikz

Use

As a processor's plugin:

import { createProcessor, defineDialect } from 'bananamark';
import { tikz } from 'bananamark-tikz';

const diagrams = tikz({
  cache: 'cache/tikz',                  // compiled diagrams are kept here (a JSON file each); without it, in memory only
  fonts: { directory: 'site/fonts' },   // the fonts of diagrams' text are copied here
});
const processor = createProcessor({ dialect: defineDialect({ tikz: true }), plugins: [diagrams] });
const page = await processor.renderAsync(source, { file: 'groups.md', outputPath: 'site/groups/index.html' });
await processor.close();   // closes the plugin: stops its worker

Or on HTML (a whole page from a site's templates, say):

const diagrams = tikz({ cache: 'cache/tikz', fonts: { directory: 'site/fonts' } });
const html = await diagrams.render(pageHTML, { outputPath: 'site/groups/index.html' });
await diagrams.close();

render(html, page) (postprocess, as a plugin) gives the HTML with the diagrams in place. page is { outputPath, file, locale, messages }: outputPath, the page's HTML file, which the fonts' addresses are relative to; file, the file errors name (default outputPath); locale and messages, for errors (see Errors; a processor gives its plugins its own). When diagrams have text, the @font-face rules for its fonts go before </head> on a whole page, or at the start of HTML without a <head> (a page's body, as a plugin sees it).

Once every page is rendered, prune() deletes what the cache no longer needs; close() stops the worker.

Options

OptionMeaningDefault
cacheThe directory compiled diagrams are kept inin memory only
fonts{ directory }: the fonts a page's diagrams use are copied there (with their licence), and the page refers to them by their path from its HTML file; or { href(font, outputPath) }: the fonts' addressesnone: diagrams with text are an error
timeoutA diagram's longest compilation, in ms. A diagram that runs out of it is remembered for ten minutes: instances allowing no more time fail it at once15000
totalTimeoutThe time, in ms, this instance may spend compiling in all (a preview: a draft with many slow diagrams must not hold the queue); diagrams after it failno limit
workerIdleTimeoutIn ms: instances share one worker (about 0.5 s to start, 370 MB), kept that long after the last of them closeseach instance its own worker
locale, messagesThe language of errors and the caller's messages, as bananamark's options (this package's own messages: tikz_failed, tikz_command, tikz_unsafe, tikz_timeout, tikz_total_timeout, tikz_worker_exit), over the page'sthe page's; otherwise 'en'

Output

A placeholder (bm-tikz-source, whose code.bm-tikz-code shows the source until it is compiled) becomes bm-tikz-rendered, holding the SVG in span.bm-tikz-body (svg.bm-tikz-svg; Chinese characters bm-tikz-cjk). A numbered diagram is in span.bm-tikz-layout, its number in span.bm-tikz-tag, and --bm-tikz-tag-space the number's width.

What diagrams can use

  • TeX packages tikz-cd, amsmath, amssymb and array; TikZ libraries a diagram loads with \usetikzlibrary{…}.
  • The commands \coloneqq, \eqqcolon, \bra, \ket, \braket, \set, \Set and \bm, and Unicode maths symbols, Greek letters and accented Latin letters typed directly, as in KaTeX.
  • Chinese characters, drawn with the page's fonts.
  • Not bananamark's katex macros: diagrams are compiled by TeX, apart from KaTeX. There is no option for a preamble of the caller's yet.
  • Not \special, anywhere in a diagram's source (comments too): it writes into the SVG as it is. The diagram is an error (tikz_command), also when it is in the cache.
  • Only a drawing comes out: a diagram whose SVG has an element TikZ does not draw with (links, scripts, animation, embedded content), an event handler, a reference outside the picture or a markup declaration is an error (tikz_unsafe), however the TeX wrote it. Nothing is removed from an SVG; the check runs on each diagram as it is used, from the cache too.

How diagrams are drawn

  • A formula holding a diagram is compiled whole by TeX: several diagrams, \left…\right or fractions around one are laid out by TeX, following LaTeX rather than KaTeX (KaTeX's own commands are not there). A \tag around it is compiled apart and put on the right, so numbers and references stay. An inline one-line tikzcd is aligned on its cells' baseline; a tikzpicture takes the usual baseline=(node.base).
  • The SVG is the TeX engine's own drawing, with an offset of the upstream converter (0.27 pt) corrected, so that it lines up with the formula's box. The page only shows it: nothing is measured or moved in the browser, and it shows without JavaScript.
  • Chinese: the engine has no Chinese fonts. Each Han character and full-width punctuation mark is declared to TeX as a box one em wide (at the size of the text: the formula's enlargement, 1.1, is undone), 85% of the size high and 15% deep; TeX places nodes, labels and arrows around these boxes, and the SVG writes the characters as <text> where TeX put them (through dvisvgm:raw), drawn in the page's fonts, textLength keeping their width. Characters on one line are one <text>, so a word copies on one line. The size follows the mathematical style, bold and italic follow TeX's fonts, and colour and rotation TikZ's.
  • This is not CTeX or xeCJK: widths are assumed, not measured from the reader's fonts (whether a rare character shows depends on them), and there is no line breaking of Chinese text, no rules for punctuation at line ends and no vertical text. Write node names in ASCII; Chinese is for what is shown.
  • Accented Latin letters are declared as TeX's accents, so \text{Möbius} and \mathsf{Ét} can be typed directly.
  • Copying gives the Chinese characters as they are; the Western maths is in the engine's BaKoMa fonts, whose Greek letters, symbols and large brackets may copy as other characters. Diagrams have no MathML.

The cache

A compiled diagram is kept by a hash of its input (its source, whether display, its mode), the compiler's code, the engine's version and the preamble it gets (the optional lines above are in it only when the diagram uses them): changing one command compiles again only the diagrams using it.

  • prune({ maxAge }): after rendering every page, deletes the cache's files this instance did not use and nothing has used within maxAge (ms, default a week: drafts being edited online keep theirs). Returns the number deleted.
  • A diagram whose TeX fails is remembered (the 200 most recent failures), and not compiled again; one that ran out of time, for ten minutes, by instances whose timeout is no longer. A diagram several pages ask for at once is compiled once.

Errors

When a diagram does not compile, render throws a bananamark BananamarkError: its message is the file and the reason ("groups.md: TikZ compilation failed: …" and TeX's error), error.file the file, error.code and error.params the message's key and values (tikz_failed, with TeX's error as params.message; tikz_command, a diagram using \special, with the command as params.command; tikz_unsafe, an SVG with more than a drawing, with what was found as params.found; tikz_timeout and tikz_total_timeout, with seconds; tikz_worker_exit, with the worker's exit code), and error.cause the error under it. The words are in the instance's locale and messages, or else the page's (a processor gives its plugins its own), in English by default; a locale of the caller's without these keys takes English's.

Contributing

Tests: see CONTRIBUTING.md.

版本与发布记录

bananamark-tikz 共发布 4 个版本,首次发布于 2026/10/28;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.2.1latest2026/11/03今天
0.2.02026/11/012 天前
0.1.02026/10/286 天前
0.0.0-stage2026/10/286 天前

关于 bananamark-tikz 的常见问题

bananamark-tikz 是什么?

用于bananark的TikZ图表:在工作线程中使用Node -tikzjax编译为SVG(仅限Node)

如何安装 bananamark-tikz?

在项目目录运行 npm install bananamark-tikz,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:bananamark(~0.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

bananamark-tikz 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bananamark-tikz 是否存在。

bananamark-tikz 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

bananamark-tikz 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

bananamark-tikz 可以商用吗?

bananamark-tikz 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

bananamark-tikz 还在维护吗?

最新版本 0.2.1 发布于 2026/11/03(今天);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/10/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。