tsaab
(T Saab)
October 19, 2015, 7:31pm
1
Hi,
I’m trying to read information from a file and use to label a line in a plot, i.e. If
stringfromfile="abcd"; (* then inside Plot ... *) Epilog->{Text[stringfromfile]}
This works fine for most case, but I don’t know how to generate superscripts in this situation, i.e. Read in a string, such as:
stringfromfile="did you know that ^4^He is an interesting isotope" (*I used the "^" here as a arbitrary marker of the superscripted character*)
and parse it such that
Text[stringfromfile]
produces: “did you know that 4 He is an interesting isotope”. I tried a bunch of approaches, but the closest I got is the following:
tmp = StringSplit[stringfromfile, "^"];
Text[(tmp[[1]] <> ""^tmp[[2]] <> tmp[[3]])];
This produces: “did you know that 4He is an interesting isotope ”
Does anyone know a way to store / interpret this formatting information, short of generating the BoxData code:
"\<\"did you know that\\!\\(\\*SuperscriptBox[\\(\\\\ \\), \\(4\\)]\\)He is an interesting isotope\"\>"
Thanks
xroy
(Xavier Roy)
October 20, 2015, 1:19am
2
Hi,
The string structure you are looking for can be obtained using boxes in the following way:
string = (RowBox[{"Did you know that", SuperscriptBox["", 4], "He is an interesting isotope?"}]
// DisplayForm)
In case the space between 4 and He appears to be too large, you can modify it either with \[NegativeThinSpace] or with \[NegativeVeryThinSpace]:
string = (RowBox[{"Did you know that", SuperscriptBox["", 4], "\[NegativeVeryThinSpace]",
"He is an interesting isotope?"}] // DisplayForm)
It is also possible to include this extra piece of code directly in the last string for visually different results.
jfriedman
(Jesse Friedman)
October 20, 2015, 2:05pm
3
Extending upon Xavier’s response, here is a function that replaces every ^nn^ superscript in a string with a Superscript object, and renders it as a Row.
superscriptsToRow[input_] :=
Row[StringReplace[input, "^" ~~ sup : DigitCharacter .. ~~ "^" :> Superscript["", sup]] /.
StringExpression -> List]
superscriptsToRow[stringfromfile]
tsaab
(T Saab)
October 20, 2015, 5:09pm
4
Thanks Jesse, this is exactly what I needed