12
$\begingroup$

With Wolfram|Alpha, if I enter:

Integrate[r^3, {t, 0, 2Pi}, {r, 0, 2}, {z, r^2, 4}]

Wolfram|Alpha will pretty print my input like so:

enter image description here

Which is very readable, and I can easily check to make sure I entered in my bounds and integrand correctly. Were as if I entered that formula straight into Mathematica, it just spits out the answer and I have to manually inspect my raw input for typos.

So, my question is: Is it possible to nicely display formulas in Mathematica without using Wolfram|Alpha?

I want a way to convert my raw input "Integrate[r^3, {t, 0, 2Pi}, {r, 0, 2}, {z, r^2, 4}]" into a more human-friendly form.

(The problem with calling Wolfram|Alpha from Mathematica all the time is that it is slow, sometimes times out, requires internet, and is often much more noisy than I need.)

$\endgroup$
  • $\begingroup$ Select cell -> Right click -> Convert to -> TraditionalForm $\endgroup$ – Sektor Nov 30 '14 at 21:55
  • $\begingroup$ Also, typing Esc + int + Esc enters $\int$, which I've always found to be far more convenient than using the drop-down menus. $\endgroup$ – DumpsterDoofus Nov 30 '14 at 22:29
  • $\begingroup$ @DumpsterDoofus Could use intt instead of int to get a complete template for the integral with placeholders for the bounds. $\endgroup$ – Sjoerd C. de Vries Dec 1 '14 at 7:13
  • $\begingroup$ See reference.wolfram.com/language/guide/…, especially the Learning Resources part. $\endgroup$ – Sjoerd C. de Vries Dec 1 '14 at 7:16
15
$\begingroup$

Something like

heldint = HoldForm[Integrate[r^3, {t, 0, 2 Pi}, {r, 0, 2}, {z, r^2, 4}]];
int = ReleaseHold[heldint];

Row[{heldint, " \[LongEqual] ", int, " \[TildeTilde] ", N[int]}] // TraditionalForm

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Beautiful! I think I'll end up using // HoldForm // TraditionalForm as my preferred way to verify my input $\endgroup$ – Daryl Nov 30 '14 at 22:43
  • $\begingroup$ Is there any way to convert those 3 lines of yours into a single function? So I could do something like Integrate[...] // Pretty and see exactly the output you showed $\endgroup$ – Daryl Nov 30 '14 at 22:46
  • 3
    $\begingroup$ Got it: SetAttributes[Pretty, HoldAll]; Pretty[fn_] := (heldint = HoldForm[fn]; int = ReleaseHold[heldint]; Row[{heldint, " \[LongEqual] ", int, " \[TildeTilde] ", N[int]}] // TraditionalForm); $\endgroup$ – Daryl Nov 30 '14 at 22:54
  • 1
    $\begingroup$ Yes. You could do int = fn; instead of int = ReleaseHold[heldint];. $\endgroup$ – Chip Hurst Dec 1 '14 at 5:22
12
$\begingroup$

TraditionalForm looks nice but it also incurs additional complexity in handling expressions. If you attempt to evaluate a TraditionalForm cell you will be asked if you wish to interpret it as input, and the equivalence is not always complete. Fortunately Mathematica is quite cabable of displaying formatted integrals in StandardForm.

If you merely wrap your expression in HoldForm and evaluate it you will get:

Integrate[r^3, {t, 0, 2 Pi}, {r, 0, 2}, {z, r^2, 4}] // HoldForm

enter image description here

You can convert the expression in place without evaluation by selecting it and using menu Cell > Convert To > StandardForm which under Windows has the keyboard shortcut Shift+Ctrl+N. Example:

enter image description here

Note the active syntax highlighting which is not present in TraditionalForm.

$\endgroup$
  • $\begingroup$ IMO this is the answer OP was looking for. To be honest I didn't read the original question carefully and just provided a way to mimic Alpha's output. $\endgroup$ – Chip Hurst Dec 1 '14 at 5:15
6
$\begingroup$

Mathematica 10 introduced a new set of functions that can help with this:

    Inactive[Integrate][r^3, {t, 0, 2 Pi}, {r, 0, 2}, {z, r^2, 4}]

enter image description here

Or, obtaining same output:

    Inactivate[Integrate[r^3, {t, 0, 2 Pi}, {r, 0, 2}, {z, r^2, 4}]]

And then:

    Activate[%]
(* 32 Pi/3 *)
$\endgroup$
2
$\begingroup$

wiki answer. All the above solutions are fine, but I think Maple does this much simpler and I thought to show how Maple does it, may be future versions of M can adopt this. (in some way, can't use the same Upper/Lower trick ofcourse, since M commands already start with UpperCase)

In Maple, there is what is called the Inert form. Command that starts with upper case, do not evaluate. This way one can verify the command by typing Int instead of int then to actually evaluate, type value() on the form. Here is an example

Mathematica graphics

You can see how simple it is.

update: Thanks Karsten comment, Mathematica in V10 seems to have adopted this using Inactive (I never used this command myself actually before...too many commands, too little time). But it seem to do what Inert in Maple does:

Mathematica graphics

Very nice. I think I'll start using this more.

$\endgroup$
  • 1
    $\begingroup$ Inactive seems to be the adoption made in v10. $\endgroup$ – Karsten 7. Dec 1 '14 at 4:28
  • 1
    $\begingroup$ @Karsten7.: Yes, but will probably also work for v11, v12 and further on $\endgroup$ – Alexey Bobrick Dec 6 '14 at 14:49
  • $\begingroup$ @Karsten7.: I would be happier if it had a "pretty" input like the output! :) Does it have such a thing? $\endgroup$ – H. R. Mar 17 '16 at 18:00
  • $\begingroup$ @H.R. I'm not sure what you mean, but you can try 1) Using one of the Palettes 2) Marking the input, right click and choose Convert To StandardForm or TraditionalForm 3) Using InputAliases (for example Esc dintt Esc). $\endgroup$ – Karsten 7. Mar 17 '16 at 20:42
  • $\begingroup$ @Karsten7.: Let me clarify myself. For example, active integration has a command form like Integrate[f[x],{x,a,b}]. Also, it has a pretty form which we can achieve by Esc+dintt+Esc. Does inactive integration has a pretty form like the latter case. $\endgroup$ – H. R. Mar 18 '16 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.