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.

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.

share|improve this question
    
Am I missing something in the question? Postfix[foo//bar] will output foo//bar? –  rasher Mar 1 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 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! –  rasher Mar 1 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... –  rasher Mar 1 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... –  rasher Mar 1 at 9:41

2 Answers 2

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...

share|improve this answer
    
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 at 8:08
    
No worry, your original question implied you were interested in the output only. I'll poke around when time permits. –  rasher Mar 1 at 8:22
    
Does Interpretation[Postfix[foo // bar], foo // bar] work? –  Simon Woods Mar 1 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. –  rasher Mar 1 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

share|improve this answer
    
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 at 12:50
    
@acheong87 apologies. Perhaps some approach, e.g. you could use`EventHandler` or other dynamic approach to achieve your aim –  ubpdqn Mar 1 at 12:56

Your Answer

 
discard

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.