7

If I write this one

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

False

How can I get str as string?

Öskå
  • 8,587
  • 4
  • 30
  • 49
user22180
  • 295
  • 1
  • 3
  • 10
  • 3
    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. – DavidC Oct 20 '14 at 12:45
  • @DavidCarraher, I have tried ToString[str] . But that gives undesirable result. – user22180 Oct 20 '14 at 12:50

5 Answers5

10
ToString[Subscript["x", "y"], FormatType -> StandardForm]

enter image description here

StringQ[%]

True

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
3

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

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
2
f = "\!\(\*TagBox[SubscriptBox[\"y\", \"x\"],\n \
\"MathMLPresentationTag\",\nAutoDelete->True]\)";

or

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

enter image description here

StringQ[f]

(*True*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
1
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

eldo
  • 67,911
  • 5
  • 60
  • 168
0

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

Öskå
  • 8,587
  • 4
  • 30
  • 49
Gustavo Delfino
  • 8,348
  • 1
  • 28
  • 58