user191919
user191919

Reputation: 754

Mathematica and Latex

I am constantly using the mathematica software and using TeXForm command to go back and forth between the calculations and the latex document I'm typesetting. However, mathematica won't allow me to define variables with underscore, which I constantly need in my latex document. Does anybody know how to create variables with "smarter" names in mathematica? In a broader sense, what is the best way to integrate the use of mathematica and latex? Thanks.

Upvotes: 2

Views: 1630

Answers (1)

sacratus
sacratus

Reputation: 146

first of all, Mathematica allows you to define variables with underscore.

Subscript[x, 1] = 3

The shortcut for this ist [ctr]+[_]

If you convert a subscript variable with TeXForm, you'll get:

x_1

I prefer to not use the subscript notation for normal variables, because you can not easily see if a variable has allready a value in this notation. So you might just write

x1

We now want to transform these kind of variable names to the subscript notation in TeXForm. One way to do this is with StringPattern.

1.Transform your expression to a String in TeXForm:

In[360]:= ToString[(-b+y1) ((b-y1)/(b-y2))^(-(w10/(x\[Gamma]1-\[Omega]2))), TeXForm]
Out[360]= (\text{y1}-b) \left(\frac{b-\text{y1}}{b-\text{y2}}\right)^{-\frac{\text{w10}}{\text{x$\gamma $1}-\text{$\omega $2}}}

2.Replace this specific String Pattern to the subscript notation of LaTeX:

In[361]:= StringReplace[%, "\\text{"~~name_?LetterQ~~index_?DigitQ~~"}":> name<>"_"<>index]

Out[361]= (y_1-b) \left(\frac{b-y_1}{b-y_2}\right)^{-\frac{\text{w10}}{\text{x$\gamma $1}-\text{$\omega $2}}}

You might have noticed, that this replacement just worked on the variablenames that consists of just one letter and one digit. Longer variable names will be ignored. This is because the StringPattern "_" stands just for ohne character, for a sequence of characters, use "__", but we have to make shure, that we match with the Shortest possible sequence. To catch the longer variable names we apply another string replacement:

In[362]:= StringReplace[%,
"\\text{"~~Shortest[name__]~~Shortest[index__?DigitQ]~~"}":> "\\text{"<>name<>"}_{"<>index<>"}"]
Out[362]= (y_1-b) \left(\frac{b-y_1}{b-y_2}\right)^{-\frac{\text{w}_{10}}{\text{x$\gamma $}_{1}-\text{$\omega $}_{2}}}

Now all variables appear to be in the correct LaTeX-notation for subscript variables. But some of the "\text{}"s and "{}"s are obsolet now, due to single letters or digits, inside. To optimize the LaTeX code, we can add further repacements:

In[371]:= StringReplace[%, "{" ~~ i_?DigitQ ~~ "}" :> i];
StringReplace[%, "\\text{" ~~ name_?LetterQ ~~ "}" :> name]

Out[372]= (y_1-b) \left(\frac{b-y_1}{b-y_2}\right)^{-\frac{w_{10}}{\text{x$\gamma $}_1-\text{$\omega $}_2}}

Now i think the TeX looks good enough, so we can define a function that does all the replacements in one step:

In[506]:= 
ClearAll[myTeXForm]
SetAttributes[myTeXForm, HoldFirst]

 myTeXForm[expr_] := Fold[StringReplace, ToString[HoldPattern[expr], TeXForm],
{"\\text{HoldPattern}\\left[" ~~ str__ ~~ "\\right]" ~~ EndOfString :> str, 
    "\\text{" ~~ Shortest[str__] ~~ Shortest[i__?DigitQ] ~~ "}" :> 
    "\\text{" <> str <> "}_{" <> i <> "}", 
    {"{" ~~ i_?DigitQ ~~ "}" :> i, "\\text{" ~~ s_?LetterQ ~~ "}" :> s}}]

Testing the function:

b=134;
myTeXForm[(-b+y1) ((b-y1)/(b-y2))^(-(w10/(x\[Gamma]13-\[Omega]2)))]
Out[510]= (y_1-b) \left(\frac{b-y_1}{b-y_2}\right)^{-\frac{w_{10}}{\text{x$\gamma $}_{13}-\text{$\omega $}_2}}

Note that i used a little trick to protect the function agains its argument values. In this example the variable b has allready the value 134, but in the TeX Output it should still apear as "b". To do so i added the Attribut HoldFirst to our function and used HoldPattern inside. Maybe one can do this easier, but it works fine.

Hope this might inspire you. Best regards.

Upvotes: 1

Related Questions