Message Boards Message Boards

GROUPS:

Define InputAlias shortcuts for quickly entering nicely formatted units?

Posted 5 months ago
858 Views
|
4 Replies
|
6 Total Likes
|

Ref the attached notebook, I'd like to be able to define InputAlias shortcuts for quickly entering nicely formatted units. The desired format is shown at Section 3 in the attached notebook. My approach at present is to use the InputAlias values defined at Section 1. This produces input in the format shown at Section 2. I then evaluate the Quantity[] parts of these expressions in place (in the input cells) to produce nicely formatted units shown at Section 3.

My question is whether there's a way to evaluate the Quantity[] function within the AddInputAlias automatically when the InputAlias is invoked so that the nicely formatted form of units are shown at Section 3 is displayed straight off without having to evaluate in place? This would save a lot of time.

Thanks in anticipation,

Ian

Attachments:
4 Replies

Ian, would you settle for having a palette to paste in the units in a Mathematica expression? Or being able to quickly generate a custom palette that has the units for a particular problem type?

I have an application called UnitsHelper. It's a package, with an advanced and basic palette and a custom style sheet that displays the units in black instead of grey. (It just seems to me that it's important information and should be displayed as all important information. But you don't have to use the style sheet or could change it to your preference.) There are also quick links to Wolfram and NIST documentation. The advanced palette displays all units by group and then either alphabetically or by size. Physical constants are also on the palette. There is a facility for using reduced units such as geometric or atomic units and also a facility to deunitize an expression to use implied input and output units. Also facilities for general decibel units.

If you're interested send me an email from my profile page and I'll send you a Dropbox link.

Thanks David. I’ve sent you an email. I have a palette for units, but it’s a bit slow to use and want to establish whether it’s possible to achieve the same thing using just keyboard input. I’ve also figured out how to customise the QuantityPanel style to ditch the odd choice of a greyed out font colour for unitiized values. Look forward to seeing your UnitsHelper package.

All the best,

Ian

For your question regarding section 3, you can use an EventHandler so that the boxes are evaluated when a particular key is pressed. For example:

CurrentValue[ SelectedNotebook[ ], { InputAliases, "qty" } ] = ToBoxes @
  EventHandler[
      Defer @ Quantity @ SelectionPlaceholder[ ],
      {
          "RightArrowKeyDown" :>
            Module[ { box, input, new },

                box = EvaluationBox[ ];
                input = First @ NotebookRead @ box;

                new = Quiet @ Check[ ToExpression[ input,
                                                   StandardForm,
                                                   ToBoxes
                                     ],
                                     input
                              ];

                NotebookWrite[ box, new ]
            ]
      }
  ]

With this, you can use the input alias qty to generate a Quantity template, but pressing the right arrow key will automatically evaluate the quantity in place (see attached video).

Edit: DynamicModule wasn't necessary

Hi Richard,

Thanks very much for your suggestion. You'd code does almost exactly what I'm looking for. I've taken the liberty of making a couple of minor modifications which (a) make the implementation a bit quicker to use, and (b) provide two alternative display forms. A notebook containing my modifications is attached. I've not carried out any rigorous testing yet, but, based on initial trials, the approach appears to work exactly as required.

Ultimately, it would, I guess, be easy to add more InputAliases for commonly used units. What might be neat solution would be a facility to enter esc-"Canonical Unit Name"-esc followed by the right arrow. So, for example, esc-"Meters"-esc (preferably without the "") would produce m in the input cell rather than the less conventional and horribly long-winded form Quantity[None,"Meters"].

I think you've solved a problem that's been causing me, and no doubt many other users, a real headache for some time and would suggest that this, or a similar, type of approach to units is built in to the system.

Thanks again for your input, its very much appreciated.

Ian

Attachments:
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract