4
$\begingroup$

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?

$\endgroup$
7
$\begingroup$

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

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

enter image description here

Here is it again saved as a PDF:

enter image description here

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

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

or

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

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

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

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.

enter image description here

"test\:2084\:2082"
$\endgroup$
1
$\begingroup$

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] <> "\)"

enter image description here

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

$\endgroup$

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