# Notebook vs. DocumentNotebook; display a Notebook inline, like a DocumentNotebook

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

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",")"}],
"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:

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

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.

-
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"] –  Rojo May 20 '13 at 3:50
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. –  Yves Klett May 23 '13 at 13:11

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

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

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"]}

-
Greaaaaaaaaaat!! –  Rojo Oct 22 '13 at 22:53
+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. –  Liam William Oct 24 '13 at 3:35
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. –  Liam William Oct 29 '13 at 2:43

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]]
}]

-