Skip to content

Render LaTeX math in Markdown previews - #57339

Open
loomou wants to merge 3 commits into
zed-industries:mainfrom
loomou:katex
Open

Render LaTeX math in Markdown previews#57339
loomou wants to merge 3 commits into
zed-industries:mainfrom
loomou:katex

Conversation

@loomou

@loomou loomou commented May 21, 2026

Copy link
Copy Markdown

Closes #40813

Summary

  • Enable pulldown-cmark math parsing and preserve InlineMath / DisplayMath events instead of dropping them.
  • Add Markdown math rendering through ratex, converting formulas to SVG images rendered by GPUI.
  • Support both inline $...$ and display $$...$$ formulas in Markdown rendering paths that already support embedded diagrams.
  • Share cache invalidation with embedded diagram rendering so theme/font changes refresh Mermaid and math output together.
  • Keep fallback source text visible while formulas render, or when rendering fails.
  • Add parser, cache-retention, fallback, and heading-font-size coverage for math rendering behavior.

Screenshots

2026-05-21_11-00-06 2026-05-21_11-00-46

Notes

This follows up on the open Markdown preview request in #40813 and the duplicate $ math rendering report in #57001.

I also checked the related ReX-based attempts in #53150 and #54668. Those PRs explored LaTeX math rendering through ReX-based rendering paths, but ReX has not been very active recently.

This PR uses the RaTeX instead. RaTeX is a KaTeX-compatible math renderer written in Rust: its parser architecture, symbol tables, font metrics, and layout semantics are based on KaTeX, while still producing native Rust display output. In this implementation, Zed parses and lays out formulas with ratex, exports them through ratex-svg, and renders the SVG through GPUI's existing SVG renderer.

Testing

  • Added parser tests for inline and display math event preservation.
  • Added KaTeX/ratex cache tests for fallback behavior, rapid edits, stale cache retention, and active render variants.
  • Added visual markdown tests for inline math in headings using heading font size and preserving unrelated cached formula renders after edits.

Release Notes:

  • Added LaTeX math rendering for Markdown previews.

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 21, 2026
@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label May 21, 2026
@MrSubidubi MrSubidubi added area:repl repl, jupyter, notebooks, etc area:preview/markdown Feedback for Zed's Markdown preview labels May 21, 2026
@MrSubidubi MrSubidubi self-assigned this May 21, 2026
@feitreim

Copy link
Copy Markdown
Collaborator

This is super cool, but I have found some bugs/nits.

Here the "and $v$" text is rendering like halfway through the equation.

Screenshot 2026-05-21 at 11 45 10 AM

Also it does just feel like the math text is really big? especially for the inline math that looks pretty odd.

Also out-of-line math isn't center aligned, which is generally how its done in other editors. (obsidian on the right).

Screenshot 2026-05-21 at 11 53 40 AM

Heres the markdown file:

Markdown

  • $v = [p_x - q_x, p_y - q_y]^T$
  • $v = p - q$
  • let $\mathbf{w}$ be the orthogonal vector to $\mathbf{v}$, we can use the definition of the dot product to find the components of $\mathbf{w}$ s.t. for $||\mathbf{w}|| \neq 0$, $0 = \mathbf{v} \cdot w \implies 0 = v_x \cdot w_x + v_y \cdot w_y$, from that we can take the components of $\mathbf{v}$, switch them and negate one, the choice of which we negate determines if it is to the left or the right, so for the north -> west example we would negate the first element, so $\mathbf{w} = [-p_y + q_y, p_x - q_x]^T$. \
  • Using the dot product definition $\mathbf{u} \cdot \mathbf{v} = \Vert \mathbf{u}\Vert \Vert\mathbf{v}\Vert \cos \theta$ we can see that $$\theta = 90^\circ \implies \cos \theta = 0 \implies \mathbf{u} \cdot \mathbf{v} = \Vert \mathbf{u}\Vert \Vert\mathbf{v}\Vert \cos 90^\circ = 0$$
  • If $0^\circ \leq \theta \leq 90^\circ$ then $\cos \theta \in [0, 1]$, one of the properties of the norm operator is that $\Vert \mathbf{v} \Vert \geq 0$, then there are no negative numbers in the equation and the dot product must be greater than 0.
  • For the $90^\circ \leq \theta \leq 180^\circ$ case, $\cos\theta \in [0, -1]$, the magnitudes will still be positive, so with a single negative part of the product, the resulting dot product will be less than 0.
  • let $\theta$ be the angle between $\mathbf{u}$ and $\mathbf{v}$. Based on the diagram we can find $\cos\theta = \frac{x}{\Vert \mathbf{u} \vert}$, because we $\mathbf{u}$ and $\mathbf{v}$ are unit vectors, we know that the norm of both will be $1$. which implies $x = \cos\theta$. now that we have $x$, lets look closer at the dot product of $\mathbf{u}$ and $\mathbf{v}$, $$\begin{aligned} \mathbf{u} \cdot \mathbf{v} &= \Vert \mathbf{u} \Vert \Vert \mathbf{v} \Vert \cos\theta \&= \cos\theta \&= x\end{aligned}$$
  • First we look to Q1 to find the vector $\mathbf{v}$, that is a vector on the line $\overline{pq}$. Then let $\mathbf{u}$ be a vector that is orthogonal to $\mathbf{v}$, to the left of $\mathbf{v}$. Next we let vector $\mathbf{x}$ be any vector between the line and point $x$. Then we take $\mathbf{u} \cdot \mathbf{x} = \alpha$, which from Q2 we know that if $\alpha > 0$, $\mathbf{x}$ is within $90^\circ$ of $\mathbf{u}$ to the left of the line, if $\alpha = 0$, $x$ is on the line, and if $\alpha < 0$, x is to the right of the line. We can also write this out and then simplify it if possible. $\mathbf{v}=\begin{bmatrix} p_x - q_x \ p_y - q_y \end{bmatrix} \to \mathbf{u} = \begin{bmatrix} -p_y + q_y \ p_x - q_x \end{bmatrix}. \ \mathbf{x}=\begin{bmatrix} p_x - x_x \ p_y - x_y \end{bmatrix}$ $$\begin{aligned} \mathbf{u} \cdot \mathbf{x} &= (-p_y + q_y)(p_x - x_x) + (p_x-q_x)(p_y-x_y) \ &=-p_xp_y +p_xq_y -q_yx_x +p_yx_x + p_xp_y -p_xx_y -q_xp_y +q_xx_y \&=p_xq_y -q_yx_x + p_yx_x -p_xx_y -q_xp_y + q_xx_y \&= \alpha \end{aligned}$$ \
  • The point $(4, 4)$ in $\hat{i}, \hat{j}$ space is equivalent to the point $(8, 4)$ in $\mathbf{u}, \mathbf{v}$ space, this can be solved by just looking at it for a few seconds, but more generally the change of basis formula is: $x = \mathbf{B}y$, where $y$ is the new coordinates, $x$ is the original coordinates and $\mathbf{B}$ is our basis. So the actual transformation from old to new would be $\mathbf{B}^{-1}x=y$.

Also kind of weird to me that the file/function names etc use the katex name instead of ratex.

@lnay

lnay commented May 28, 2026

Copy link
Copy Markdown
Contributor

ReX has not been very active recently

In case there is any mixup, the other works you referenced involve the KenyC fork (much more modular, last commit 3 months ago). Indeed the original rex repo is not very active. On the flip side I would be a little wary in the year 2026 of the reliability of a young project with a flashy website.

ReachForStar added a commit to ReachForStar/zed-ZH_CN that referenced this pull request Jul 25, 2026
移植上游 PR zed-industries#57339:启用 pulldown-cmark 的 ENABLE_MATH,
新增 katex.rs,公式经 RaTeX 输出 SVG 后复用 GPUI 栅格化管线,
与 mermaid 同构(后台异步渲染、缓存、主题联动、失败回退源码)。

- render_mermaid_diagrams 更名为 render_embedded_diagrams
- 保留 fork 的标题 mt_6 与 h1-h3 底边框样式
- 适配 fork 的 DivStackEntry 与 parse_markdown_with_options 签名
- 新增依赖 ratex-{layout,parser,svg,types} 0.1
@RamonOpazo

Copy link
Copy Markdown

Is this feature still considered? LaTeX-style Markdown formula preview with RaTeX is a solid approach and would greatly improve documentation and note taking.

@Cyfine

Cyfine commented Aug 15, 2026

Copy link
Copy Markdown

I don’t know why this isn’t on their priority list. Without equation rendering, it doesn’t feel feature complete to me (as well as a lot of ppl) as an editor.

@lamthanhhiep85

This comment was marked as spam.

@RamonOpazo

RamonOpazo commented Aug 17, 2026

Copy link
Copy Markdown

Wouldn't it be better to start afresh? There have been some changes in the markdown API since v11.x and it may be necessary to rethink the integration. RaTeX is super nice and rusted, so definitely a win on top of VSCode js approach.

@RamonOpazo

This comment was marked as spam.

@wesleyel

Copy link
Copy Markdown

I don’t know why this isn’t on their priority list. Without equation rendering, it doesn’t feel feature complete to me (as well as a lot of ppl) as an editor.我不明白为什么这项功能不在他们的优先事项清单上。对于我(以及很多人)来说,作为编辑器,如果没有公式渲染功能,就感觉它的功能并不完善。

@RamonOpazo @Cyfine @lamthanhhiep85

Please check this comment; this PR has implemented a highly usable Markdown preview and agent panel Markdown formulas.

#58593 (comment)

@Arcitec

Arcitec commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

There are three competing RaTeX implementation pull requests:

But keep in mind that LaTeX support was already closed in 2024 as "not planned":

#11126

And confirmed again in 2026 as "out of scope and not on our roadmap" because Zed is a code editor. It's not a LaTeX math formula renderer:

#54668 (comment)

But it was reopened (without comment), so maybe there's a chance. If so, someone will have to take the time to figure out which of all the pull requests is the cleanest solution. And more importantly: Explain why Zed needs to render math formulas.

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

Labels

area:preview/markdown Feedback for Zed's Markdown preview area:repl repl, jupyter, notebooks, etc cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support LaTex rendering in markdown preview

9 participants