I've tried ToString[]
, but it doesn't work properly with subscripts:
How to convert expr
containing subscript to a string with exactly the same FullForm
that "expr"
has?
UPDATE by xzczd
There actually exists a more general issue behind the specific problem above, that is, how to convert an expression containing 2D format into corresponding string that retains the format programmatically? For example, how to convert
{a^b + 1, 1.23*10^2, 2 Subscript[a, b]}
(* Please Press Ctrl+Shift+N to convert it to standard form *)
to
{"\!\(\*SuperscriptBox[\(a\), \(b\)]\)+1", "1.23\[Times]\!\(\*SuperscriptBox[\(10\), \(2\
\)]\)", "2\!\(\*SubscriptBox[\(a\), \(b\)]\)"}
The attempt is easy to achieve by hand: we just need to place quotes around the expression and place a \[Times]
between 1.23
and 10^2
, but how to do this with program? ToString
won't work, as shown above.
Subscript[t, 1]
copied & pasted into quotes. $\endgroup$"\!\(t\_1\)"
only with version 5.2, but both with versions 8.0.4 and 11.0.0 I get"\!\(\*SubscriptBox[\(t\), \(1\)]\)"
(Win7 x64). $\endgroup$ToString
always creates the same string from identical expressions but there is no simple way to convert"\!\(t\_1\)"
to"\!\(\*SubscriptBox[\(t\), \(1\)]\)"
or backward. Moreover, there are many ways to display visually the same using different code, for exampleToString["\!\(t\_1\)", StandardForm] // FullForm
returns entirely different string which is rendered identical to"\!\(t\_1\)"
. $\endgroup$