This extension modifes the copy/paste behavior in any browser supporting the
so that, when selecting and copying whole KaTeX-rendered elements, the text
content of the resulting clipboard renders KaTeX elements as their LaTeX source
surrounded by specified delimiters. (The HTML content of the resulting
clipboard remains the selected HTML content, as it normally would.)
The default delimiters are
$...$ for inline math and
$$...$$ for display
math, but you can easy switch them to e.g.
copyDelimiters in the source code.
This extension isn't part of KaTeX proper, so the script should be separately
included in the page. It also provides optional custom CSS that
defines KaTeX equations as
so that they get selected all-or-nothing (and thus trigger the good behavior
provided by this extension). Without this CSS, partially selected equations
will just get the usual HTML copy/paste behavior.
<link href="https://email@example.com/dist/contrib/copy-tex.css" rel="stylesheet" type="text/css"> <script src="https://firstname.lastname@example.org/dist/contrib/copy-tex.min.js" integrity="sha384-BU/ZjHp6l8sAdA5pVWg7PDyDWxlvDtRbesZdg6/WprhHmvgodUl4JKPQH/53jvNY" crossorigin="anonymous"></script>
See index.html for an example.
(To run this example from a clone of the repository, run
in the root KaTeX directory, and then visit
with your web browser.)
If you want to build your own custom copy handler based on this one,
copy-tex.js into your codebase and replace the
This extension has been tested on Chrome, Firefox, Edge, and Safari.
Microsoft Edge does not seem to support text and HTML content in a single clipboard. In this browser, this extension will just put the text content into the clipboard.
Safari copies correctly, but the selection rectangle renders strangely
(too big) when interacting with display math
(because of the
user-select: all CSS).