I have a Mathematica expression generated by another program, which I would like to open in a notebook, properly formatted. For instance, the other program generates this:
Plot[{Exp[x],Interpolation[Table[{k/5,Exp[(k-1/2)/5]},{k,0,5}],
InterpolationOrder->0][x]},{x,0,1},Filling->{1->{{2},{Yellow,Orange}}},
PlotLabel->Style["Formatting",Blue,FontFamily->"Courier"]]
The text is written into a file, crudely suffixed “.nb”, and launched, and the expression opens in a notebook without formatting. To achieve formatting, writing a file manually with BoxData seems impractical.
The file is actually being launched from .Net using Process.Start(“filename.nb”), but a command line launch seems equally problematic.
Here is the solution I adopted. Thanks for all the help.
The main step of the solution is to format the command via the kernel:-
Then the formatted data is encapsulated to create a notebook:-
This is written to a file, suffixed “.nb”. All fine and dandy.
This approach works well for multi-statement blocks of code, but some additional processing was included to format a single function call of the form Function[expression, options] to add a line-break before each option. Here is the C# code used to produce both types of output:-