56
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ Just this would work: TraditionalForm[HoldForm[(v/w)] Sin[w t]] $\endgroup$ Dec 7, 2012 at 2:53
  • $\begingroup$ possible duplicate of Create complicated text formulas in graphics $\endgroup$
    – Jens
    Dec 7, 2012 at 3:19
  • $\begingroup$ Your variables have values? $\endgroup$ Dec 7, 2012 at 3:28
  • $\begingroup$ The box language is not very unlike LaTeX. Why indeed not use that? $\endgroup$ Dec 7, 2012 at 9:19

3 Answers 3

46
$\begingroup$

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.1103. 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$.

$\endgroup$
54
+50
$\begingroup$

MaTeX is a package made exactly for this purpose. It can automatically compile small LaTeX snippets and import them as Mathematica graphics.

It is described in detail in this other Mathematica.SE answer.

A short tutorial is available here.

Finally, to show what it can do, this is an example plot made with MaTeX labels (borrowed from the tutorial above, which has full code for it):

enter image description here

$\endgroup$
1
  • 14
    $\begingroup$ I wanted to post this answer here because this question comes up first when googling for LaTeX labels in Mathematica. $\endgroup$
    – Szabolcs
    Nov 1, 2015 at 23:51
2
$\begingroup$

See my suggestions here

Basically you can create your own PlotThemes in MMA 9+ allowing you to always use the same font sizes and especially font stlyes. Thus, using a Theme with your document font size and the latex fonts (e.g. "CMU Serif") and a fixed ImageSize creates eps-plots of decent quality. If you need to change something, you just change the custom PlotStyle and let MMA redraw the plots.

$\endgroup$
2
  • $\begingroup$ Have you tried MaTeX? $\endgroup$
    – rm -rf
    Mar 10, 2016 at 14:32
  • $\begingroup$ I have, but if I am not mistaken, the difference is in the LaTex Typesetting and that can be accomplished over TeXForm or similar features, if I am not mistaken. Further, an equation on a frame label can just be written as one would write it in Mathematica. It will look so similar to "proper" latex typesetting, that it should be good even for a thesis project. In MaTeX I would need to define every TickLabel for every single plot... I never got the alternative MathPSfrag running, though $\endgroup$
    – chris1992
    Mar 10, 2016 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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