Fast math typesetting for the web.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Clipboard integration extension that enables copying rendered math expressions as TeX source code, preserving original LaTeX notation for easy sharing and editing.
import "katex/contrib/copy-tex";For CommonJS:
require("katex/contrib/copy-tex");The copy-tex extension automatically enhances the clipboard behavior when copying math expressions rendered by KaTeX.
// No explicit API - works automatically once imported
// Modifies global copy behavior for .katex elementsUsage Example:
<script src="katex.js"></script>
<script src="katex/contrib/copy-tex.js"></script>
<div id="math-content">
<!-- Rendered math elements -->
</div>
<script>
// Render some math
katex.render("\\frac{1}{2}", document.getElementById("math-content"));
// Now when users copy the rendered math, they get:
// - HTML format: full rendered HTML for pasting into rich text editors
// - Plain text format: TeX source code (e.g., "\frac{1}{2}")
</script>When users copy rendered math expressions:
Before copy-tex:
After copy-tex:
The extension supports configurable delimiters for copied TeX code:
interface CopyDelimiters {
/** Delimiters for inline math */
inline: [string, string];
/** Delimiters for display math */
display: [string, string];
}
const defaultCopyDelimiters: CopyDelimiters = {
inline: ['$', '$'],
display: ['$$', '$$']
};
/**
* Replace KaTeX elements with their TeX source
* @param fragment - Document fragment to process
* @param copyDelimiters - Delimiter configuration
* @returns Modified fragment with TeX source
*/
function katexReplaceWithTex(
fragment: DocumentFragment,
copyDelimiters?: CopyDelimiters
): DocumentFragment;Usage Example (Advanced):
import { katexReplaceWithTex, defaultCopyDelimiters } from "katex/contrib/copy-tex";
// Custom copy handler with LaTeX-style delimiters
const customDelimiters = {
inline: ['\\(', '\\)'],
display: ['\\[', '\\]']
};
// Manual processing for custom copy behavior
document.addEventListener('copy', function(event) {
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const fragment = range.cloneContents();
// Replace KaTeX elements with TeX source
const processed = katexReplaceWithTex(fragment, customDelimiters);
event.clipboardData.setData('text/plain', processed.textContent);
event.preventDefault();
});The extension automatically expands selections to include complete math expressions:
Behavior Example:
<p>The equation <span class="katex">E = mc²</span> shows energy equivalence.</p>
<!-- If user selects part of "mc²", the extension expands selection to include entire "E = mc²" -->
<!-- Copy result:
HTML: Full rendered equation HTML
Text: "E = mc^2" (original TeX)
-->The copy-tex extension works automatically with any rendering approach:
// React component
useEffect(() => {
import("katex/contrib/copy-tex"); // Enable copy-tex for all rendered math
}, []);
// Vue component
mounted() {
require("katex/contrib/copy-tex");
}
// Angular component
ngOnInit() {
import("katex/contrib/copy-tex");
}Copy-tex extension requires:
Fallback for older browsers:
// Check for required APIs
if (window.getSelection && document.createRange && window.ClipboardEvent) {
import("katex/contrib/copy-tex");
} else {
console.warn("Copy-tex extension requires modern browser APIs");
}Copy-tex works seamlessly with auto-rendered content:
<div id="content">
<p>Display math: $$\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}$$</p>
<p>Inline math: The formula \(x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}\) solves quadratics.</p>
</div>
<script>
import renderMathInElement from "katex/contrib/auto-render";
import "katex/contrib/copy-tex";
// First render the math
renderMathInElement(document.getElementById("content"));
// Copy-tex automatically enhances all rendered expressions
// Users can now copy any math and get TeX source in plain text
</script>The copy-tex extension:
Math not copying as TeX:
Wrong delimiters in copied text:
$ delimiterskatexReplaceWithTex with custom delimiters for different formatsSelection not expanding properly:
.katex class