Pasting $\LaTeX$ into a Mathematica notebook

29

12

I have read that $\LaTeX$ source can be directly pasted into a Mathematica notebook. However, I have been unsuccessful in this regard. An example of the errors received are:

$\frac{1}{2}$
Syntax::sntxb: Expression cannot begin with "$ frac{1}{2}$".
Syntax::tsntxi: "$ frac" is incomplete; more input is needed.
Syntax::sntxi: Incomplete expression; more input is needed .

This is not remediated by removal of the $ signs, placement of two $ signs ($$), or use of other $\LaTeX$ math mode delimiters (e.g., \[ \], \begin{displaymath}\end{displaymath}, \begin{equation}\end{equation}, etc). I do not wish to import an entire .tex file as I merely want to pass a few select equations. Please advise as to my error. Thank you.

Edit: I am using Mathematica 8.0.1.0.

user001

Posted 2012-02-22T03:18:05.217

Reputation: 1 287

Are you prompted whether you want to paste literal text or the corresponding typeset expression when you try to paste? Also, what version of Mathematica are you using? – Andy Ross – 2012-02-22T03:34:59.510

I am using Mathematica version 8 (see edited post). I receive no prompts when I paste -- the $TeX$ markup is simply pasted directly into a cell in the notebook. Thank you. – user001 – 2012-02-22T03:38:46.080

2I just learned that the prompt is controlled via GlobalOptions > MessageOptions > TeXPasteWarning in the Options Inspector which can be found under Edit > Preferences > Advanced. Yours may be set to False? – Andy Ross – 2012-02-22T04:20:46.317

1Thanks for looking into that and letting me know. Despite empirical evidence to the contrary, the TexPasteWarning parameter was set to True. Rebooting Mathematica did not enable prompting either. – user001 – 2012-02-22T04:49:13.163

@Andy please add that valuable information to your answer. – Mr.Wizard – 2012-02-22T12:33:24.813

I am using 8.0.4 and it only worked when pasting $\frac {1} {2}$ and then with TeXPasteWarning enabled the dialog gives me option of accepting this format without further warnings. – Ailton Andrade de Oliveira – 2012-03-02T00:50:59.003

Answers

29

I'm prompted by Mathematica when pasting (using 8.0.4) so I don't have this issue. The following seems to do the trick though..

ToExpression["\\frac{1}{2}", TeXForm]

I would expect others might have more illuminating responses to this.

EDIT:

The prompt I referred to is controlled via GlobalOptions > MessageOptions > TeXPasteWarning in the Options Inspector which can be found under Edit > Preferences > Advanced. If this is set to False you won't get prompted as to how you would like to paste the input.

Andy Ross

Posted 2012-02-22T03:18:05.217

Reputation: 18 640

The strings must contain double backslashes, which can be problematic when copying from sources. I have not found a convenient, automatic way to double the backslashes once inside MMA, because MMA interprets single backslashes as escaping the following character. In "\frac{1}{2}", the "\f" is interpreted as a line feed. I suppose StringReplace could be brought to bear, but it would take considerable work to make a robust backslash-doubling transform. – Reb.Cabin – 2014-03-16T19:11:55.543

Excellent, that works. Thank you very much. I suppose I can try rebooting and see if Mathematica starts issuing prompts. – user001 – 2012-02-22T03:47:16.957

5

I find pasting $\frac{1}{2}$ works but pasting \frac{1}{2} does not. So the dollar signs seems necessary, although I realize that doesn't fix your problem it may be a useful hint to other users.

Virgo

Posted 2012-02-22T03:18:05.217

Reputation: 335

1

Let's assume I have the following $\LaTeX$ expression that I want to include in a Mathematica notebook

$\frac{d P(r)}{d r}=-\frac{G M \rho}{r^2}$

If I copy the expression, including the $...$ (which are essential for Mathematica to recognize $\LaTeX$) and paste it in a Mathematica notebook, the following dialog box appears

enter image description here

And after clicking the button "yes", the following expression appears in the notebook (using Mathematica 12.1), as described in the documentation

enter image description here

The related issue of converting $\LaTeX$ into Mathematica input form is discussed in this answer.


EDIT for Mathematica 12.2

Mathematica 12.2 in December 2020 introduced a new menu "Inline TeX Input" to write $\TeX$ or $\LaTeX$ input directly. Selecting the option from the Insert menu (or typing Ctrl+4)

enter image description here

Produces the following box, where one can type $\LaTeX$ directly

enter image description here

Clicking away from the box converts the $\LaTeX$ into Mathematica input

enter image description here

divenex

Posted 2012-02-22T03:18:05.217

Reputation: 305

Hi divenex i do not what version of Mathematica you use but i just download a version trial 12.2.0 (the wesite of Mathematica is horrrible it is totally a mess) and I try paste formulas from MAthpix into latex for use as standar form and it is not work properly .I do not understood how mathematica do not check properly a program before to sell it , and whats mean clicling away??? – capea – 2020-12-31T16:02:43.427

@capea it seems your problem may be with mathpix & learning to use Mathematica in itself. “Clicking away” means exactly that, clicking out of the input field that is parses the latex input. At any rate, this is a new feature, and such issues may be expected with something just added to a program with such widely varied functionality. – CA Trevillian – 2021-01-01T19:29:44.540

Thanks CA trevillan , if you see the questions of other users you realize that the problem is not the user's I try only to help , anyway MAthematica it is a great progam – capea – 2021-01-01T20:52:24.863