Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

3 Answers

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

share|improve this answer
    
@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
1  
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
1  
@Rojo ¡Si, señor! –  Rolf Mertig Jun 28 '13 at 21:35
show 3 more comments

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"]}
share|improve this answer
    
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
add comment

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]]
}]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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