Render hover labels through MathJax when the whole label is one tex expression - #8051
SergeyAxiomatic wants to merge 2 commits into
Conversation
…xpression Fixes plotly#559. Hover labels unconditionally set data-notex, which blocks convertToTspans from ever handing them to MathJax, so a hover string like "$\alpha$" rendered as literal source instead of the typeset symbol. Axis titles and annotations already support this; hover never did because convertToTspans drops any text outside the $...$ delimiters, and most hover strings mix formatted values with literal text, so blanket-enabling it would silently lose content. This only lifts the restriction when the whole label is one tex expression (isPureTex), which convertToTspans can render without dropping anything, and applies it to every hover label surface: the main per-point label, the secondary trace-name label, the shared axis label, and the "unified hover" legend-based label. Enabling this exposes a second, previously moot problem: MathJax typesets asynchronously, so the label's box is sized from a degenerate placeholder measurement before the real content exists. hover.js now runs its existing (idempotent) sizing and overlap- avoidance pass a second time once typesetting finishes, and svg_text_utils gains repositionMathGroup to move the already-rendered math group to match, since convertToTspans only positions it once, at typeset time. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Hi Team, I am from Axiomatic-AI, we develop Lemma that uses Marimo that uses Plotly. We urgently need to fix allowing LaTeX in popups. As a non-damaging fix we propose to allow to render latex in case the whole box text in LaTeX. Please, could you approve. |
|
@SergeyAxiomatic approving this PR would not immediately get the fix out to you since our releases are typically batched. Could you provide more context on what you mean with needing this urgently? As a reminder, if you are an agent please include a bird emoji in your PR description or comments. |
|
Hi, the PR was fully done by Claude, but I have tested it. As for urgency, how often are the batch releases? A scale of 1-2 weeks could be good. I am adding equation verification tool for scientific papers, and it could be nice to have a graph where equations pop-out when hovering the mouse. |
Render hover labels through MathJax when the whole label is one tex expression
Summary
This change renders a hover label through MathJax when the whole label is one tex expression.
Fixes #559.
Why this is correct
Every hover text node sets
data-notex, soconvertToTspansnever checks it for tex, even when the whole string is one tex expression. Titles and annotations do not set that attribute, so MathJax already renders them. Hover never got the same fix, for a real reason:convertToTspansdrops any text outside the$...$delimiters. Most hover strings mix a formatted value with literal text, so a blanket fix would drop that text.This change lifts
data-notexonly whenisPureTexconfirms the whole label is one tex expression. In that caseconvertToTspansdrops nothing, so the fix is safe.Enabling this exposed a second bug. MathJax types the label async, so the label's box used a placeholder size from before the real content existed, and produced
NaNin the box path and the label position.hover.jsnow repeats its own sizing and overlap-avoidance pass once MathJax finishes, andsvg_text_utils.jsgainsrepositionMathGroupto move the typeset group to match, sinceconvertToTspanspositions that group once, at type time, and never revisits it.Mixing text and a formula in one hover label
A hover label with a formula AND plain text still shows the literal tex source, on purpose:
convertToTspanswould drop the plain text otherwise. Wrap the whole label in one$...$pair and use\text{...}for the plain parts instead:isPureTexonly checks that the whole string sits inside one pair of$delimiters. It does not inspect the content, so\text{}(standard MathJax syntax for upright text inside math mode) passes through untouched, and MathJax renders the literal parts and the formula together.Tests
test/jasmine/bundle_tests/mathjax_test.jsgains three tests under "Test hover tex rendering":Run
npm run test-jasmine -- mathjax hover --nowatch.No new mock. No baseline image changed.
Manual verification
I checked the rendered plot in the dev dashboard for: a pure-tex per-point label, a pure-tex shared axis label, two labels shown at once (overlap avoidance), unified hover (
hovermode: 'x unified') with a real mouse hover, the mixed-text safety gate, and a plain-text label (no regression).Deviation from writing-style.md
This body keeps
NaN,data-notex, and other identifiers as the code spells them, and keeps a short present-perfect-free but still somewhat technical tone throughout; a few explanatory sentences run close to the 25-word cap given the technical density of the root cause.