I'm using Mathematica for some calculations that I need to put into the report. As a text editor I'm using MS Word + MathType, but that is not the main case. The thing is that I have to format my calculations like this:

Let R = 3. Then, the volume of the sphere is $$ V=\frac{4\pi R^3}{3} = \frac{4\pi \cdot 3^3}{3} = 113.1$$

This is just an example, but it shows what I need: the output that Mathematica provides has to be not only as a number, but as a copyable textline with the new variable (V in this case) at the beginning, then the equal mark, then the formula as text, equal mark, formula with numbers, equal mark again, and finally the result. And I also need to manipulate the roundoff of the result (say, like the Round[] function does). The way, how I'd want to have it in Mathematica is a function like N[], when it goes like this:

In[1]:=  2Pi//N
Out[1]:= 6.28319

Or, to be specific:

In[1]:=  R=3;
In[2]:=  V=(4 Pi*R^3)/3 //FriendlyOutput

Out[2]:= Here comes the expression shown above

I have tried to create such function, but I came up only with two variants:

1) In[1]:= Print[HoldForm[(4 Pi*R^3)/3], HoldForm["="], Round[(4 Pi*R^3)/3, 0.01]]


2) In[1]:= {HoldForm[(4 Pi*R^3)/3], "=", Round[(4 Pi*R^3)/3, 0.01]}


But as you can see they are way far from what I want, they're not functions that I could apply directly to the expression, and, not to mention, when I copy the output as a MathML, LaTeX or directly into Word by pressing Ctrl+C, it's full of unnecessary garbage (I would show it, but there's a limit on the amount of links).

Is there any way to make it convenient and ready-to-copy as I mentioned at the beginning?


2 Answers 2

f[x_] := RowBox[{"V=", 
FractionBox[RowBox[{4, "\[Pi]", SuperscriptBox["R", 3]}], 3], "=",
 RowBox[{4, "\[Pi]", 
   SuperscriptBox[ToBoxes[x, TraditionalForm], 3]}], 3], "=", 
ToBoxes[4 Pi x^3/3, TraditionalForm]}] // DisplayForm    

would work. You only need to copy the result as Latex texts.

  • $\begingroup$ Although your solution gives a nice copyable output, the method is not what I'm actually looking for. Pirx was closer on that, as his solution is much easier to use (but still, I'm looking for even more convenient way — see my comments to his answer). Howewer, your remark about copying as Latex was pretty useful, so thanks for that. $\endgroup$
    – Dimitry
    Oct 18, 2016 at 22:33

Or, you could simply define

g[x_] := V == 4 Pi R^3/3 == (Defer[4 Pi x^3/3]) == N[4 Pi x^3/3]

Of course, you can wrap whatever rounding or number format you like around the last expression. The main trick is to use the ==, or \[LongEqual] instead of the equal sign.

  • $\begingroup$ Your method is neat and pretty convenient to use, but I ment it in slightly different way. You see, is your solution if I enter a new mathimatical expression, I have to copy it twice (to Defer[] and to N[] functions), not to mention, that I need to paste the whole function g[x_] every single time. This is better, that nothing, but what I was asking is a bit different. Let me give you and example, of what kind of function I's like to have: $\endgroup$
    – Dimitry
    Oct 18, 2016 at 22:19
  • $\begingroup$ In := V = 4 Pi R^3/3 //FunctionINeed $\endgroup$
    – Dimitry
    Oct 18, 2016 at 22:21
  • $\begingroup$ Out := is what your solution gives $\endgroup$
    – Dimitry
    Oct 18, 2016 at 22:22
  • $\begingroup$ *typos in first comment: is your solution = in your solution; I's like to have = I'd like to have $\endgroup$
    – Dimitry
    Oct 18, 2016 at 22:24
  • $\begingroup$ And also, there're a couple of things in your solution, that doesn’t work exactly how I need. The first is that your function doesn't assign the result to the variable (V in this case). The second is that in case I have assigned the variable R before (e.g. R=2), it will replace the name of the variable with it's value. It means, that I can't initialize the variables and hold all their values, which defeats the convenience of further calculations. $\endgroup$
    – Dimitry
    Oct 18, 2016 at 22:49

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.