If I write this one
str=Subscript["x","y"];
StringQ[str]
False
How can I get str
as string?
If I write this one
str=Subscript["x","y"];
StringQ[str]
False
How can I get str
as string?
ToString[Subscript["x", "y"], FormatType -> StandardForm]
StringQ[%]
True
ToString[Subscript["x", "y"], StandardForm]
$\endgroup$
– Mr.Wizard♦
Aug 14 '15 at 9:26
In Mathematica version 10, StringTemplate
can allow you to convert to string, e.g.
st = StringTemplate[
"Pythagoras theorem: \!\(\*SuperscriptBox[\(`1`\), \
\(2\)]\)+\!\(\*SuperscriptBox[\(`2`\), \(2\)]\)= \
\!\(\*SuperscriptBox[\(`3`\), \(2\)]\)"];
You can apply the template and yield a string,, allowing you to style, join etc.
TemplateApply[st, {x, y, z}]
yields:
StringQ@TemplateApply[st, {x, y, z}]
yields True
and to illustrate StringJoin
and Style
:
Style[TemplateApply[st, {x, y, z}] <> ". QED", Red,
FontFamily -> "Kartika"]
f = "\!\(\*TagBox[SubscriptBox[\"y\", \"x\"],\n \
\"MathMLPresentationTag\",\nAutoDelete->True]\)";
or
f="\!\(x\_y\)"
StringQ[f]
(*True*)
super /: MakeBoxes[super[x_, y_], StandardForm] :=
RowBox[{SuperscriptBox[ToString @ x, ToString @ y]}]
str = ToString[super[a, b], StandardForm]
str // Head
String
str // FullForm
Subscript["x","y"]
is not a string but you can convert it into one with ToString
:
ToString[Subscript["x", "y"]]
Now StringQ@ToString[Subscript["x", "y"]]
is True
StringQ
asked whether str was a string. Mathematica correctly answered, "False". Trystr//FullForm
or str//FreeForm to confirm the structure of str. Then try ToString[str] and see what you get. $\endgroup$ – DavidC Oct 20 '14 at 12:45