One of my cells contains some Text and Input:

some text 3+5,

so the expression underlying this looks like

Cell[  TextData[{ 
    "some text ",
     StyleBox["3+5", "Output"]
        }],   "Text"]

I now wish to select only the "3+5", using commands, as to be able to evaluate that part in place, as if one uses the command "Evaluate in Place" of the Evaluation menu (and using the notebook end to manually select 3+5).

I've tried using Cases[ ... , StyleBox[__]], but this gives an empty set. What am I missing here?

Once again, thanks for all help!


1 Answer 1


If I understand correctly you want to replace all output-style boxes with evaluated values. You can do this by

Module[{nb = EvaluationNotebook[], sel = Null},
 SelectionMove[nb, Before, Notebook, AutoScroll -> False];
  While[sel =!= {},
   SelectionMove[nb, Next, Cell, AutoScroll -> False];
   sel = NotebookRead[nb];
   If[MatchQ[sel, Cell[_, "Text", ___]], 
    NotebookWrite[nb, sel /. StyleBox[expr_, "Output"] :> 
     StyleBox[ToString[ToExpression[expr], InputForm], "Output"]
  • $\begingroup$ Thank you for your answer. It is indeed what I was looking for, but I wonder: why aren't you using SelectionEvaluate[]? Thank you for your answer! $\endgroup$
    – Gabriel
    Commented Oct 20, 2013 at 11:50
  • $\begingroup$ Second question: Why the need for ToString? Furthermore, this doesn't seem to work when using exponents in symbolic forms... Any help with that? $\endgroup$
    – Gabriel
    Commented Oct 21, 2013 at 8:35
  • $\begingroup$ @Gabriel I don't use SelectionEvaluate because I select whole cell and I want to evaluate only output-style boxes in it. Without ToString examples like a+b don't works. I edit my post to correct the problem with exponents. $\endgroup$
    – ybeltukov
    Commented Oct 21, 2013 at 10:27

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.