Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create a notebook outside of Mathematica?

111 views
Skip to first unread message

kj

unread,
Dec 13, 2010, 3:52:34 AM12/13/10
to

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.

Albert Retey

unread,
Dec 14, 2010, 6:58:50 AM12/14/10
to
Hi,

I don't think it must be that complicated. If you need just InputForm
expressions separated in one cell each, this would be a prototype:

Notebook[{
Cell["(x+y)^1", "Input"],
Cell["(x+y)^2", "Input"],
Cell["(x+y)^3", "Input"]
}]

I think this should not be too difficult to generate also from other
languages...

hth,

albert

kj

unread,
Dec 15, 2010, 7:51:24 AM12/15/10
to
In <ie7m5q$b4p$1...@smc.vnet.net> Albert Retey <aw...@gmx-topmail.de> writes:
>> 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.
>>
>I don't think it must be that complicated. If you need just InputForm
>expressions separated in one cell each, this would be a prototype:

>Notebook[{
> Cell["(x+y)^1", "Input"],
> Cell["(x+y)^2", "Input"],
> Cell["(x+y)^3", "Input"]
> }]

>I think this should not be too difficult to generate also from other
>languages...


Thanks for pointing this out! It's indeed much simpler than my
attempts at reverse engineering notebooks had led me to believe.

~kj

John Fultz

unread,
Dec 15, 2010, 7:51:55 AM12/15/10
to
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
jfu...@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
>

kj

unread,
Dec 17, 2010, 3:27:29 AM12/17/10
to
In <ieadlb$r9u$1...@smc.vnet.net> John Fultz <jfu...@wolfram.com> writes:

>As has already been pointed out, notebooks with InputForm cells aren't so
>difficult to create. It's basically just...

<snip>

John, thanks for the very informative post!

~kj

0 new messages