# How do I write subscripts in the middle of a string? [duplicate]

As stated in the tile, I want to have a subscript in the middle of a sting, but StringJoin does not allow "Test"<>Subscript["test", "test"]<>"test". Any suggestions?

When you convert the subscripts to strings be sure to choose StandardForm:

"Test  " <> ToString[Subscript["x", "21"], StandardForm] <> "  test"


Here is it again saved as a PDF:

StringInsert["David", "\!$$\*SubscriptBox[\(a$$, $$b$$]\)", 3]


(* $\text{Da}a_b\text{vid}$ *)

or

"David" <> "\!$$\*SubscriptBox[\(c$$, $$d$$]\)" <> "cat"


(* $\text{David}c_d\text{cat}$ *)

• This doesn't produce a string containing subscript glyphs. It won't look like subscripted text anywhere outside of Mathematica – m_goldberg Mar 13 '15 at 17:01

Subscripting is a formatting operation for displayed output only. You can't put Mathematica subscripting into a string object which can only contain unicode characters. You can put unicode subscript characters into string.

"test\:2084\:2082"


building on David's answer, supposing we have a Subscript object:

  s = Subscript[a, b];


you can roll that into a string like this:

 sout = "the subscript form of a is: " <> "\!$$" <> ToString[ToBoxes[s], InputForm] <> "$$"


note the string actually contains all that escape code:

 sout // FullForm


"the subscript form of a is: !($$a\_b$$)"

hence the result is only useful for display within mathematica.

the docs on this were a little hard to find: http://reference.wolfram.com/language/tutorial/StringRepresentationOfBoxes.html