1
$\begingroup$

I want to use Text to add some labels to a graph and was wondering if it is possible to use expressions inside Text? Here is a MWE of what I am trying.

field = 1.45;
Plot[x, {x, 1, 3}, Epilog -> {Style[Text["H = " field " T", {1.5, 2.5}], 12]}]

The problem is that this adds 1.45H=T to the plot and I want it to add H=1.45T. I have tried converting field to a string first and using that instead, but the output is the same.

$\endgroup$
3
$\begingroup$
field = 1.45;

Plot[x, {x, 1, 3},
 Epilog -> {Style[Text["H = " <> ToString@field <> " T", {1.5, 2.5}], 12]}]

enter image description here

$\endgroup$
  • $\begingroup$ My pleasure, thanks for acceptance :) $\endgroup$ – eldo Oct 15 '14 at 16:17
  • $\begingroup$ Or if you want TraditionalForm: Epilog -> {Style[Text[ToString@TraditionalForm[H == field*T], {1.5, 2.5}], 12]} $\endgroup$ – Bob Hanlon Oct 15 '14 at 16:53
  • $\begingroup$ @kguler Thanks for letting me know, I have upvoted the answer. $\endgroup$ – Michael Oct 15 '14 at 18:35
  • $\begingroup$ Thanks @Bob Hanlon for an alternative solution. Seeing multiple solutions is a nice way to learn something new about Mathematica $\endgroup$ – Michael Oct 15 '14 at 18:36
2
$\begingroup$

You could also use

field = 1.45;
Plot[x, {x, 1, 3}, Epilog -> Inset[Style[Row[{"H = ", field, " T"}], 12], {1.5, 2.5}]]

enter image description here

$\endgroup$
  • $\begingroup$ +1 because the Row-solution is superior in more complicated cases (Superscript etc.). $\endgroup$ – eldo Oct 15 '14 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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