Re: How to create a notebook outside of Mathematica?

*To*: mathgroup at smc.vnet.net*Subject*: [mg114747] Re: How to create a notebook outside of Mathematica?*From*: John Fultz <jfultz at wolfram.com>*Date*: Wed, 15 Dec 2010 07:51:55 -0500 (EST)

As has already been pointed out, notebooks with InputForm cells aren't so difficult to create. It's basically just... Notebook[{ Cell["input1","Input"], Cell["input2","Input"] }] You could also create package (.m) files, which is even easier, and will create StandardForm cells (nicer for supporting syntax highlighting and some other code-editing niceties). With package files, if you put a blank line between inputs, and then open the file in Mathematica, they'll become separate cells. So, literally, something like this would work: input1 input2 That's assuming, of course, that the inputs would represent something syntactically complete. Package files are very convenient in the way that they allow for clean interchange between a plain text environment and the Mathematica notebook interface, and they may work for you. But there's one potential challenge that might be a problem...package files always throw away their output when saved by Mathematica. So, if you're trying to generate notebooks which can be executed and then saved with full output, that might be an issue. In that case, you can convert the package file to a notebook file by doing... File->Save As... and choosing Notebook as the type. You can do this programmatically by doing... FrontEndExecute[FrontEndToken[nb,"Save", {filename,"Notebook"}]] where nb is the NotebookObject of the package window (EvaluationNotebook[],for example), and filename is the string containing the name of the target .nb file. Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc. On Mon, 13 Dec 2010 03:52:51 -0500 (EST), kj wrote: > > I often would like to be able to use some external program (e.g. > a Python script) to generate the input expressions for a Mathematica > notebook. > > The problem is that I don't know how best to turn such a file of > Mathematica expressions into something that Mathematica will > recognize as a notebook. > > One naive approach would be to tack the extension .nb to such a > file, and open the file with Mathematica, but this doesn't work > too well. Mathematica *will* open such a file without any fuss, > but it stuffs all the expressions into a single cell. As a result, > evaluating such a notebook produces a huge, unreadable heap of > output right after the bloated single input cell. > > Is there a better way to do this, so that I don't end up with all > the expressions in a single cell? > > For example, is there an easy way to tell Mathematica to treat > expressions separated by at least 2 newlines as belonging to > different cells? Alternatively, is there a way to automate the > splitting of the single cell into multiple ones? > > TIA! > > ~kj > > P.S. I realize that regular notebooks generated by Mathematica > are just text files, describing cells, etc., as Mathematica > expressions, and, of course, I could just write *that* out, i.e. > not just the expressions I'm interested in, but also the surrounding > cell specifications. This is way beyond my grasp of Mathematica's > internal notebook format, and this is an area in which the > documentation is *particularly* cryptic, so I prefer not to go > anywhere near such a solution.