Skip to content

Public API to get computed metrics (width/height/depth) without rendering to the DOM #3077

Open

Description

@archishadatta

Is your feature request related to a problem?

Any app that needs to know how big a LaTeX expression will render — before or without inserting it into the visible page — currently has to call convertLatexToMarkup(), insert the resulting markup into a hidden/off-screen element, force a layout, and read the size back with getBoundingClientRect(). This works, but it costs a synchronous forced reflow per measurement, and it means keeping a hidden DOM mirror whose styling (font, size, weight) has to stay in exact sync with wherever the real content renders.

What I found looking at the source

Internally, MathLive already computes exact width/height/depth for every box using font-metrics tables (src/core/box.ts, getCharacterMetrics, horizontalLayout) — entirely without touching the DOM. That's genuinely the hard part (accurate math typesetting metrics), and it's already solved here. The computed numbers just aren't exposed by any public export — convertLatexToMarkup and friends only return markup, not the dimensions that went into producing it.

Describe the solution you'd like

A small public function, something like:

function getLatexMetrics(latex: string, options?: SomeOptions): { width: number; height: number; depth: number }

that returns the box tree's already-computed dimensions directly, with no DOM node created. Naming/shape totally open to whatever fits MathLive's existing conventions better.

Who would use this

Anyone doing custom layout around math content — canvas/SVG-based renderers, print layout, custom editors — that needs a size before deciding where/how to place the expression, without paying for a DOM round-trip for a number MathLive already calculated internally.

Additional context

Happy to put together a PR if this direction seems reasonable — wanted to check on the API shape first per the contributing guide.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions