What is the problem with this expression inside ListPrint? OverTilde
generates here a mess:
FrameLabel->{{"f"<>"("<>ToString[OverTilde["x"]]<>")",None},{"x",None}}
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityWhat is the problem with this expression inside ListPrint? OverTilde
generates here a mess:
FrameLabel->{{"f"<>"("<>ToString[OverTilde["x"]]<>")",None},{"x",None}}
OverTilde["x"]
is a short way of writing Overscript["x","~"]
so it is not OverTilde
per se causing the issue here. The problem is that by default ToString
writes OutputForm
. So what you need to do is to write the string as StandardForm
:
FrameLabel->{{"f"<>"("<>ToString[OverTilde["x"],StandardForm]<>")",None},{"x",None}}
For example this doesn't work:
Plot[Sin[x], {x, 0, 10},
Frame -> True,
FrameLabel -> {{"f" <> "(" <> ToString[OverTilde["x"]] <> ")",
None}, {"x", None}},
BaseStyle -> {FontSize -> 20, FontFamily -> "TimesNewRoman"}]
But this works fine:
Plot[Sin[x], {x, 0, 10},
Frame -> True,
FrameLabel -> {{"f" <> "(" <>
ToString[OverTilde["x"], StandardForm] <> ")", None}, {"x",
None}},
BaseStyle -> {FontSize -> 20, FontFamily -> "TimesNewRoman"}]
This is get what JohnFultz suggested in a comment on record.
A simple and easy-to-understand approach using Row
is demonstrated by
Plot[Sin[x], {x, 0, 2 Pi},
Frame -> True,
FrameLabel -> {x, Row[{"f(", OverTilde["x"], ")"}]},
BaseStyle -> {FontSize -> 16}]
Row
is an optimal approach to this problem. I interpret the question as one which requires an explanation of unexpected ToString
behaviour ("What is the problem with this expression...").
$\endgroup$
Sep 28, 2013 at 12:28
Frame labels are automatically converted to TraditionalForm
Plot[Sin[x], {x, 0, 10},
Frame -> True, FrameLabel -> {x, f[\!\(\*OverscriptBox[\(x\), \(~\)]\)]},
BaseStyle -> {FontSize -> 20, FontFamily -> "TimesNewRoman"}]
In the notebook it looks much better:
I specify BaseStyle
to enlarge the labels. You can enter x̃
as x
Ctrl+7~
as Szabolcs write in the comment. More correct to use HoldForm[x]
and HoldForm[f[x̃]]
to localize f
and x
.
My solution differs from Szabolcs's comment:
Plot[Sin[x], {x, 0, 10}, Frame -> True,
FrameLabel -> {"x", "f(\!\(\*OverscriptBox[\(x\), \(~\)]\))"},
BaseStyle -> {FontSize -> 20, FontFamily -> "TimesNewRoman"}]
It is because TraditionalForm
automatically italicizes variables and makes other necessary transformations, e.g. Sin[x]
→ $\sin(x)$, Hypergeometric2F1[z, b, c, z]
→ ${}_2F_1(a,b,c;z)$.
Why "f("<>ToString[OverTilde["x"]]<>")"
works strange?
Answer:
ToString[OverTilde["x"]]
~ x
It is two-line string with ~
over x
. Therefore
"f(" <> ToString[OverTilde["x"]] <> ")"
f(~ x)
"f("
is prepended to first line and ")"
is appended to the second line. More correct usage:
ToString[f[OverTilde["x"]]]
~ f[x]
FrameLabel -> {"x", "f(\!\(\*OverscriptBox[\(x\), \(~\)]\))"}
. I entered this asf(x
Ctrl-7~
)
. It's readable and easy to format in the notebook, but it becomes a bit ugly when pasting here. $\endgroup$ToString
. Both answers are correct and sufficient, but surely a solution involvingRow
is easier to understand and apply to other similar problems. $\endgroup$ToString
is not by default writingStandardForm
. I agree though thatRow
ought to be the preferred way of doing this. $\endgroup$