# How do I format the output to include the function definition?

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]


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.$$

• Perhaps HoldForm[X[x, y, z]] == X[x, y, z] // TraditionalForm? – wxffles Oct 20 '14 at 20:50
• 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! – Michael Witt Oct 20 '14 at 20:59

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];

$$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.$$
• In Alpha, we usually do something like Row[{HoldForm[f], " \[LongEqual] ", f}] // TraditionalForm. – Chip Hurst Oct 21 '14 at 0:28
• @Chip Why not Row[{HoldForm[f], f}, "\[LongEqual]"]? I believe that uses the proper spacing rules. – Mr.Wizard Oct 21 '14 at 1:25
• 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. – Chip Hurst Oct 21 '14 at 2:13