I find that cell output below each cell terrible for organizational purposes. Is there another way to display the output of each cell?
3 Answers
You can find endless ways to get what you have asked for looking Help for CreateDocument
or NotebookWrite
:
myNb = CreateDocument[1 + 1];
NotebookWrite[myNb, 2 + 2];
Another possibility, as example, is:
fileOut = "example.txt";
streamOut = OpenWrite[fileOut];
Write[streamOut, OutputForm[Log[1/x]]];
Close[streamOut];
Can you, please, describe your needs in detail ?
Modifying an example in the documentation to $Pre, one can save all output and then process it as desired. Begin by defining.
SetAttributes[saveoutputs, HoldAll];
outputs = {};
saveoutputs[new_] := (outputs = {outputs, HoldForm[new]}; new);
$Post = saveoutputs;
Then create some output, for instance,
r = 3.2
(* 3.2 *)
area = Pi r^2
(* 32.1699 *)
A calculation with no output
noout = E^2;
Two lines in a single cell
Sqrt[3]
%^2
(* Sqrt[3] *)
(* 3 *)
Solve
an equation
Solve[x^5 - x + 1 == 0, x]
(* {{x -> Root[1 - #1 + #1^5 &, 1]},
{x -> Root[1 - #1 + #1^5 &, 2]},
{x -> Root[1 - #1 + #1^5 &, 3]},
{x -> Root[1 - #1 + #1^5 &, 4]},
{x -> Root[1 - #1 + #1^5 &, 5]}} *)
Finally, turn off the output-saving.
$Post =.
All the output now is in outputs
, as can be seen from
Flatten[outputs] // TableForm
Note that each element of outputs
remains in HoldForm
. A user wishing to use parts of outputs
can copy the desired line and paste it as input. Usually, it also is possible to apply ReleaseHold
to the desired element, as in
ReleaseHold[Flatten[outputs][[7]]]
(* {{x -> Root[1 - #1 + #1^5 &, 1]},
{x -> Root[1 - #1 + #1^5 &, 2]},
{x -> Root[1 - #1 + #1^5 &, 3]},
{x -> Root[1 - #1 + #1^5 &, 4]},
{x -> Root[1 - #1 + #1^5 &, 5]}} *)
If you want to change this at the Notebook
level try setting CellEvaluationFunction
at the stylesheet level to evaluate and then return Null
.
i.e., evaluate the following in the stylesheet editing page
CellPrint@
Cell[
StyleData["Notebook"],
CellEvaluationFunction->(someContext`$evalFunction@ToExpression@#;&)
]
The ToExpression
is necessary as the function takes the BoxData
argument of the cell. The someContext`
is useful because otherwise evalFunction
will be tagged as $CellContext`evalFunction
and so will, I believe, depend on the context of the cell (usually Global`
). Just makes it a little bit cleaner.
Now whatever you set as someContext`$evalFunction
will be applied to the output of the evaluation for any cell in the notebook but no output cells will be returned.
Here's an example pushing the outputs onto the end of a new document:
CellPrint@
Cell[
StyleData["Notebook"],
CellEvaluationFunction->
(If[!MatchQ[
NotebookInformation@betterFormatting`Private`nb,_List],
betterFormatting`Private`nb=
CreateDocument[{},
WindowTitle->"Output Notebook",
System`ClosingSaveDialog->False]
];
SelectionMove[betterFormatting`Private`nb,After,Notebook];
NotebookWrite[betterFormatting`Private`nb,
Cell[BoxData@ToBoxes@ToExpression@#,"Output"]
];&)
]