6
$\begingroup$

I have a very simple problem with Mathematica that I couldn't find an easy answer to. I have long notebook with a lot of cells and want to evaluate a specific cells inline from another cell.

So far I have been trying to put tags on the cells and then use: NotebookEvaluate[EvaluationNotebook[], EvaluationElements -> "Tags" -> {tag1, tag2,..}]

I thought that this should do it, but instead it always evaluates the whole notebook and gets stuck in a loop. Is there something slightly wrong, or a trick with the tag names? Or am I doing something conceptually wrong?

Btw it works fine if I use the option EvaluationElements-> "InitializationCell" but doesn't work properly with the "Tags" option.

$\endgroup$
3
  • $\begingroup$ possible bug. With V9 on OS X I get repeated (endless loop) print statements $\endgroup$ Commented Jan 8, 2015 at 7:58
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Commented Jan 8, 2015 at 9:38
  • $\begingroup$ I can reproduce the problem in V9.0.1 on Win7/64. It evaluates all elements in the notebook, ignoring the EvaluationElements. If the NotebookEvaluate expression is in the target notebook, it loops indefinitely. In V10.0.2 it works properly, even from within the same notebook. $\endgroup$
    – WReach
    Commented Jan 8, 2015 at 15:30

1 Answer 1

7
$\begingroup$

Assuming you are doing this in the same notebook then:

nb = EvaluationNotebook[];
NotebookFind[nb, "tag1", All, CellTags]
SelectionEvaluate[nb]

If you have several tagged cells that you want to evaluate then

(NotebookFind[nb, #, All, CellTags];SelectionEvaluate[nb]) & /@ {"tag1", "tag2"}

Edit

The above is the way I would normally do it because it predates the addition of EvaluationElements. I tried that method and it failed in V9. Additionally the example in the docs fails (V9 OS X) which seems a good enough reason to call this a V9 bug.

Contrary to earlier edit this is now working in V10.

$\endgroup$
3
  • 1
    $\begingroup$ +1. When I tried it in V10.0.2 (Win 7/64) it worked perfectly, evaluating all (and only) tagged cells. NotebookEvaluate is defined to return only the value from the last evaluation, so it is hard to see the failure from just the screenshot since none of the cells have visible side-effects. Does it fail if the evaluated cells contain Print expressions, or the InsertResults -> True option is included? $\endgroup$
    – WReach
    Commented Jan 8, 2015 at 15:40
  • $\begingroup$ @WReach you are correct. It is working okay in V10. It looks like a V9 bug only -- fixed in 10. $\endgroup$ Commented Jan 8, 2015 at 22:14
  • $\begingroup$ Thank you Mike, your solution works for me. I am indeed using V9 so there seems to be a bug with the EvaluationElements option. But your workaround does give the desired result in V9. Thanks a lot! $\endgroup$
    – Gradd
    Commented Jan 9, 2015 at 6:09

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.