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?
- 41,180
- 3
- 34
- 96
- 1,389
- 9
- 15
4 Answers
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:

- 37,541
- 3
- 85
- 158
StringInsert["David", "\!\(\*SubscriptBox[\(a\), \(b\)]\)", 3]
(* $\text{Da}a_b\text{vid}$ *)
or
"David" <> "\!\(\*SubscriptBox[\(c\), \(d\)]\)" <> "cat"
(* $\text{David}c_d\text{cat}$ *)
- 41,180
- 3
- 34
- 96
-
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"
- 107,779
- 16
- 103
- 257
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
- 38,913
- 1
- 43
- 110