1
$\begingroup$

The ToExpression command works well with scalars \frac{x}{\sqrt{5}} \mu S_1

ToExpression["\\frac{x}{\\sqrt{5}} \\mu S_1", TeXForm] 

but fails when trying to use the aligned environment:

ToExpression["\\begin{aligned} \\frac{dS}{dt} & = - \\frac{S}{N} \\left( \\beta_1 i_1 + \\beta_2 \ i_2 \\right) + \\sigma_1 R_1 + \\sigma_2 R_2, & & \\\\ \\frac{di_1}{dt} & = \\frac{\\beta_1}{N} S i_1 - \\gamma_1 i_1, & \ \\frac{di_2}{dt} & = \\frac{\\beta_2}{N} S i_2 - \\gamma_2 i_2 \\end{aligned}", TeXForm] 

giving a lot of ErrorBoxes.

Is there a way to import this $\TeX$ expression to Mathematica? The problem disappears when replacing the 'aligned' environment manually by a list:

ToExpression["\\{\\frac{dS}{dt} =\\frac{\\beta_1}{N} (N-i_1-R_1-i_2- \ \\epsilon R_2) i_1- \\gamma_1 i_1,\\frac{dS}{dt} \ =\\frac{\\beta_1}{N} (N-i_1-R_1-i_2- \\epsilon R_2) i_1- \\gamma_1 \ i_1\\}", TeXForm] 

which compiles correctly. So, it seems this part of the conversion, i.e. removing the 'aligned' , should be done manually, which makes sense, for how could Mathematica have guessed what to do with the 'aligned'?

$\endgroup$
1
  • 2
    $\begingroup$ Hm, if you look at TeXForm[{{a + b, c}, {d, e}}], you see that it gives array. However, when parsing back, ToExpression[ToString@TeXForm[{{a + b, b, c}, {d, e, f}}], TeXForm], it gives a weird looking matrix ... $\endgroup$ Commented Sep 23, 2023 at 10:54

1 Answer 1

1
$\begingroup$

Its complicated, but Import the LaTeX string and export it to a notebook is working.

 Export[path=Environment["userprofile"] <> "\\Desktop\\my.nb", ImportString["\\begin{aligned} \frac{dS}{dt} & = - \frac{S}{N} \left( \beta_1 I_1 + \beta_2 I_2 \right) + \sigma_1 R_1 + \sigma_2 R_2, & & \\ \frac{dI_1}{dt} & = \frac{\beta_1}{N} S I_1 - \gamma_1 I_1, & \frac{dI_2}{dt} & = \frac{\beta_2}{N} S I_2 - \gamma_2 I_2, \\ \frac{dR_1}{dt} & = \gamma_1 I_1 - \sigma_1 R_1, & \frac{dR_2}{dt} & = \gamma_2 I_2 - \sigma_2 R_2. \end{aligned}, "LaTex" ] ] Run[path ] 

Probably there exists a way to proceed with the cell expression directly without creating a notebook.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.