6
$\begingroup$

To explain, I do most of my probability homework in Mathematica, and use the typesetting built into Mathematica to finish answers. Often, I want to output a function as the answer to a problem, but I want it to be explicit, that is, in the example below, I create a function, X[x,y,z], and I want the result to not just display the (in this case) piecewise function, but I want it to define it as a function. As an example:

Clear[X, x, y, z]
X[x_, y_, z_] := Piecewise[{{3/(4*Pi), Sqrt[x^2 + y^2 + z^2] <= 1}}, 0]
X[x, y, z]//TraditionalForm

The output of this is $$ \left\{ \begin{array}{cc} \frac{3}{4 \pi } & \sqrt{x^2+y^2+z^2}\leq 1 \\ 0 & \text{True} \\ \end{array} \right. $$

I want it (the output) to look (something) like

$$ X(x,y,z)=\left\{ \begin{array}{cc} \frac{3}{4 \pi } & \sqrt{x^2+y^2+z^2}\leq 1 \\ 0 & \text{True} \\ \end{array} \right. $$

$\endgroup$
  • 1
    $\begingroup$ Perhaps HoldForm[X[x, y, z]] == X[x, y, z] // TraditionalForm? $\endgroup$ – wxffles Oct 20 '14 at 20:50
  • $\begingroup$ Yeah, that seems to pretty much do what I want it to. I think I'd prefer it without the parenthesis it puts around the piecewise equation, but it does give me the result I want. Thanks! $\endgroup$ – Michael Witt Oct 20 '14 at 20:59
7
$\begingroup$

To expand my comment into an answer:

HoldForm[X[x, y, z]] == X[x, y, z] // TraditionalForm

$$X(x,y,z)=\left( \left\{ \begin{array}{cc} \frac{3}{4 \pi } & \sqrt{x^2+y^2+z^2}\leq 1 \\ 0 & \text{True} \\ \end{array} \right. \right)$$

But Mathematica decides it needs parentheses. I agree that this doesn't look ideal. There are a couple of ways around it. One is that you can manually edit the traditional form in Mathematica. Another option is to piece it together ourselves in a Row (with a goofy equals to nothing):

Clear[traditionalise];
Attributes[traditionalise] = {HoldAll};
traditionalise[f_] := TraditionalForm@Row@{HoldForm@f == " ", f}

traditionalise[X[x,y,z]]

$$ X(x,y,z)=\left\{ \begin{array}{cc} \frac{3}{4 \pi } & \sqrt{x^2+y^2+z^2}\leq 1 \\ 0 & \text{True} \\ \end{array} \right. $$

$\endgroup$
  • 2
    $\begingroup$ In Alpha, we usually do something like Row[{HoldForm[f], " \[LongEqual] ", f}] // TraditionalForm. $\endgroup$ – Chip Hurst Oct 21 '14 at 0:28
  • 2
    $\begingroup$ @Chip Why not Row[{HoldForm[f], f}, "\[LongEqual]"]? I believe that uses the proper spacing rules. $\endgroup$ – Mr.Wizard Oct 21 '14 at 1:25
  • $\begingroup$ Yes, you are correct. I guess I stretched the truth a bit. The idiom I referred to is used in other custom Row-like constructs. $\endgroup$ – Chip Hurst Oct 21 '14 at 2:13

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.