tags:

views:

54

answers:

3

Hi,

I'd like to be able to mix text and computation. Something like this:

blah blah blah ...
blah blah ... The average mass is (m1 + m2 + m3)/3 = 23.4 g ... blah blah
blah blah blah ...

Where the "(m1... )/3" is the input, and the "23.4" is the output. Right now I only know how to show input in one cell, and the output in another cell below it.

Is this possible?

Update: I want to include these bits of computation in the midst of larger blocks of writing, so I'm not sure how to use a Print statement as Koantig suggested, because it seems I'd have to concatenate an entire paragraph/cell worth of strings and styles.

thanks,
Rob

+2  A: 

I suppose that you have entered your expression in a text cell. If you highlight the expression to evaluate, in your case

(m1 + m2 + m3)/3

and hit Shift+Ctrl+Enter (on my Windows box, not sure about your box, but it's option Evaluation | Evaluate in Place if you prefer the menu), then your expression will be replaced by the result of evaluating it. I know that this is not exactly what you want, but it's the closest I have found myself. I copy the expression to the rhs of the = sign and evaluate the copy.

I expect that someone will come along soon and tell us the smart way to do this.

High Performance Mark
That's the way that I do it. One extra comment is that to start an in-line math cell, press `Ctrl-(`.
Simon
+1  A: 

Maybe something like this?

m1 = 10;
m2 = 20;
m3 = 50;
f = "(m1+m2+m3)/3";
Print[f <> "= " <> ToString@N@ToExpression@f <> " g"]

The result:

(m1+m2+m3)/3= 26.6667 g
Koantig
And wrapping this in the right Boxes would produce exactly what OP wants. Probably.
High Performance Mark
I should explain better in my question that my original goal was to be in "word processor" mode, writing large blocks of text, and then have the calculated tidbits in the midst of it. So I'm still not sure how I'd do that. I wouldn't want to have a large paragraph of text enclosed in strings in the Print call.
Rob N