0
$\begingroup$

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]
 ];
$\endgroup$
  • 3
    $\begingroup$ In light of the number of contortions you'll likely need to go through to ensure valid names, seems to me making a simple index file with the "pretty" form and a reference to a simple file name would be a whole lot easier... $\endgroup$ – ciao Mar 2 '15 at 2:32
  • $\begingroup$ I have certainly done that many times before and it works well for a set of plots that are similar and works very well when making an animation for a beamer where I can also have Mathematica write the LaTeX code. But when playing with parameters for plots that have to be manually copied PowerPoint it is nice to have a systematic way of generating human readable file names. $\endgroup$ – c186282 Mar 2 '15 at 21:48
3
$\begingroup$

The command,

StringReplace[newA, Shortest["\\[" ~~ z__ ~~ "]"] :> z]

replaces strings like \[string] by string. In particular, it transforms newA to

Gamma^2-at-Sin[CapitalTheta]-x^2-y_sub

as requested. Of course, this procedure will not work, if the string itself contains \\[ or ].

$\endgroup$
0
$\begingroup$

I suggest you do it by hand, using a convention something like this:

filename[ac] = "gamma_2_at_sin_theta_x_2_y"

If you have so many outputs that you can't do it by hand, use some kind of index file as per @rasher.

I don't think you should do this automatically, because it will be very difficult to write a safe robust general case that avoids difficult characters in file names, and produces comprehensible useful output. It's possible but not an instructive use of mathematica.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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