Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have some calculations with arbitrary functions. In the output, Mathematica always shows the functions with its arguments. I would like to tidy the notation a bit, by hiding the arguments in the output.

For example, I have something like this:

f[x,y] + (g[x,y])^2 = ...

and prefer like this:

f + g^2 = ...

I would like to create a function to do this work on any expression, involving all functions... is that possible?


I had outputs like this:

enter image description here

I then found a function to transform in a more human way:

pdConv[f_] := TraditionalForm[f /. Derivative[inds__][g_][vars__] :> 
    Apply[Defer[D[g[vars], ##]] &, Transpose[{{vars}, {inds}}] /. {{var_, 0} :> 
    Sequence[], {var_, 1} :> {var}}]];

Much better now:

enter image description here

Then I modified it, by taking out the [vars] from g[vars]:

pdConv[f_] := TraditionalForm[f /. Derivative[inds__][g_][vars__] :> 
    Apply[Defer[D[g, ##]] &, Transpose[{{vars}, {inds}}] /. {{var_, 0} :> 
    Sequence[], {var_, 1} :> {var}}]];

It now looks like this (notice that the functions arguments don't appear on functions inside the derivatives):

enter image description here

Now the last step would be to take off the arguments that appear on the functions outside derivates. The code above was able to do some of the work in a general way... that's what I'm looking for.

share|improve this question
Try adding the rule f /: Format[f[___]] := f; and similarly for g. –  J. M. Nov 24 '12 at 17:04
Isn't there a general expression? Thanks for the help. –  Giovanni Nov 24 '12 at 17:17
It is possible, but you need to know yourself clearly what you want before you can tell Mathematica. If you were given an output to format, how would you decide what's a function and what's not? Say Plus[x, f[y,z]] –  Rojo Nov 24 '12 at 17:17
Why, do you want this to happen for built-in functions as well? –  J. M. Nov 24 '12 at 17:18
For reference, the function pdConv comes from this page on the Wolfram Blog. –  Jens Nov 25 '12 at 0:31
show 1 more comment

3 Answers

up vote 3 down vote accepted

I propose a whitelist approach for functions you do want to display the contents of:

clean[expr_] :=
  HoldForm[expr] /.
    (h : Except[HoldForm | Equal | Power | Times | Plus | Log | Subscript])[args___] :> h

eqn = Subscript[R, tt\[Phi]]^\[Phi] == 
   D[\[Xi][r, z], r] D[\[Xi][r, z], 
     z] Exp[4 \[Psi][r, z] - 2 \[Nu][r, z]]/\[Xi][r, z];

eqn // clean

Mathematica graphics

You have already shown how to handle the special case of Derivative which can be combined with this method.

Alternatively you could show only full expressions for functions in a given context, e.g. System`:

clean2[expr_] :=
  HoldForm[expr] /.
    h_Symbol[args___] /; Context@Unevaluated@h =!= "System`" :> h

eqn // clean2

Mathematica graphics

Again this doesn't handle the case of Derivative or SubValues functions.

share|improve this answer
Perfect! It's working very well. Thank you! :) –  Giovanni Nov 25 '12 at 0:56
I added Log so that Log[f[x, y]] // clean works –  chris Nov 25 '12 at 7:39
@chris That's okay, but I intended this only as an example. Only Giovanni knows which functions he wants fully displayed. –  Mr.Wizard Nov 25 '12 at 7:43
@Mr.Wizard yes; I don't see why its better to specify functions you don't want because its as endless as functions you want; right now BesselY[2, f[x, y]] // clean does to work either ;-) –  chris Nov 25 '12 at 7:45
In fact its probably less open-ended(?) to list variables you introduced rather than functions that might occur. –  chris Nov 25 '12 at 7:46
show 5 more comments

Restrict on arguments

As others have mentioned replacing all functions with arguments with just their names wouldn't leave you with something very useful, so you have to make some restrictions. It has been shown how you can restrict for which functions such replacements should be made. What I would like to add is the possibility to make such redefinitions only for certain kinds of arguments. This could be as simple as:

eqn = Subscript[R, tt\[Phi]]^\[Phi] == 
   D[\[Xi][r, z], r] D[\[Xi][r, z], 
     z] Exp[4 \[Psi][r, z] - 2 \[Nu][r, z]]/\[Xi][r, z];
(eqn // pdConv) /. {f_[r, z] :> f}

for the example you gave.

Formal Parameters

A feature that is available since Mathematica 7 are special names for "formal parameters" which are Protected so you can be reasonably sure they won't have values. I think it's just such formula type expressions as yours that these were introduced, so you might want to use them for your arguments. If you do so you could make this approach somewhat more general: First define the helper function:

formalQ[___] = False;
formalQ[x_Symbol] := StringMatchQ[
   ToString[x, CharacterEncoding -> "ASCII"], "\\[Formal" ~~ __

and then define your expressions using those formal parameters (which you can enter with e.g. Esc-$-x-Esc).

eqn = Subscript[R, tt\[Phi]]^\[Phi] == 
   D[\[Xi][\[FormalR], \[FormalZ]], \[FormalR]] D[\[Xi][\[FormalR], \
\[FormalZ]], \[FormalZ]] Exp[
      4 \[Psi][\[FormalR], \[FormalZ]] - 
       2 \[Nu][\[FormalR], \[FormalZ]]]/\[Xi][\[FormalR], \[FormalZ]];

and now use something like this to get the desired simplification:

(eqn // pdConv) /. {f_[__?formalQ] /; Context[f] =!= "System`" :> f}


Another thing I would like to mention is that you could make your results look the way you want but still evaluate correctly by using Interpretation. The following does this for a combination of pdConv, the above replacement and some additional formatting so one can immediately see which of the variables are actually functions with left out parameters:

formatEqn[expr_] := Interpretation[TraditionalForm[
    expr /. {
       Derivative[inds__][g_][vars__] :> Interpretation[Apply[
          HoldForm[D[g[vars], ##]] &, Transpose[{{vars}, {inds}}] /. {
            {var_, 0} :> Sequence[],
            {var_, 1} :> {var}
       } /. {
      \[FormalF]_Symbol[\[FormalA]__?formalQ] /; 
        Context[\[FormalF]] =!= "System`" -> 
        Style[\[FormalF], RGBColor[0, 0.6, 0], 
         Bold], \[FormalF][\[FormalA]]]

You should now be able to copy the result (or parts of it), paste to a new input cell and evaluate that new cell. It would also be possible to programmatically do further work with the result given above with something like this:

formattedEqn = eqn // formatEqn
(ToExpression[ToBoxes[formattedEqn]] /. \[Xi]->Function[Sin[#1]*Cos[#2]])// formatEqn

I'm guess that this will not exactly match your needs but think with this and the information others gave and some further reading of the documentation of the functions used/mentioned above you should be well prepared to exactly build what you need...

share|improve this answer
Interesting. Worked well too. Thank you. –  Giovanni Nov 25 '12 at 18:04
add comment

As suggested in the comments

eqn = 
 Subscript[R, ttϕ]^ϕ == 
  D[ξ[r, z], r] D[ξ[r, z], 
    z] Exp[4 ψ[r, z] - 2 ν[r, z]]/ξ[r, z]

Using a Format rule:

Format[ξ[r, z]] = ξ;
Format[ψ[r, z]] = ψ;
Format[ν[r, z]] = ν;


Mathematica graphics

share|improve this answer
Yes, it works, but again, I would like to know if there is a general expression that converts everything at once. I use different symbols from time to time and if this needs to be done every time, I prefer not doing it and leaving in the original format. Thanks anyway. –  Giovanni Nov 24 '12 at 20:25
One would need to exclude Built in symbols as you probably don't want e.g. Log[f[r,z]] to become Log –  chris Nov 24 '12 at 21:31
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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