This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Manipulating Notebooks  /

2.10.2 Notebooks as Mathematica Expressions


Expressions corresponding to notebooks.

  • Here is a simple Mathematica notebook.
  • Here is the expression that corresponds to this notebook.
  • Notebook[{
    Cell["Section heading", "Section"],
    Cell["Some text.", "Text"],
    Cell["More text.", "Text"]}]

    Just like individual cells, notebooks in Mathematica can also have options. You can look at and modify these options using the options inspector in the standard notebook front end.


    A few of the large number of possible options for notebooks.

    In addition to notebook options, you can also set any cell option at the notebook level. Doing this tells Mathematica to use that option setting as the default for all the cells in the notebook. You can override the default by explicitly setting the options within a particular cell.

  • Here is the expression corresponding to a notebook with a ruler displayed in the toolbar at the top of the window.
  • Notebook[{
    Cell["Section heading", "Section"],
    Cell["Some text.", "Text"]},
    WindowToolbars->{"RulerBar"}]

  • This is what the notebook looks like in the front end.
  • This sets the default background color for all cells in the notebook.
  • Notebook[{
    Cell["Section heading", "Section"],
    Cell["Some text.", "Text"]},
    Background->GrayLevel[.7]]

  • Now each cell has a gray background.
  • If you go outside of Mathematica and look at the raw text of the file that corresponds to a Mathematica notebook, you will find that what is in the file is just the textual form of the expression that represents the notebook. One way to create a Mathematica notebook is therefore to construct an appropriate expression and put it in a file.
    In notebook files that are written out by Mathematica, some additional information is typically included to make it faster for Mathematica to read the file in again. The information is enclosed in Mathematica comments indicated by (* ... *) so that it does not affect the actual expression stored in the file.


    Setting up notebooks in the front end from the kernel.

  • This writes a notebook expression out to the file sample.nb.
  • In[1]:= Notebook[{Cell["Section heading", "Section"],
    Cell["Some text.", "Text"]}] >> "sample.nb"

  • This reads the notebook expression back from the file.
  • In[2]:= <<sample.nb

    Out[2]=

  • This opens sample.nb as a notebook in the front end.
  • In[3]:= NotebookOpen["sample.nb"]

    Once you have set up a notebook in the front end using NotebookOpen, you can then manipulate the notebook interactively just as you would any other notebook. But in order to use NotebookOpen, you have to explicitly have a notebook expression in a file. With NotebookPut, however, you can take a notebook expression that you have created in the kernel, and immediately display it as a notebook in the front end.

  • Here is a notebook expression in the kernel.
  • In[4]:= Notebook[{Cell["Section heading", "Section"],
    Cell["Some text.", "Text"]}]

    Out[4]=

  • This uses the expression to set up a notebook in the front end.
  • In[5]:= NotebookPut[%]

    Out[5]=

  • You can use NotebookGet to get the notebook corresponding to a particular NotebookObject back into the kernel.
  • In[6]:= NotebookGet[%]

    Out[6]=