I am implementing pseudocode in Mathematica for a class I'm teaching. I would like to copy the textbook syntax which is IF ... THEN ... ELSE ... ENDIF. My results are not very satisfactory and my question is whether there is a way of overcoming the problems I've met.

I have used the Notation package as follows:

Mathematica graphics

now this is fine as far as it goes, but my first problem is that the precedence structure it creates means that compound arguments x, y and z have to be enclosed in brackets or the "keywords" are treated as multipliers:

IF (3 < 5) THEN a ELSE b ENDIF (* parses ok: returns a *)
IF  3 < 5  THEN a ELSE b ENDIF (* does not parse ok *)

output: Mathematica graphics

a related issue is that I cannot split expressions over multiple lines without introducing bracketed expressions and putting the newlines within the brackets:

complicatedExpressionInvolvingReturns = 99998;
anotherComplicatedExpressionInvolvingReturns = Total;
IF (
   >= 99999
  ) THEN (
   {y1, y2, y3, y4}]
  ) ELSE (

   {y4, y3, y2, y1}]

my second issue is that I would like If[a,b,c] to print as IF a THEN b ELSE c ENDIF but I have not found a way of using Notation to do this.

Is there a better way of doing this, perhaps without using the Notation package?

  • $\begingroup$ I don't know what I changed, but now If[a,b,c] is now printed in the form I prefer $\endgroup$
    – fairflow
    Commented Sep 12, 2016 at 11:08
  • $\begingroup$ Precedence of operators is hard coded and I don't know any way to hack it. Front End is creating box expressions, according to built-in precedence, while code is typed. You can "intercept" boxes, before they are converted to expression, using $PreRead, but writing robust parser that would change built-in precedence, by untangling all possible box structures, seems to be an enormous work. $\endgroup$
    – jkuczm
    Commented Sep 19, 2016 at 13:25

1 Answer 1


Inspired by my answer to Seeking more verbose syntax, e.g. "Then" -> ",", I thought I would try a similar approach for this question. So, define the following aliases:

CurrentValue[EvaluationNotebook[], {InputAutoReplacements, "if"}] = RowBox[{
        DisplayFunction->(StyleBox["IF", ShowAutoStyles->False, FontColor->GrayLevel[.5]]&),
    StyleBox["\[NegativeMediumSpace]", ShowContents->True]  
CurrentValue[EvaluationNotebook[], {InputAutoReplacements, "then"}] = TemplateBox[
    DisplayFunction->(StyleBox["THEN", ShowAutoStyles->False, FontColor->GrayLevel[.5]]&),
CurrentValue[EvaluationNotebook[], {InputAutoReplacements, "else"}] = TemplateBox[
    DisplayFunction->(StyleBox["ELSE", ShowAutoStyles->False, FontColor->GrayLevel[.5]]&),
CurrentValue[EvaluationNotebook[], {InputAutoReplacements, "endif"}] = TemplateBox[
    DisplayFunction->(StyleBox["ENDIF", ShowAutoStyles->False, FontColor->GrayLevel[.5]]&),

I fixed some issues with my initial answer, and I changed to input auto replacements a la Chip Hurt's answer to the linked question. It should be much more robust. An animation showing it in action:

enter image description here

  • $\begingroup$ nice. It should be possible to combine with a Notation-style output format that matches the input. $\endgroup$
    – fairflow
    Commented Jul 4, 2019 at 18:02

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.