20
$\begingroup$

I find that the equations I write in Mathematica are not always publication quality. Here is a typical example:

in-cell equation from a notebook

If I copy as Latex, then I get the following code:

\frac{\sum _{i=1}^N \frac{(k|k) \tilde{x}_j^i}{\sqrt{P_{\text{jj}} (k|k)}}}{N}

This code is actually kind of messed up and does not reproduce the equation correctly, but it is easy to manually edit it to create the right Latex code:

\frac{1}{N}{\sum _{i=1}^N \frac{ \tilde{x}_j^i(k|k)}{\sqrt{P_{\text{jj}} (k|k)}}}

Now, if we render this Latex code we get a much nicer looking version of the same equation:

Latex version of the same equation

The sigma looks better, variables are italicized, the vertical line (not sure what it is called) looks much better. The runt vertical line in Mathematica just looks really bad. Also, the coloring is removed, which for my purposes is better because I am including the equation in black and white text and do not want randomly colored symbols.

My question is: how can I make the equation look better in the notebook? For example, if I am using Mathematica to make a slide show or presentation, I want the equation to look as professional as possible, but still be an editable cell. What are my options?

Note that using TraditionalForm has the following consequences:

TraditionForm of Summation

Update

If we really torture it with both Defer and HoldForm, looks like we can get the typeset equation:

Typeset equation using TraditionalForm and HoldForm and Defer

$\endgroup$
5
  • 3
    $\begingroup$ If you want publication quality does this mean you want these equations for display rather than for evaluation? If so then begin by using text cells rather than input cells and start with Control - 9. Alternatively you could use TraditionalForm in your input cell Cell > Convert to > TraditionalForm $\endgroup$ Commented Nov 9, 2013 at 20:47
  • $\begingroup$ This is related to e.g. How to make traditional output for derivatives there you can find another links. $\endgroup$
    – Artes
    Commented Nov 9, 2013 at 20:48
  • 2
    $\begingroup$ re: your update. You need to be clear about what you are asking. Your update renders traditional looking output. Yet elsewhere in your question you have said "but still be an editable cell" which based on your comments below where you ask for the stuff to be "calcuable" presumes you mean evaluatable. Do you want a traditional looking rendering that you can evaluate or not? $\endgroup$ Commented Nov 9, 2013 at 23:17
  • $\begingroup$ I guess the main use case is that it is easily modifiable, not necessarily editable. For example, in a presentation you can hide the input cell so the presentable form is visible, then if you need to edit the equation you can unhide, edit the input cell, then hide it again. The main objective is to make a publishable/presentable notebook without hacks like pasting images of equations, and is calcuable (so pure text equations will not be possible in some cases, unless you hide the "real" equation and create a duplicate text equation). $\endgroup$ Commented Nov 9, 2013 at 23:23
  • 1
    $\begingroup$ Then use TraditionalForm input cells. $\endgroup$ Commented Nov 9, 2013 at 23:33

2 Answers 2

16
$\begingroup$

Typesetting in Text cells

Cell[TextData[Cell[BoxData[
 FormBox[
  RowBox[{
   FractionBox["1", "N"], 
   RowBox[{
    UnderoverscriptBox["\[Sum]", 
     RowBox[{"i", "=", "1"}], "N"], 
    FractionBox[
     RowBox[{
      SubsuperscriptBox[
       OverscriptBox["x","~"], 
       "j", "i"],
       "(", 
      RowBox[{"k", "|", "k"}], ")"}], 
     SqrtBox[
      RowBox[{
       SubscriptBox["P", "jj"], "(", 
       RowBox[{"k", "|", "k"}], ")"}]]]}]}], 
  TraditionalForm]]]], "Text",
 UnderoverscriptBoxOptions->{LimitsPositioning->False},
 OverscriptBoxOptions->{DiacriticalPositioning->True},
 FractionBoxOptions->{AllowScriptLevelChange->False}]

gives

enter image description here

This was typeset in a text cell by starting with Ctrl-9 and then typing

enter image description here

The default ScriptLevel for inline cells is 1 so I set that to 0 in a private style (Format > Edit Stylesheet):

enter image description here

The additional UnderoverscriptBox, OverscriptBox and FractionBox options were set via the option inspector. You can do additional tweeking by nudging terms around via Insert > Typesetting.

Typesetting in Input cells

Wrapping TraditionalForm around your expression, as per your updated question, is intended to render the output as traditional form. However your question seems to be framed around having traditional looking equations that -- based on your update -- can be evaluated.

If you want all you input and output to appear like this then set this globally in your preferences:

enter image description here

Then in your input cells you can type the equation and it will appear in traditional form:

enter image description here

If you only want to convert certain cells then either make the changes via the menu Cell > Convert To or via a right click:

enter image description here

which produces:

enter image description here

$\endgroup$
7
  • $\begingroup$ Ok, that seems to be a way to typeset an equation in the notebook, but in that case its not a calcuable equation is it, its just text right, or not? $\endgroup$ Commented Nov 9, 2013 at 22:54
  • $\begingroup$ Well you can't evaluate LaTeX either. If you want textbook looking stuff but you want to evaluate then use TraditionalForm $\endgroup$ Commented Nov 9, 2013 at 22:55
  • $\begingroup$ That seems like a good direction, but as shown above in the updated question when I use TraditionalForm it changes the equation. Is there a way to use TraditionalForm and maintain the same equation? Even if I use Defer, it still changes the order of terms. $\endgroup$ Commented Nov 9, 2013 at 22:57
  • $\begingroup$ You could do TraditionalForm @ HoldForm @ Sum[i,{i,1,n}] $\endgroup$ Commented Nov 9, 2013 at 23:06
  • $\begingroup$ That does indeed work if you use a combination of Defer on the 1/N, then use an outer HoldForm $\endgroup$ Commented Nov 9, 2013 at 23:09
9
$\begingroup$

You can use MaTeX to get $\LaTeX$ formatting directly:

MaTeX["\\frac{1}{N}{\\sum _{i=1}^N \\frac{\\tilde{x}_j^i(k|k)}{\\sqrt{P_{\\text{jj}} (k|k)}}}"]

Mathematica graphics

MaTeX can also be applied directly to Mathematica expressions, but then you'll get the non-ideal translation to $\LaTeX$ code you mention yourself because MaTeX uses Mathematica's builtin TeXForm for this.

$\endgroup$

Your Answer

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

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