Message Boards Message Boards

2 Replies
6 Total Likes
View groups...
Share this post:

Typesetting Units

Posted 11 years ago
The new support for units in v9 is welcome and seems nicely integrated.  The output of expressions with units seems nice but the input is very clunky, at best.  To input a value with units one types: Quantity[5,"Meters"]. I want to do this faster and with keyboard shortcuts.  It is possible to write this as 5 Quantity["Meters"], since Quantity["Meters"] is equivalent to Quantity[1,"Meters"] and units behave well under multiplication.  A keyboard shortcut for Quantity["Meters"] can then be created.  This KB shortcut I want to be "qm", meaning you type esc q m esc.  That is easy to do but what I want is something that is typeset as "m" while inputting.  This took quite a bit of playing but I have what seems to be a solution that I found by editing an Output cell with units: 

"qm" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"m\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: meters"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Meters\""}], "]"}]& )]

I am a bit over my head with this sort of typesetting; the TemplateBox[{},... ] seems wrong but it does work.  Note that it also has the mouseover ToolTip.  The length of the expression created some issues when adding the shortcut.  To add a shortcut go to Options Inspector.  It must be set to either "Global Preferences" or "Selected Notebook".  Under "Editing Options" look for "InputAliases".  Clicking on the icon gives the usual place to add these shortcuts easily. There is an issue here with the length of the replacement rule.  In my OS X implementation the text window to add the shortcut is too small and it truncates what is pasted into it.  The work-around is to add it explicitly to the List of replacement rules; be careful of positioning of commas and the curly brackets while doing this.  Clicking the "x" at the left of "InputAliases" restores the defaults.  If you have previous modifications made here, saving the contents to a text file before editing is safest.

I created such replacement rules for a somewhat full set of SI units and this led to another difficulty; the list of rules for InputAliases was too long to display in Options Inspector.  I had to create a large text file of my new rules added to existing default ones and then paste that to replace the InputAlias rules.  In doing this the list is there but disappears from the Options Inspector.  To modify more requires clicking the "x" to restore the defaults and then pasting the modified text of the list back in.

The text of all the rules, including the defaults, follows.  Note there is also the rule I find very useful: 

"eee" -> RowBox[{"\[Times]", SuperscriptBox["10", "\[SelectionPlaceholder]"]}]

Here is the full content (for plain text for easy copy-paste please follow this link): 

{"intt" -> RowBox[{"\[Integral]", RowBox[{"\[SelectionPlaceholder]", RowBox[{"\[DifferentialD]", "\[Placeholder]"}]}]}], "dintt" -> RowBox[{SubsuperscriptBox["\[Integral]", "\[SelectionPlaceholder]", "\[Placeholder]"], RowBox[{"\[Placeholder]", RowBox[{"\[DifferentialD]", "\[Placeholder]"}]}]}], "sumt" -> RowBox[{UnderoverscriptBox["\[Sum]", RowBox[{"\[SelectionPlaceholder]", "=", "\[Placeholder]"}], "\[Placeholder]"], "\[Placeholder]"}], "prodt" -> RowBox[{UnderoverscriptBox["\[Product]", RowBox[{"\[SelectionPlaceholder]", "=", "\[Placeholder]"}], "\[Placeholder]"], "\[Placeholder]"}], "dt" -> RowBox[{SubscriptBox["\[PartialD]", "\[Placeholder]"], " ", "\[SelectionPlaceholder]"}], "ia" -> TemplateBox[{"\[SelectionPlaceholder]"}, "Inactive"], "cbrt" -> RadicalBox["\[SelectionPlaceholder]", "3", SurdForm -> True, MultilineFunction -> None], "surd" -> RadicalBox["\[SelectionPlaceholder]", "\[Placeholder]", SurdForm -> True, MultilineFunction -> None], "ket" -> TemplateBox[{"\[SelectionPlaceholder]"}, "Ket"], "bra" -> TemplateBox[{"\[SelectionPlaceholder]"}, "Bra"], "braket" -> TemplateBox[{"\[SelectionPlaceholder]", "\[Placeholder]"}, "BraKet"], "curl" -> TemplateBox[{"\[Placeholder]", "\[SelectionPlaceholder]"}, "Curl"], "dive" -> TemplateBox[{"\[Placeholder]", "\[SelectionPlaceholder]"}, "Div"], "grad" -> TemplateBox[{"\[Placeholder]", "\[SelectionPlaceholder]"}, "Grad"], "lap" -> TemplateBox[{"\[Placeholder]", "\[SelectionPlaceholder]"}, "Laplacian"], "delay" -> TemplateBox[{"\[SelectionPlaceholder]"}, "SystemsModelDelay"], "eee" -> RowBox[{"\[Times]", SuperscriptBox["10", "\[SelectionPlaceholder]"]}], "qm" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"m\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: meters"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Meters\""}], "]"}]& )], "qs" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"s\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: seconds"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Seconds\""}], "]"}]& )], "qkg" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"kg\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: kilograms"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Kilograms\""}], "]"}]& )],"qj" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"J\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: joules"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Joules\""}], "]"}]& )],  "qn" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"N\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: newtons"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Newtons\""}], "]"}]& )],  "qo" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"\[CapitalOmega]\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: ohms"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Ohms\""}], "]"}]& )], "qc" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"C\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: coulombs"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Coulombs\""}], "]"}]& )], "qt" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"T\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: teslas"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Teslas\""}], "]"}]& )], "qv" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"V\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: volts"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Volts\""}], "]"}]& )], "qw" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"W\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: watts"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Watts\""}], "]"}]& )], "qpa" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"Pa\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: pascals"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Pascals\""}], "]"}]& )], "qa" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"A\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: amperes"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Amperes\""}], "]"}]& )], "qk" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"K\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: kelvins"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Kelvins\""}], "]"}]& )], "qf" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"F\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: farads"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Farads\""}], "]"}]& )], "qh" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"H\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: henries"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Henries\""}], "]"}]& )], "qhz" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"Hz\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: hertz"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"Hertz\""}], "]"}]& )], "qdc" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"\[Degree]C\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: degrees Celsius"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"DegreesCelsius\""}], "]"}]& )], "qdf" -> TemplateBox[{},"QuantityUnit", DisplayFunction->(TooltipBox[StyleBox[RowBox[{StyleBox["\"\[Degree]F\"", "QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False], "Unit: degrees Fahrenheit"]& ), InterpretationFunction->(RowBox[{"Quantity", "[", RowBox[{"\"DegreesFahrenheit\""}], "]"}]& )]}
POSTED BY: Terrence Honan
2 Replies
This is a completely different approach, but to speed up input of units I use simple shortcut CTRL + = . This allows me to use free-from input to write units inside formulas in a quite arbitrary convenient for me form. Mathematica will parse them in a correct unit form. For example:

You can see another example from Stephen Wolfram blog below or find a few more following this link. Would this help you anyhow to address your problem?

POSTED BY: Vitaliy Kaurov
What I am looking for is something that looks cleaner.  In the classroom I use Mathematica as a calculator with the results projected for the students.  My goal is not just the ease of use for myself but the clarity of the presentation for students.  The CTRL + = approach is nice in that it allows you to discover units without knowing the appropriate input; it doesn't look nearly as clean as my suggestion and would be a distraction for students when used in a classroom presentation.  I also feel my approach is much more efficient for everyday use.

I would prefer to see Wolfram adopt my suggested keyboard shortcut approach at a system level.  Also a palette containing a large collection of units combined with mouseovers displaying the keyboard shortcuts would be ideal.
POSTED BY: Terrence Honan
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract