Solutions for copy-n-paste text with LaTeX formulas in it

I had the idea to start copy-n-pasting some great answers from to my own notes. I quickly discovered that formulas aren’t quite copy-able :frowning: along with the text - getting garbage when I do that.

This is a Wiki Post! Let’s compile a list of best solutions for this problem.

The complete solution involves two steps:

  1. copy: being able to copy the source (usually you can’t just copy wysiwyg style)
  2. paste: pasting it somewhere where it can be rendered back

Note: we don’t want just the Latex formulas - we want the whole text with some Latex in it. This is for quickly saving interesting/useful notes, not just for writing your own paper or post, where you want to inject some latex. So OCR solutions don’t quite fit here, since even if they get the rest of the text - most likely they will lose formatting.

1. How to copy text that has \LaTeX in it

Here is a list of main sources where the text with \LaTeX formulas copying is usually done from:


Say you want to copy a definition along with the formula from this page.

Step 1. Click on [Edit] link of the desired section (or the whole doc), which will take you to here.


Step 2. And now you can grab the formula and any text around it:

A regularization term (or regularizer) <math>R(f)</math> is added to a [[Loss functions for classification|loss function]]:
: <math>\min_f \sum_{i=1}^{n} V(f(x_i), y_i) + \lambda R(f)</math>

Step 3. The MD editors won’t know what to do with <math>/</math> tags, so these tags will need to be replaced with $ or ($$ in some editors).

Probably one could write a quick sanitizer in greasemonkey style or something, or use your editor, or go a command line way:

perl -pi -e 's|</?math>|\$|g' 

So now we get back:

A regularization term (or regularizer) $R(f)$ is added to a [[Loss functions for classification|loss function]]:
$\min_f \sum_{i=1}^{n} V(f(x_i), y_i) + \lambda R(f)$

and it gets rendered w/o any further effort:

A regularization term (or regularizer) R(f) is added to a [[Loss functions for classification|loss function]]:
\min_f \sum_{i=1}^{n} V(f(x_i), y_i) + \lambda R(f)

Stackoverflow network (stats, math, etc.)

Say you want to copy this answer with \LaTeX formulas in it.

Step 1. Click on edit the answer:


Step 2. grab what you need, e.g.:


With L1 regularization, you penalize the model by a loss function $L_1(w)$ = $\Sigma_i |w_i|$.

This is already a perfect markdown! Just paste it into your markdown editor that supports MathJax if you want the preview.

With L1 regularization, you penalize the model by a loss function L_1(w) = \Sigma_i |w_i|.

Alternatively, tell your browser to show you the source (usually Ctrl-U) and grab the text with the formula, but it’s less good since it’s much more difficult to find what you need and when you do not all editors support mathjax renderding inside <p></p> so you have the <p> tags to remove, e.g. with perl:

perl -pi -e 's|</?p>||g' 

2. Where to paste to

The easiest way is to paste it into a markdown aware editor, like this forum, but perhaps you’d like to save the information in your own notes, so let’s list some of them that support \LaTeX.

Online MD editors supporting \LaTeX MathJax:

  • stackedit - works great, and can take even MathJax inside <p></p>
  • markx - same as stackedit

Ubuntu MD editors supporting \LaTeX MathJax:

  • typora - a nice wysiwyg md editor with MathJax support!
    • can’t handle $foo$ inside <p></p> - need to remove the html
  • - two panels md editor
    • I wasn’t able to get $foo$ to work, despite its snapshots saying that it should be able to do that - only $$foo$$ which makes it less handy as it requires manual editing to add extra $$

Wishlist: Direct Copy-n-Paste

Ideally, of course, one should be able to copy-n-paste directly from the web into an editor and have all the markup/markdown preserved, including latex formulas - if you find a direct way please share.

One advantage for example of using say OpenOffice/LibreOffice is that it can automatically save remote images locally - the markdown is not great that way, since the link rot is very high, as always, so it’s not a safe way to save notes if they involve images - we want them to be local.

This is a wikipost - Please contribute and enhance! Thank you!

1 Like

The best way to do this is Mathpix Snip

It allows you to just take a screenshot of the equation you want by just clicking and dragging over the equations you need and it will output it in LaTeX.

Here is the workflow (taken from the website):

Most likely using deep learning, so a great way to use deep learning to help take better deep learning notes!

1 Like

This is good, but it’s not addressing my need. There are many tools that let you copy the Latex - this is a very inefficient way if you want to copy the whole block of text with some latex in it.

What I’m after is a quick copy-n-paste of the whole text. I will clarify the first post.