3
$\begingroup$

Here is code that I thought would make the front end parse f as BigFunction:

MakeExpression["f", StandardForm] := MakeExpression["BigFunction", StandardForm]

It works provided it is the only thing in the input:

f
(* BigFunction *)

But it doesn't work if it appears anywhere inside a larger expression:

f + 1
(* f + 1 *)    (* I want BigFunction + 1 *)

f[x]
(* f[x] *)     (* I want BigFunction[x] *)

How do I make single character expressions parse as longer names?


Note: Since I want the conversion to occur during parsing, I don't want to use Set:

f = BigFunction
$\endgroup$

1 Answer 1

2
$\begingroup$

In addition to your definition

MakeExpression["f", StandardForm] :=  MakeExpression["BigFunction", StandardForm]

you can use:

MakeExpression[RowBox[{x___, "f", y___}], StandardForm] := 
   MakeExpression[RowBox[{x, "BigFunction", y}], StandardForm]

and your examples will work. Add also:

MakeExpression[h_[x___, "f", y___], StandardForm] := 
 MakeExpression[h[x, "BigFunction", y], StandardForm]

and it should cover ***Box cases:

enter image description here

$\endgroup$
11
  • $\begingroup$ It works. But I'm worried the catch-all pattern might be quite inefficient for larger inputs. Do you have any thoughts on this, or have any alternatives in mind? Also, do you know if Mathematica uses the same catch-all pattern to parse π -> Pi? $\endgroup$
    – QuantumDot
    Oct 17, 2016 at 13:23
  • $\begingroup$ @QuantumDot yep, noticed that inconvenience, I will try to figure it out ;) $PreRead would be a quick fix but it is not what should be done. $\endgroup$
    – Kuba
    Oct 18, 2016 at 14:02
  • $\begingroup$ @QuantumDot is my edit enough? $\endgroup$
    – Kuba
    Oct 18, 2016 at 14:23
  • $\begingroup$ @QuantumDot I've talked with J.Harris at WTC about that, it seems that symbols like Pi are handled in a special way so those three rules seem inevitable. $\endgroup$
    – Kuba
    Nov 15, 2016 at 8:40
  • $\begingroup$ I couldn't get the third rule to work unless I use: MakeExpression[RowBox[{h_, "[", RowBox[{x___, "f", y___}], "]"}], StandardForm] := MakeExpression[RowBox[{h, "[", RowBox[{x, "BigFunction", y}], "]"}], StandardForm]. Is that what you meant? $\endgroup$
    – QuantumDot
    Jul 29, 2017 at 1:31

Your Answer

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

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