3
$\begingroup$

What is the problem with this expression inside ListPrint? OverTilde generates here a mess:

FrameLabel->{{"f"<>"("<>ToString[OverTilde["x"]]<>")",None},{"x",None}}
$\endgroup$
4
  • $\begingroup$ I think you are making this much more complicated than necessary. I would simply use FrameLabel -> {"x", "f(\!\(\*OverscriptBox[\(x\), \(~\)]\))"}. I entered this as f(x Ctrl-7 ~ ). It's readable and easy to format in the notebook, but it becomes a bit ugly when pasting here. $\endgroup$
    – Szabolcs
    Sep 27, 2013 at 14:02
  • 1
    $\begingroup$ @Szabolcs and et al., I find it bizarre that, with two answers up as I write this comment, one leapt straight to linear syntax/boxes and the other to fixing up ToString. Both answers are correct and sufficient, but surely a solution involving Row is easier to understand and apply to other similar problems. $\endgroup$
    – John Fultz
    Sep 27, 2013 at 22:53
  • $\begingroup$ @JohnFultz My suggestion wasn't to type the text representation of boxes, as it was pasted here. I meant typing the equivalent 2D math expression using keyboard shortcuts. If starting with Ctrl-9 to create an inline cell, then single-character variables will be properly capitalized too. I find this wysiwyg way of entering the formula quite convenient, and it's what I use myself for plot labels. $\endgroup$
    – Szabolcs
    Sep 28, 2013 at 1:46
  • $\begingroup$ @JohnFultz I guess it depends on whether in responding you answer the specific problem or suggest alternatives that maybe be better. IMO the specific problem is that ToString is not by default writing StandardForm. I agree though that Row ought to be the preferred way of doing this. $\endgroup$ Sep 28, 2013 at 1:55

3 Answers 3

5
$\begingroup$

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"}]

enter image description here

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"}]

enter image description here

$\endgroup$
3
$\begingroup$

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}] 

plot.png

$\endgroup$
3
  • $\begingroup$ I guess the point I was making in my comment to john fultz was that the question asked "What is the problem with this expression..." not "what is the best way to ...". Do we answer the actual questions here or do we re-frame questions to ones which we would prefer to answer? $\endgroup$ Sep 28, 2013 at 12:18
  • $\begingroup$ @MikeHoneychurch. Your point is well-taken. I think we should answer the actual question and also reframe and answer them to show better practice. $\endgroup$
    – m_goldberg
    Sep 28, 2013 at 12:23
  • $\begingroup$ I haven't searched but I would guess there must be many answers here that show that 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
2
$\begingroup$

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:

enter image description here

I specify BaseStyle to enlarge the labels. You can enter 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"}]

enter image description here

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]
$\endgroup$

Your Answer

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

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