I often see on this site and at the mathgroup the repeated questions on how to rearrange expression that Mathematica "likes" to keep in one form, but the user prefers in another. Consider this trivial example:

(x^2 - 1)/(x + 1) // Simplify

Mathematica returns -1 + x, rather than the x - 1 that the user might prefer.

I have seen many answers to these questions and gave a few myself. The problem here is that the answers are non-universal; they strongly depend upon the expression in question. Besides, they require some additional programming for sorting terms, and the more complex the formula, the longer will be the code that does the sorting.

It seems that the problem of sorting terms in analytic expressions into the desired order might be solved, if one could assign tags to the terms to be sorted. One would then sort the terms according to a specified list of such tags.

Now comes my question, can you tell me how to apply tags to expression terms?

  • $\begingroup$ Alexei, I think answered your direct question. Does it help? Are you asking for assistance implementing your scheme as well, or is the basic tagging method useful in itself? $\endgroup$
    – Mr.Wizard
    Apr 15, 2013 at 21:58
  • $\begingroup$ @Mr.Wizard Thank you. It is useful in itself. I did not think about this possibility. On the other hand, ideas of how to reorder terms in the output would be also helpful. The example above is too simple and may be reordered by Traditional form. My ultimate aim is to learn how to reorder one or several subexpressions of a complex expression, the subexpressions consisting of more than two terms. $\endgroup$ Apr 17, 2013 at 11:45

1 Answer 1


Attempting to answer the question itself, at a low level (box form) you can use TagBox, and at a higher level you might use Annotation. Both display without a visible tag:

Annotation[3, "tag1"]
TagBox[3, "tag1"] // DisplayForm

Annotation formats as a TagBox upon output (conversion to boxes).

Annotation[3, "tag1"] // ToBoxes // InputForm
TagBox["3", Function[Annotation[Slot[1], "tag1"]]]

Your Answer

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

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