Is it possible to preserve a specific style of notation in $Mathematica$ output?

foo // bar
(* bar[foo] *)

Hold[foo // bar]
(* bar[foo] *)

I'm writing some code that generates sample code (e.g. a usage hint), and in one case I feel that postfix notation is easier on the eyes. I know it's subjective and not that important but now I'm just curious whether this is possible to control.


Postfix preserves the notation as @rasher pointed out. However, my intent was to allow convenient evaluation of sample code by being able to immediately evaluate the output—that is, ShiftEnter the foo//bar. When Postfix is the head of an expression though, the evaluation simply returns the expression—which, makes complete sense, since that's what Postfix... does. (This question almost seems circular now.)

Is there a way, perhaps, to create a custom PostfixToEvaluate head that would display the expression in postfix but, maybe via an UpValue, remove itself and evaluate its contents, upon evaluation? (I don't know if that makes any sense. I'm going to try it now...)

Tags I considered but don't believe are relevant: . Note that I've left "hold" in the title as that's the intuitive keyword by which I would have searched, were I a future visitor.

  • $\begingroup$ Am I missing something in the question? Postfix[foo//bar] will output foo//bar? $\endgroup$
    – ciao
    Mar 1, 2014 at 7:42
  • $\begingroup$ @rasher — Nope... that's it. Sorry, I viewed this as a notational issue more than a coding one, and didn't even imagine there'd be functions for manipulating notations, e.g. Postfix. Thank you. Would you post it as an answer? (Though, I expect this question to get closed or deleted.) It wasn't very obvious to me, at least. $\endgroup$ Mar 1, 2014 at 7:45
  • $\begingroup$ No worries, sure, I'll post it, and I'd be surprised if closed, it's in the somewhat obscure area... you ask interesting questions! $\endgroup$
    – ciao
    Mar 1, 2014 at 7:47
  • $\begingroup$ You could perhaps do as something like Interpretation["2//Sqrt//N", 2 // Sqrt // N]. This displays as the string, when shift-entered, it evaluates as expected. Downside is it leaves the original, but quotes it (might not be such a bad thing). You could, I suppose, add the notebook commands to go up one and delete that as part of eval... $\endgroup$
    – ciao
    Mar 1, 2014 at 9:07
  • $\begingroup$ Perhaps some progress... Interpretation[Style["2//Sqrt//N", ShowStringCharacters -> False], 2 // Sqrt // N] will show as the string, when evaluated, evaluates as expected, turns into typical bold MMA input form when done with no quotes, can be re-evaluated as often as wanted... $\endgroup$
    – ciao
    Mar 1, 2014 at 9:41

3 Answers 3


As requested:


will output


And as you noted in the comment reply, there are equivalents for infix and prefix. Format might also be of interest...

  • $\begingroup$ Hm, I encountered an issue, actually. (Sorry to unaccept!) My intent was to generate an output cell in postfix notation that would, conveniently, evaluate on Shift+Enter. Unfortunately, the Postfix head remains upon attempting to evaluate. I wonder how I can generate output in Postfix notation that would vanish the Postfix and evaluate its contents upon evaluation? $\endgroup$ Mar 1, 2014 at 8:08
  • $\begingroup$ No worry, your original question implied you were interested in the output only. I'll poke around when time permits. $\endgroup$
    – ciao
    Mar 1, 2014 at 8:22
  • $\begingroup$ Does Interpretation[Postfix[foo // bar], foo // bar] work? $\endgroup$ Mar 1, 2014 at 13:08
  • $\begingroup$ @SimonWoods: Only when foo/bar evaluate to themselves, hence by use of strings in my comment to OP. OP says they've got a working way, my curiosity has been whetted...Using Interpretation is pretty cool though, since I can display anything, valid or not, manipulate as I please, etc. $\endgroup$
    – ciao
    Mar 1, 2014 at 19:57

This may be unhelpful but motivate. Assuming the purpose is education you could use:

ntn[u_] := 
 FlipView[{Style[u, 20], 
   Row[{Style[ToExpression[u], 20]}, Background -> LightRed]}]

Examples (in gif):

enter image description here

  • $\begingroup$ Interesting! Unfortunately my actual expression involves input in TableForm (see comments on question), so I'm afraid a string representation may fail. Thank you for your input however. $\endgroup$ Mar 1, 2014 at 12:50
  • $\begingroup$ @acheong87 apologies. Perhaps some approach, e.g. you could useEventHandler or other dynamic approach to achieve your aim $\endgroup$
    – ubpdqn
    Mar 1, 2014 at 12:56

I'm not clear on what you're looking for here, but perhaps the following is close. First, I assume you don't want everything in postfix because you mention "in one case I feel that postfix notation is easier on the eyes". So, rather than creating a wrapper that makes everything postfix, I will instead modify the formatting of the function bar so that it is displayed in postfix:

bar /: MakeBoxes[bar[foo_], StandardForm] := RowBox[{MakeBoxes[foo], "//", "bar"}]

Let's also give bar a down value:

bar[x_] := x^2

Then, you can create a table using Defer:

table = TableForm[
    {Plus, a+b},
    {Subtract, a-b},
    {bar, Defer[a //bar]}
    TableHeadings->{"function", "example"}

enter image description here

Evaluating the above TableForm object yields:

{{Plus, a + b}, {Subtract, a - b}, {bar, a^2}}


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.