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.

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!

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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"]
]]]]
share|improve this answer
    
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! –  Gabriel Oct 20 '13 at 11:50
    
Second question: Why the need for ToString? Furthermore, this doesn't seem to work when using exponents in symbolic forms... Any help with that? –  Gabriel Oct 21 '13 at 8:35
    
@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. –  ybeltukov Oct 21 '13 at 10:27
    
Thank you for the change! –  Gabriel Oct 21 '13 at 14:54
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.