# Is there a way to “hold” prefix / infix / postfix notation?

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.

## Edit

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.

• Am I missing something in the question? Postfix[foo//bar] will output foo//bar? – ciao Mar 1 '14 at 7:42
• @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. – Andrew Cheong Mar 1 '14 at 7:45
• 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! – ciao Mar 1 '14 at 7:47
• 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... – ciao Mar 1 '14 at 9:07
• 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... – ciao Mar 1 '14 at 9:41

As requested:

Postfix[foo//bar]


will output

foo//bar


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

• 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? – Andrew Cheong Mar 1 '14 at 8:08
• No worry, your original question implied you were interested in the output only. I'll poke around when time permits. – ciao Mar 1 '14 at 8:22
• Does Interpretation[Postfix[foo // bar], foo // bar] work? – Simon Woods Mar 1 '14 at 13:08
• @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. – ciao Mar 1 '14 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):

• 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. – Andrew Cheong Mar 1 '14 at 12:50
• @acheong87 apologies. Perhaps some approach, e.g. you could useEventHandler or other dynamic approach to achieve your aim – ubpdqn Mar 1 '14 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]}
},

Evaluating the above TableForm object yields: