Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am interested in making a simple accounting style line calculator that allows me to interact with it something like this:

In[]:   4 + 5
Out[]:  9
In[]:   +3
Out[]:  12
In[]:   *2
Out[]:  24
In[]:   /4
Out[]:  6

In the front end, I can do this with:

$PreRead = (ReplaceAll[#, {"*" :> "% *", "+" :> "% +", "-" :> "% - ", 
 "/" :> "% /"}] &)

(And stop it by $PreRead =.) which works nicely, however I would like to use the numberpad, and the + key on the number pad starts a free form input.

So, I thought I'd be sneaky and open up a kernel window. Sadly, the above assignment of $PreRead doesn't seem to execute in the Kernel, even though it looks like the variable has been assigned properly.

I'm interested in why $PreRead isn't behaving the same in the Kernel window as it would in the FrontEnd, and it wouldn't hurt to know if it is possible to shut off the keymapping of the numberpad + key to free form input.

share|improve this question
$PreRead does not seem to work in a kernel window on mac, on versions 9.0.1 and 10.3. I guess it never worked in versions in between. – Jacob Akkerboom yesterday

2 Answers 2

My definition of $Pre does not work as intended. I will edit later. See also edits of this answer. I originally intended a solution similar to that in Kuba's answer. I got confused because $PreRead does not do anything for me in a kernel window, which also seems to be the case for the OP, but not for Kuba.

The kernel does not deal with boxes. In the front end $PreRead is applied to the boxes corresponding to the line (cell) that is evaluating. In the kernel there are no boxes, the inputted command is regarded as a string. $PreRead does not seem to do anything in a kernel window.

You can use $Pre instead. Possibly it is more convenient to parse strings, though here I will present a solution that is as close as possible to your original solution.

Note that your definition of $PreRead does not work as you report

$PreRead = (ReplaceAll[#, {"*" :> "% *", "+" :> "% +", "-" :> "% - ", 
     "/" :> "% /"}] &)
4 + 5
5+4 %3

I think the following is a correct definition.

myPreRead =
    {op : "*" | "+" | "-" | "/", sq___} :> {"%", op, sq}
    ] &;

Here is a definition of $Pre that let's myPreRead do the work all the work.

myPre =
  ToExpression[#, StandardForm] &@


% + 1
share|improve this answer

This doesn't seem to work on Mac

You can check with $PreRead = Echo (or Print in case of V<10.3) that +1 returns RowBox[{+,1}] in FrontEnd and "+1" in Kernel.

So the solution is to use:

$PreRead = StringReplace[#, StartOfString ~~ op : "*" | "+" | "-" | "/" :> "%" <> op] &

enter image description here

share|improve this answer
Hmm, for me setting a value to $PreRead does nothing in the kernel. – Jacob Akkerboom 2 days ago
@JacobAkkerboom why you people are still using Macs? :P – Kuba yesterday
I would switch to windows, but the Timing is bad ;) – Jacob Akkerboom yesterday
@JacobAkkerboom I suppose that withput $PreRead you need to combine $Pre and $SyntaxHandler. Does $SyntaxHandler work for you e.g. when you type *1? – Kuba yesterday
ah yes, I had already checked $SyntaxHandler. That one does work. So you mean to cause a syntax error using $Pre? Ingenious, but quite hacky too :). – Jacob Akkerboom yesterday

Your Answer


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.