18
$\begingroup$

If you evaluate a DocumentNotebook[] expression in the front-end, it nicely displays inline, inside of an output cell in the current notebook:

DocumentNotebook displayed

For my purposes, TextCell and ExpressionCell expressions are insufficiently flexible. So (as in the question Combination of CellPrint and PrintTemporary, or DisplayForm for Cells), I'm programmatically generating some formatted output that generates raw Cell[] box expressions. For instance, the output might be

cell = Cell[TextData[{"Function ",
    Cell[BoxData[FormBox[RowBox[{"f","(","x",")"}],
        TraditionalForm]], FormatType->"TraditionalForm"]}],
   "Subsection"];

(where $f(x)$ will actually be some complicated expression).

Unfortunately, DocumentNotebook[] seems to require TextCell/ExpressionCell and doesn't work when given Cell[]s:

DocumentNotebook with Cell

So I tried using Notebook[] instead, which does work with Cell[]s. However, evaluating a Notebook expression doesn't display in the front-end:

Notebook

It displays correctly when I run NotebookPut@Notebook@{cell}, but in a new window. How can I get it to display inline in the current notebook, like how DocumentNotebook displays? I tried wrapping it in CellPrint and in DisplayForm, but those don't work for Notebook[]s.

Relatedly, what's the difference between DocumentNotebook (and friends) and Notebook? Both appear to be intended to represent a notebook as an expression, but as I discovered they behave slightly differently.

$\endgroup$
2
  • $\begingroup$ I know this doesn't answer your question, but I don't know an answer off the top of my head and I gotta go sleep. But try cell = TextCell[Row[{"Function ", ExpressionCell[TraditionalForm@Defer@f[x]]}], "Subsection"] $\endgroup$
    – Rojo
    May 20, 2013 at 3:50
  • $\begingroup$ Would you mind getting rid of the brackets in the question title? They make reading it quite hard and do not clarify as such - most readers will recognize the functions anyway. $\endgroup$
    – Yves Klett
    May 23, 2013 at 13:11

3 Answers 3

13
+50
$\begingroup$

Perhaps it's not obvious because this doesn't look like a box. But the way that DocumentNotebook, et al, get translated into Notebook is by going through MakeBoxes. Which means that RawBoxes saves the day by preserving its guts in MakeBoxes:

DocumentNotebook@{TextCell["Hello world!", "Section"], RawBoxes@cell, 
  TextCell["The solution is:", "Text"]}
$\endgroup$
3
  • $\begingroup$ Greaaaaaaaaaat!! $\endgroup$
    – Rojo
    Oct 22, 2013 at 22:53
  • $\begingroup$ +1 Is there a way to remove the border and button? Options[DocumentNotebook] seems to bring up a lot of options but I don't see anything specific there or in the docs. $\endgroup$
    – William
    Oct 24, 2013 at 3:35
  • $\begingroup$ In case anyone is interested the following automatically wraps the cells with RawBoxes and then removes the border and button to print as a single cell. $\endgroup$
    – William
    Oct 29, 2013 at 2:43
6
$\begingroup$

Not quite tested

embeddedNotebookForm/:
MakeBoxes[embeddedNotebookForm[nb:Notebook[cells_List,___]],StandardForm]:=
    MakeBoxes@DocumentNotebook[{TextCell@"tag"}]/.{
        {{Cell["tag"]}}:>Block[{},List/@
            Replace[cells,
                Cell[CellGroupData[{cs__Cell},_]]:>cs, {1}]
                /;True],
    _Notebook|_DocumentNotebook:>nb}

So you would do Notebook[...]//embeddedNotebookForm

Example, try NotebookGet[]//embeddedNotebookForm in some notebook with stuff

$\endgroup$
8
  • $\begingroup$ @Silvia, but that is not a proper Notebook[...] expression. Try to NotebookPut that and you'll get errors too $\endgroup$
    – Rojo
    May 20, 2013 at 20:41
  • $\begingroup$ Ah.. I see.. TextCell[..] is NOT a Cell expression! +1 $\endgroup$
    – Silvia
    May 20, 2013 at 20:54
  • 2
    $\begingroup$ BTW I'm always wonder would it be possible to realize a nested Notebook, which serves like the minipage environment in LaTeX. $\endgroup$
    – Silvia
    May 20, 2013 at 20:57
  • $\begingroup$ @Rojo very nice, but I had to change Replace[cells,Cell[CellGroupData[{cs__Cell},_]]:>cs, {1}] to (cells //. Cell[CellGroupData[{cs__Cell}, _]] :> cs) $\endgroup$ Jun 28, 2013 at 20:24
  • 2
    $\begingroup$ @Rojo ¡Si, señor! $\endgroup$ Jun 28, 2013 at 21:35
2
$\begingroup$

Another solution involves messing with the box structure directly.

This is just a basic example but GridBox accepts as argument cells of the form {{cell}..}. In fact this is what DocumentNotebook actually outputs Cells inside of a GridBox. Column and Row both use GridBox internally also.

A simple example would be.

CellPrint@Cell[BoxData[
   GridBox[{{
        Cell[BoxData["test"], "Input"]
      }}
    ]], "Output"]

Creating a function with MakeBoxes allows you to accept a list of cells like so. You need to use a Replace to make sure that the code works for CellGroupData because GridBox doesn't support such and returning an empty string when the Cell list is empty.

CellList /: MakeBoxes[CellList[list_], form_] := If[
  Length[list] > 0,
  GridBox[Map[{#} &,
    Replace[
     list
     , Cell[CellGroupData[{x___}, ___]] :> (x), {0, 1}
     ]
    ]],
  ""
  ];

And print cells like so.

CellList[{
  Cell[BoxData["1"], "Input"],
  Cell[BoxData["2"], "Input"],
  Cell[CellGroupData[{
    Cell[BoxData["a"], "Input"],
    Cell[BoxData["a"], "Output"]
   }, Open]]
}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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