Copy LaTeX source from VS Code's Markdown preview — no more garbled math!
When you copy text containing math equations from VS Code's built-in Markdown preview, the result is usually garbled — each symbol ends up on its own line due to KaTeX's internal HTML rendering. This extension fixes that by intercepting the copy event and replacing rendered math with the original LaTeX source.
Before vs After
Without this extension (default VS Code behavior):
The probability density function satisfies
∫
−
∞
∞
f
(
x
)
d
x
=
1
With this extension:
The probability density function satisfies $\int_{-\infty}^{\infty} f(x)dx = 1$
Features
Inline math is copied as $...$
Block/display math is copied as $$...$$
Regular text is copied normally (the extension only activates when math is present in the selection)
Zero configuration — just install and it works
No dependencies
How It Works
VS Code renders math in Markdown preview using KaTeX, which stores the original LaTeX source in a hidden <annotation> element inside the MathML output. This extension:
Intercepts the copy event in the Markdown preview
Detects KaTeX-rendered math elements in the selection
Extracts the LaTeX source from MathML annotations
Reconstructs clean text with proper $...$ / $$...$$ delimiters
Writes it to the clipboard
Why This Extension?
VS Code's GitHub issue #153258 reports this problem, but it was closed as "not planned"
KaTeX has an official copy-tex contrib that does exactly this for web pages, but VS Code does not include it in its built-in Markdown preview
No existing Marketplace extension solves this specific problem
Requirements
VS Code 1.70.0 or later
The built-in Markdown math rendering must be enabled ("markdown.math.enabled": true, which is the default)