Often I'm making many plots for different values of parameters. Each plot has a nice PlotLabel
with superscripts, subscripts and some special characters. I would like to take this plot label and turn it into an acceptable, readable and unique file name.
Here is an example of a plot label
ac= {63425, 63433, 63432, 83, 117, 112, 101, 114, 115, 99, 114, 105, 112, \
116, 66, 111, 120, 91, 63433, 947, 63424, 44, 32, 63433, 50, 63424, \
93, 63424, 32, 97, 116, 32, 83, 105, 110, 91, 920, 93, 32, 63425, \
63433, 63432, 83, 117, 112, 101, 114, 115, 99, 114, 105, 112, 116, \
66, 111, 120, 91, 63433, 120, 63424, 44, 32, 63433, 50, 63424, 93, \
63424, 32, 63425, 63433, 63432, 83, 117, 98, 115, 99, 114, 105, 112, \
116, 66, 111, 120, 91, 63433, 121, 63424, 44, 32, 63433, 115, 117, \
98, 63424, 93, 63424};
a = FromCharacterCode[ac]
I had a hard time copying in the string that I wanted so I made a list of Characters.
The following StringReplace
is a start but I would like much better.
newA=StringReplace[a,{
" "->"-",
"\!\(\*SubscriptBox[\("~~f_~~"\), \("~~d__~~"\)]\)" :>f<>"_"<>d,
"\!\(\*SubsuberscriptBox[\("~~f_~~"\), \("~~d__~~"\), \("~~u_~~"\)]\)" :>f<>"_"<>d<>"^"<>u,
"\!\(\*SuperscriptBox[\("~~f_~~"\), \("~~u_~~"\)]\)" :>f<>"^"<>u
}];
newA=ToString[newA,CharacterEncoding->"PrintableASCII"]
This is a start but nothing that I can use yet. One of the things I would like to do is replace special characters with there long names like turning:
\[Gamma]^2-at-Sin[\[CapitalTheta]]-x^2-y_sub
into
Gamma^2-at-Sin[CapitalTheta]-x^2-y_sub
I'm also looking at using TeXForm
then doing some StringReplace
on that output.
How have others converted complex text into file names?
LabelToFile Using what bbgodfrey suggested here is a first cut at a function to do what I want:
Clear[LabelToFile];
Options[LabelToFile]={"Sub"->"_","Super"->"^","Point"->"p"};
LabelToFile[a_?StringQ,opts:OptionsPattern[LabelToFile]]:=Module[
{newA,sub,sup,pt},
sub=OptionValue["Sub"];
If[!StringQ[sub],sub="_"];
sup=OptionValue["Super"];
If[!StringQ[sup],sup="^"];
pt=OptionValue["Point"];
If[!StringQ[pt],pt="p"];
newA=StringReplace[a,{
Shortest["\!\(\*SubscriptBox[\("~~f__~~"\), \("~~d__~~"\)]\)" ] :>f<>sub<>d,
Shortest["\!\(\*SuperscriptBox[\("~~f__~~"\), \("~~u__~~"\)]\)" ] :>f<>sup<>u,
Shortest["\!\(\*SubsuperscriptBox[\("~~f__~~"\), \("~~d__~~"\), \("~~u__~~"\)]\)"]:>f<>sub<>d<>sup<>u}];
newA=ToString[newA,CharacterEncoding->"PrintableASCII"];
newA=StringReplace[newA,{
Shortest["\\[Capital"~~z__~~"]"]:>z,
Shortest["\\["~~z__~~"]"]:>ToLowerCase[z],
" "->"-",
"."->pt
}];
Return[newA]
];