I want to apply TreeForm to some expressions of the form m[m[m[y[1], y[1]], x[1]], z], but I want Mathematica to treat expressions of the form x[i] or y[i] as atoms. For instance,

TreeForm[m[m[m[y[1], y[1]], x[1]], z]]


Output tree

but I would like the leaves to be y[1], y[1], x[1] and z. How can I do this?


1 Answer 1


Since strings are atomic and are displayed as themselves in a tree, we can just replace the terms with their string counterparts.

TreeForm[m[m[m[y[1], y[1]], x[1]], z] /. pat : _x | _y :> ToString@pat]

enter image description here

  • 1
    $\begingroup$ Thanks! Worked like a charm. $\endgroup$ Nov 28, 2019 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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