6
$\begingroup$

I am trying to use Graphics[Text[]] to label some curves on a plot. However, I cannot format the text properly.

I want my text to look like this

$$F= \left(\dfrac{2\tilde\omega}{\sqrt{3}}\right)^\frac{3}{2}$$

but it looks like this

$$F= (\dfrac{2\tilde\omega}{\sqrt{3}})^\frac{3}{2}$$

Here is my code.

Graphics[{Text[
   Style["F=(\!\(\*FractionBox[\(2\\\ \*OverscriptBox[\(\[Omega]\), \
\(\[Tilde]\)]\), SqrtBox[\(3\)]]\)\!\(\*SuperscriptBox[\()\), \
FractionBox[\(3\), \(2\)]]\)", FontSize -> 15, 
    FontFamily -> "CMU Serif", Purple], {2.4, 0.755}]}]

I'm adding a screenshot here because I don't know how to format Mathematica code on this website.

enter image description here

This is the output of my code.

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ @Nasser's answer is excellent. There is a sort of dirty hack that you can use: inline cells. If you're in a "context" that is not the standard input (e.g. a Text cell or just within a string), you can do a Control-9 to get a little inline cell where the cursor was. You an do pretty much everything to this little cell as you can with a regular cell. So, you can start with Text[""], put your cursor between the quotes, do Control-9, and create the expression you wanted (using the Palettes is a nice way to do this). Once it looks as you want, you can insert it into your Graphics. Kind of a hack. $\endgroup$
    – lericr
    Feb 26, 2022 at 18:47
  • $\begingroup$ @lericr Side note: seems that the Ctrl+9 short cut won't work in a Wolfram lanuage input cell if the format of cell is InputForm at least in windows, v12.3.1. (One can switch to InputForm by pressing Ctrl+Shift+I, and switch back to the default StandardForm by pressing Ctrl+Shift+N. ) $\endgroup$
    – xzczd
    Feb 27, 2022 at 6:30
  • 1
    $\begingroup$ All you need to do is enter the text in the string as TraditionalForm as I did in this answer $\endgroup$
    – Jens
    Feb 27, 2022 at 23:11
  • $\begingroup$ @Jens Oh, Why I forgot about Ctrl+Shift+T… Post this as an answer? This is the best solution so far. $\endgroup$
    – xzczd
    Feb 28, 2022 at 2:14
  • $\begingroup$ @xzczd OK - I added an answer... $\endgroup$
    – Jens
    Feb 28, 2022 at 3:50

4 Answers 4

10
$\begingroup$

May be

text = HoldForm[F = ((2*OverTilde[\[Omega]])/Sqrt[3])^(3/2)]; 
Graphics[{Text[
   Style[text, FontSize -> 15, FontFamily -> "CMU Serif", 
    Purple], {2.4, 0.755}]}]

Mathematica graphics


Another option is to use MaTeX directly to make these labels. Like this

<< MaTeX`

text = MaTeX[
   "F=\\left( \\frac{2 \\tilde{\\omega}}{\\sqrt 3} \
\\right)^{\\frac{3}{2}}", Magnification -> 2];
Graphics[{Text[text, {2.4, 0.755}]}]

Mathematica graphics

Color can be added also if needed. Text above is used just to put the MaTeX output at correct location since the output is not really text in this case but an image rendering.

$\endgroup$
2
  • 4
    $\begingroup$ I'll second that recommendation for using MaTeX when elegant typography is essential. (My hope is that Wolfram Research buys MaTeX from Szabolcs Horvát, as he deserves compensation for his great work, and that $\LaTeX$-style typography gets fully integrated with the Mathematica kernel. But who knows?) $\endgroup$ Feb 26, 2022 at 18:14
  • $\begingroup$ Mathematica has native $\LaTeX$ capabilities, which are not complete, but I have built a lightweight wrapper around it that works in most simple cases. It has the advantage that it is formatted text and not an image. I have only tested it on version 11.3. If someone is willing to test it on later versions, I can post it here. $\endgroup$
    – robjohn
    Feb 27, 2022 at 15:07
11
$\begingroup$

Use the SpanMaxSize option on each parenthesis.

Graphics[{Circle[], Text[DisplayForm[FormBox[
   Style[RowBox[{"F", "=", 
       SuperscriptBox[
         RowBox[{StyleBox["(", SpanMaxSize -> Infinity], 
            FractionBox[
              RowBox[{"2", " ", "\[Omega]"}], 
              SqrtBox["3"]], 
       StyleBox[")", SpanMaxSize -> Infinity]}], 
         RowBox[{"2", "/", "3"}]]}], FontSize -> 15, 
  FontFamily -> "CMU Serif", Purple], TraditionalForm]]]}]

enter image description here A more efficient method would be to change the notebook options so that you don't have to use the code every time.

$\endgroup$
7
$\begingroup$

You can also use the new-in-12.2 inline $\LaTeX$ cell. (The keyboard shortcut is Ctrl+4.) First generate the $\LaTeX$ code with TeXForm:

TeXForm@Unevaluated[F = ((2 OverTilde@ω)/Sqrt@3)^(3/2)]
(* F=\left(\frac{2 \tilde{\omega }}{\sqrt{3}}\right)^{3/2} *)

Alternatively, select the code and right click and select Copy AsLaTeX, or simply type it manually. Then

enter image description here

$\endgroup$
4
$\begingroup$

I answered a related question here, and the method really applies almost anywhere you might want to enter nicely formatted maths in a notebook: In the string, type a single dummy letter, then highlight that letter and press Shift-Command-T to turn that letter into TraditionalForm (it's Command on Mac and Ctrl on Windows; you can also go to the menu Cell > Convert To > TraditionalForm). Then go back and change the dummy letter into the math expression you want. Parentheses will extend with their content as expected. You can see how I do this in the screen capture at the end of my linked post.

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