4
$\begingroup$

I'm trying to write a notebook from the kernel, so using one list of elements in one document, I'd like to create a second document with some interactions between the elements of the list.

For that, I'm already trying to write on a separate document, which works with

 NotebookWrite[ newDoc, Cell[ "2+3", "Input"]]

Now, this works fine, but I'm trying to get this string "2+3" computed in the new notebook. Nor using ToExpression, Evaluate seems to be working.

Any help for this? Also: is there a good book explaining these different concepts of how the Kernel works? I'm reading the online documentation, but sometimes, I'd like to have a more "book"-approach, if possible. What would you recommend?

Thanks all, once again!

$\endgroup$
2
  • $\begingroup$ Maybe NotebookWrite[newDoc, Cell["2+3", "Input"], All]; SelectionEvaluate[newDoc] ? $\endgroup$
    – andre314
    Sep 7, 2013 at 18:23
  • $\begingroup$ "The Mathematica book" has a chapter called "Manipulating Notebooks from the Kernel" : reference.wolfram.com/legacy/v5_2/book/section-2.11.3 $\endgroup$
    – andre314
    Sep 7, 2013 at 18:59

1 Answer 1

2
$\begingroup$

Let's set things up first

newDoc = NotebookPut[Notebook[{}]]
NotebookWrite[newDoc, Cell["2+3", "Input"]]

If you don't mind evaluating the whole notebook, you could do

NotebookEvaluate[newDoc, InsertResults -> True]

If you only want to evaluate that cell, may want to consider setting a CellTag for your Cell. For example

NotebookWrite[newDoc, Cell["2+3", "Input", CellTags-> "targetCell"]]

then you can evaluate only this Cell by using

NotebookEvaluate[newDoc, InsertResults -> True, 
 EvaluationElements -> {"Tags" -> {"targetCell"}}]

Note that if you want to insert Cell's in this way, it is probably a good idea to make nice box expressions instead of just strings like "2+3". An alternative would then be

NotebookWrite[newDoc, Cell[ RowBox[{"2", "+", "3"}]], "Input", CellTags-> "targetCell"]]

You can find the boxes by using Show Expression from the Cell menu.

The syntax of the option EvaluationElements is a bit confusing to me. See also this question, which is very closely related.

$\endgroup$

Your Answer

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

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