Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need to typeset large number of equations to be used for PlotLabel and Titles and such. These have to look nice. Hence I use TraditionalForm with HoldForm and this works well, except for a tiny little annoying thing. This below is just an example:

TraditionalForm[HoldForm[(v/w) Sin[w t]]]

displays as $\frac{v \ \sin(t)}{w}$ and not as $\frac{v}{w} \sin(t)$. In otherwords, the Sin[x] was put in the numerator as well, where I'd like it to remain on its own as was typed.

I used () above as a hint to leave Sin[] alone, but this hint did nothing.

An easy fix is to use HoldForm around each small sub-expression as follows

TraditionalForm[Row[{HoldForm[(v/w)], HoldForm[Sin[w t]]}]] 

which now generates $\frac{v}{w} \sin(t)$

But the above is a small example. I have much larger equations, and would be nice if I could tell Mathematica when I type it, using () or another trick, to please not group these subexpression as it did in the above example (I'd like the equations to look exactly as they appear in the text book.

It is not a big issue really to use HoldForm many times to force the shape to look exactly as I wanted, but just thought to check if someone knows an easier way to do this other than what I have shown.

I just had a thought! is it possible to use $\LaTeX$ directly to typeset things in Mathematica for purpose of display? i.e. it would be nice if one can write something like Text["$\frac{v}{w} \sin(t)$"] and have Mathematica interpret this as $\LaTeX$, and typeset it as $\LaTeX$ output. If possible, I can just write everything in $\LaTeX$ as strings, and problems solved. What I mean by the above is as follows (only to help explain the point):

In MATLAB I can do this:

text(.5,.5,'$\frac{v}{w} \sin(t)$','interpreter','latex','FontSize',18)

which generates

LaTeX'd formula

Notice I had to tell MATLAB to use the $\LaTeX$ interpreter. But that is all. It will be really nice to be able to do something like this in Mathematica if possible. Using Strings also avoids many other problems when typesetting symbolic expressions in Mathematica since a string is inert and a raw Symbol and can't have value other than itself.

Thanks.

share|improve this question
1  
Just this would work: TraditionalForm[HoldForm[(v/w)] Sin[w t]] –  Vitaliy Kaurov Dec 7 '12 at 2:53
    
possible duplicate of Create complicated text formulas in graphics –  Jens Dec 7 '12 at 3:19
    
Your variables have values? –  Vitaliy Kaurov Dec 7 '12 at 3:28
    
The box language is not very unlike LaTeX. Why indeed not use that? –  Sjoerd C. de Vries Dec 7 '12 at 9:19

1 Answer 1

up vote 15 down vote accepted

I'll treat this question as being mainly about annotating graphics using LaTeX syntax. For that purpose, here is a template that you could use:

Plot[Sin[x], {x, 0, 2 Pi}, 
 Epilog -> 
  Text[Style[
    ToExpression["\\sin\\alpha", TeXForm, HoldForm], Large], {Pi, .5}]]

sine

However, there are several caveats because the ability of Mathematica to interpret $\LaTeX$ is limited. I'm copying this in slightly edited form from a list of issues that I had lying around on my web site:

  • Note that all backslashes should be doubled because a single \ counts as an escape character. In the above code, the argument HoldForm (or alternatively Hold if you want to do calculations later) prevents evaluation so that it is possible to display everything without re-arranging the order.
  • The $\LaTeX$ conversion will throw an error if Mathematica thinks the expression has incorrect syntax. So you won't always be able to get literal translations of arbitrary LaTeX fragments.
  • Spaces are completely ignored by Mathematica, even in products of variables such as $z = x y$. The Mathematica expression will become z = xy which incorrectly references a single variable with a two-letter name xy. You have to explicitly insert thin spaces ("\\,") in the string argument of ToExpression wherever you want an implicit multiplication to appear. Note that $\LaTeX$ ignores spaces too, but it defaults to defining each letter as a separate variable in the above expression. Mathematica simply uses the wrong default assumption here.
  • Spaces between digits as in 3.1 103 are also ignored, so this example would be translated as (3.11)3. As above, you have to force Mathematica to recognize spaces, or use a multiplication operator such as \times in your $\LaTeX$ code.
  • To translate integrals properly, Mathematica expects the integration variable in the $\LaTeX$ code to be preceded by \\, d. Only with the additional space will it recognize the d as the differential part of the integral.
  • Express derivatives using the \partial symbol: ToExpression["\\frac{\\partial^2 f(x)}{\\partial x^2}", TeXForm, HoldForm]
  • All matrices and vectors must be written in list form using escaped curly brackets, as in \{x,y,f (z)\} because the conversion of array environments or commands such as \pmatrix to Mathematica produces wrong formatting.
  • To group expressions, use only round parentheses, i.e., \left( and \right), not square [...] or curly {...} brackets. The latter are interpreted differently by Mathematica.
  • Do not use Mathematica's built-in symbols as names for variables in your $\LaTeX$ code. They can lead to syntax errors or will be replaced by different-looking output forms. An example is ToExpression["E = mc^2", TeXForm, HoldForm] where the output contains $\mathbb{e}$ instead of $E$.

Finally, when Mathematica fails to translate from LaTeX, create a snippet of the MMA code that you think should be generated. Put this into TeXForm. From the result, you can often guess the required LaTeX form that will work in reverse as an input to ToExpression.

If this is close to what you wanted, maybe you can edit the title of the question to include $\LaTeX$.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.