6
$\begingroup$

I'm having difficulties in displaying a proper fraction in a label. Here's the 2D graphics which shows the problem :

PolarPlot[{
    LegendreP[5, Cos[theta]], 
    11/2 If[theta <= Pi,  1, -1]LegendreP[5, Cos[theta]]^2 Sin[theta]
    },
    {theta, 0, 2Pi},
    PlotStyle -> {
        Directive[Thickness[0.0025], Blue],
        Directive[Thickness[0.0025], Red]
        },
    PlotLabel -> Row[{
        Style[HoldForm[LegendreP[5, " cos \[Theta] "]], Blue, 16],
        "\n",
        Style[11/2 HoldForm[LegendreP[5, " cos \[Theta] "]^2 "sin \[Theta]"], Red, 16]
        }]
]

The label contains the fraction 11/2 which is badly formatted. I would like to have the fraction displayed as a small fraction in front of P_5(cos(theta))^2 sin(theta), without the big parenthesis.

Is that possible?

I am looking for a solution that works in Mathematica 7.

$\endgroup$
3
  • 1
    $\begingroup$ Wrap 11/2 in a Style. e.g. Style[Style[11/2, 10] HoldForm[ LegendreP[5, " cos \[Theta] "]^2 "sin \[Theta]"], Red, 16] $\endgroup$
    – Edmund
    Commented Mar 15, 2016 at 14:44
  • $\begingroup$ @Edmund, your suggestion works, but MMA still adds big parenthesis around the LegendreP. How to prevent that ? $\endgroup$
    – Cham
    Commented Mar 15, 2016 at 14:55
  • $\begingroup$ This issue bothered me too. There is no easy and satisfactory answer yet. For now I prefer to use the niceFrac function by Jens: mathematica.stackexchange.com/questions/15204/… $\endgroup$
    – yanfyon
    Commented May 11, 2016 at 20:03

3 Answers 3

6
$\begingroup$

Based on @Mike's answer here, add ScriptLevel -> 1 to Style.

Mathematica graphics

Mathematica uses the ScriptLevel value to determine when it should decrease the font size in equations. The script level increases with each nested fraction/subscript/superscript. I think that Mathematica switches to smaller fonts at a ScriptLevel of 1. In inline formulas (the ones you type with Ctrl-9 in text cells) the starting ScriptLevel is 1, thus fractions become small immediately. Otherwise it is usually 0, so the outermost fractions are large (but things written within them become small).


However, the ScriptLevel is already 1 in Column by default, as opposed to Row. Don't use Row[{..., "\n", ...}] if you mean a Column. You may want to specify alignment using the second argument of Column.

PolarPlot[{LegendreP[5, Cos[theta]], 
  11/2 If[theta <= Pi, 1, -1] LegendreP[5, Cos[theta]]^2 Sin[
    theta]}, {theta, 0, 2 Pi}, 
 PlotStyle -> {Directive[Thickness[0.0025], Blue], 
   Directive[Thickness[0.0025], Red]}, 
 PlotLabel -> 
  Column[{Style[
     "\!\(\*FormBox[\(\*SubscriptBox[\(P\), \(5\)](cos\\\ \
\[Theta])\),
TraditionalForm]\)", Blue, 16], 
    Style["\!\(\*FormBox[\(\*FractionBox[\(11\), \(2\)] \
\*SuperscriptBox[\(\*SubscriptBox[\(P\), \(5\)](cos\\\ \[Theta])\), \
\(2\)] sin\\\ \[Theta]\),
TraditionalForm]\)", Red, 16]}]]

Mathematica graphics

All this ugly SuperscriptBox stuff that you see above will format nicely in the front end if you paste it back:

Mathematica graphics

I typed it like this:

  • type "x"
  • select the x and press Control-Shift-T to convert to TraditionalForm
  • position the cursor next to the x, then type the formula as you normally would

Personally I prefer to type formulae directly into a single string rather than try to mix strings with HoldForm in a complicated way.

$\endgroup$
4
$\begingroup$

Update: According to the OP's comment, he is looking for a solution that works in Mathematica 7. MaTeX requires Mathematica 10. I am leaving this answer here in case others find it useful, but note that it does not solve the OP's problem.


If you're fussy about formula formatting, you'll be better off using MaTeX.

MaTeX["\\frac{11}{2} P_5(\\cos \\theta)^2 \\sin\\theta", 
 "DisplayStyle" -> False]

Mathematica graphics

I made MaTeX precisely because people I worked with were fussy about formatting.


Load package:

<<MaTeX`

Add the color package to the preamble and change to CM Bright sans-serif fonts:

SetOptions[MaTeX, 
 "Preamble" -> {"\\usepackage{cmbright}", 
   "\\usepackage{amsmath,amssymb}", "\\usepackage{color}"}]

PolarPlot[{LegendreP[5, Cos[theta]], 
  11/2 If[theta <= Pi, 1, -1] LegendreP[5, Cos[theta]]^2 Sin[
    theta]}, {theta, 0, 2 Pi}, 
 PlotStyle -> {Directive[Thickness[0.0025], Blue], 
   Directive[Thickness[0.0025], Red]}, 
 PlotLabel -> 
  MaTeX["{\\color{blue}P_5(\\cos\\theta)},\;\; \
{\\color{red}\\frac{11}{2} P_5(\\cos \\theta)^2 \\sin\\theta}", 
   "DisplayStyle" -> False, FontSize -> 16]]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ I don't know MaTeX (that's in Mathematica ?). The 11/2 fraction displayed above is what I want in MMA labels. However, your code doesn't appear to work in my old version 7. $\endgroup$
    – Cham
    Commented Mar 15, 2016 at 14:52
  • $\begingroup$ @Cham You have to click the link to see what it is, but it does not work with Mathematica 7. $\endgroup$
    – Szabolcs
    Commented Mar 15, 2016 at 14:54
2
$\begingroup$

I wasn't totally happy with this. Even in the LaTeX code by Szabolcs, the fraction is larger than the P right next to it. But this gives close to the same thing as his, but without an external package

PolarPlot[{
      LegendreP[5, Cos[theta]], 
      11/2 If[theta <= Pi,  1, -1] LegendreP[5, Cos[theta]]^2 Sin[
    theta]
      },
     {theta, 0, 2 Pi},
     PlotStyle -> {
           Directive[Thickness[0.0025], Blue],
           Directive[Thickness[0.0025], Red]
           },
     PlotLabel -> Row[{

    Style[HoldForm[LegendreP[5, " cos \[Theta] "]], Blue, 16],
            "\n",

    Row[Style[#1, Red, #2] & @@@ {{11/2, 
        8} , {HoldForm[
         LegendreP[5, " cos \[Theta] "]^2 "sin \[Theta]"], 16}}]
            }]
 ]

enter image description here

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