6
$\begingroup$

If I write this one

str=Subscript["x","y"];
StringQ[str]

False

How can I get str as string?

$\endgroup$
  • 3
    $\begingroup$ StringQ asked whether str was a string. Mathematica correctly answered, "False". Try str//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
  • $\begingroup$ @DavidCarraher, I have tried ToString[str] . But that gives undesirable result. $\endgroup$ – user22180 Oct 20 '14 at 12:50
10
$\begingroup$
ToString[Subscript["x", "y"], FormatType -> StandardForm]

enter image description here

StringQ[%]

True

$\endgroup$
  • $\begingroup$ Thanks for the help. That is exactly what I was looking for. $\endgroup$ – user22180 Oct 20 '14 at 15:45
  • $\begingroup$ Equivalently and shorter: ToString[Subscript["x", "y"], StandardForm] $\endgroup$ – Mr.Wizard Aug 14 '15 at 9:26
3
$\begingroup$

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:

enter image description here

StringQ@TemplateApply[st, {x, y, z}] yields True

and to illustrate StringJoin and Style:

Style[TemplateApply[st, {x, y, z}] <> ". QED", Red, 
 FontFamily -> "Kartika"]

enter image description here

$\endgroup$
2
$\begingroup$
f = "\!\(\*TagBox[SubscriptBox[\"y\", \"x\"],\n \
\"MathMLPresentationTag\",\nAutoDelete->True]\)";

or

f="\!\(x\_y\)"

enter image description here

StringQ[f]

(*True*)
$\endgroup$
1
$\begingroup$
super /: MakeBoxes[super[x_, y_], StandardForm] :=
   RowBox[{SuperscriptBox[ToString @ x, ToString @ y]}]

str = ToString[super[a, b], StandardForm]

enter image description here

str // Head

String

str // FullForm

enter image description here

$\endgroup$
0
$\begingroup$

Subscript["x","y"] is not a string but you can convert it into one with ToString:

ToString[Subscript["x", "y"]]

Mathematica graphics

Now StringQ@ToString[Subscript["x", "y"]] is True

$\endgroup$
  • $\begingroup$ But have you seen the result? that is not desired. $\endgroup$ – user22180 Oct 20 '14 at 12:48
  • $\begingroup$ well... if you care about the string I prefer that rather than \!("x"_"y") $\endgroup$ – Gustavo Delfino Oct 21 '14 at 7:16
  • $\begingroup$ ok, see ybeltukov's answer. That is what I wanted. You were almost there, just what you need to add FormatType -> StandardForm as an option. $\endgroup$ – user22180 Oct 21 '14 at 13:46

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