Skip to main content

Crate mxblocks_tex

Crate mxblocks_tex 

Source
Expand description

Math messages: a formula rendered to a PNG by the mxblocks-math program (packages/mxblocks-math/ in this repository) and sent as an m.image.

The renderer’s contract: mxblocks-math render [--inline] --out PATH.png - with the LaTeX on stdin, exit 0 and one JSON line on stdout ({"mimetype":"image/png","w":W,"h":H,"size":BYTES}, w/h the display size of a 2x PNG); non-zero with the TeX error on stderr. Any program that keeps it works (Renderer::new takes its path).

The event (math_content): msgtype: m.image with the upload’s url and info, filename: math.png, body the raw LaTeX (readable on clients that render nothing), formatted_body MSC2191 markup (maths_html: <div data-mx-maths="…"><code>…</code></div>, a <span> inline, which Element’s LaTeX labs flag renders). The sender adds the mxblocks payload with the math block (with_caption). A caption goes first in body and formatted_body (a blank line, a <p>), and on the math block as caption.

§mxblocks-tex

Math messages for mxblocks: render a formula with the mxblocks-math program (packages/mxblocks-math/) and build the m.image event that carries it, with MSC2191 markup (data-mx-maths) for clients that render LaTeX and the raw TeX as the plain body.

The renderer contract: mxblocks-math render [--inline] --out PATH.png -, TeX on stdin; exit 0 with one JSON line on stdout ({"mimetype":"image/png","w":W,"h":H,"size":BYTES}, w/h the display size of a 2x PNG), or non-zero with the TeX error on stderr.

let r = mxblocks_tex::Renderer::new("mxblocks-math").render(r"e^{i\pi} + 1 = 0", true).await?;
// Upload r.png with your Matrix client, then pass its returned mxc URI.
let mxc = "mxc://example.org/rendered";
let content = mxblocks_tex::math_content(r"e^{i\pi} + 1 = 0", true, "", &r, &mxc, None);
assert_eq!(content["msgtype"], "m.image");

Apache-2.0.

Structs§

Rendered
A rendered formula.
Renderer
A renderer program and its limits.

Constants§

DEFAULT_MAX_PNG
The default largest PNG accepted, in bytes.
DEFAULT_RENDERER
The default renderer (on PATH).
DEFAULT_TIMEOUT
The default time the renderer may take.

Functions§

html_escape
HTML-escaped s (quotes too).
math_content
The m.image content for a rendered formula uploaded to mxc, without the mxblocks payload (write with_caption’s under the content key). caption: text to go with the formula, if any; reply_to: an event to reply to.
maths_html
MSC2191 markup for tex: a div (display) or span (inline) with data-mx-maths and the source in <code> for clients without it.
pick_renderer
The first non-blank of choices (an env var, a config value, …), trimmed, else default.
with_caption
The payload with the caption put on its math block (caption, kept only when it is one line of at most MAX_MATH_LINE characters, as the spec requires; a longer caption stays in the body only).