Simpler input for the new unit support

Here is a cheap way which does not involve WA, but will only be as good as you make it to be (so that you'd have to customize it yourself): create a dynamic environment:

ClearAll[withUnits];
SetAttributes[withUnits, HoldAll];
withUnits[code_] :=
  Function[Null,
     Block[{Quantity},
       SetAttributes[Quantity, HoldRest];
       Quantity /: UnitConvert[arg_, Quantity[_, unit_]] :=
          UnitConvert[arg, unit];
       Quantity /: Times[0, Quantity[_, unit_]] :=
          Quantity[0, unit];
       With[{
          m = Quantity[1, "Meters"], 
          s = Quantity[1, "Seconds"],       
          min =  Quantity[1, "Minutes"],
          km = Quantity[1, "Kilometers"]
        },
       #]],
    HoldAll][code];

So that

withUnits[UnitConvert[1 m/s^2*(1 min)^2,km]]

(*  18/5km  *)

You can set $Pre = withUnits, if you want to save some typing. The above function is a hack, of course, but it does dynamic code generation, uses Block trick and local UpValues, so I decided to post it still.


As noted in the documentation for Quantity, you can use ctrl-= to input units. This uses Wolfram|Alpha, so needs an internet connection.

enter image description here

Quantity will also use Wolfram|Alpha to try to interpret strings, so you could also use:

In[8]:= UnitConvert[Quantity["1 m/s^2*(1 min)^2"], Quantity["km"]]

Out[8]= Quantity[18/5, "Kilometers"]

You could set an input alias such as

With[{rules = {"m" -> "Meters", "km" -> "Kilometers"}},
 AppendTo[CurrentValue[InputNotebook[], InputAliases], 
  "qu" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, 
    "QuantityUnit", 
    DisplayFunction -> (PanelBox[RowBox[{##}], FrameMargins -> 2] &), 
    InterpretationFunction -> (ToBoxes@
        Quantity[ToExpression@#1, Evaluate[#2 /. rules]] &)]]]

Then, escquesc brings up a little panel with a couple of placeholders where you write, for example

Mathematica graphics

You could set the styles on a stylesheet, add units to your list of rules, or even not add any. If it doesn't recognize it, it queries WolframAlpha just like Quantity, and then caches the result. Setting the input alias to $FrontEnd instead of InputNotebook[] would make it global and permanent. Or you could add it to the Notebook style of some stylesheet instead

Tags:

Units