How to join two strings one of which is Subscript? [duplicate]

If I write this one

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


False

How can I get str as string?

• 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

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


StringQ[%]


True

• Thanks for the help. That is exactly what I was looking for. – user22180 Oct 20 '14 at 15:45
• Equivalently and shorter: ToString[Subscript["x", "y"], StandardForm] – 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

• But have you seen the result? that is not desired. – user22180 Oct 20 '14 at 12:48
• well... if you care about the string I prefer that rather than \!("x"_"y") – Gustavo Delfino Oct 21 '14 at 7:16
• 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. – user22180 Oct 21 '14 at 13:46