Tag Info

Hot answers tagged

9

There are several ways to do that. Perhaps, the simplest is to do something like this: ClearAll[color]; SetAttributes[color, HoldAll]; color[_Export | _Import] := LightGreen; color[_Set] := LightBlue; color[_] := None; and then $Pre = Function[ code , SetOptions[ EvaluationCell[], Background -> color[code]]; code , ...


8

The following works for V10. First we define some abbreviation rules: rule = {"Newtons" :> N, "Meters" :> m, "Pascals" :> Pa, "Farads" :> F}; (* add more rules here *) Then: unit = TextString[QuantityUnit[Quantity[1, "Newtons/Meters^2"]] /. rule] "N/m^2" StringQ[unit] True TextString[QuantityUnit[Quantity[1, ...


4

stringUnits = Module[{qf = QuantityForm[QuantityUnit[#], "Abbreviation"], boxes}, boxes = Cases[ToBoxes[qf], StyleBox[RowBox[{x___}], sty_] :> x]; StringJoin[ boxes /. x_String :> ToExpression[x] /. Null -> " " /. SuperscriptBox[a_, b_] :> StringJoin[ToString@a, "^", ToString@b]]] &; stringUnits @ Quantity["Newton/Meters^2"] (* ...


2

You aren't doing anything wrong, it's just that Mathematica uses a linear form for the units in Quantity expressions. As mfvonh commented, the expression Power[Quantity["ElectricConstant"], -1] is evaluated to Quantity[1, Power["ElectricConstant", -1]] i.e. a Quantity with a magnitude of 1 and units of "reciprocal electric constants". The unit ...


2

This is not yet a real answer as I offer no solution, only observations. Attempting to answer this by spelunking Quantity formatting is like falling down the rabbit hole. One internal function leads to another ad infinitum. I shall stop for now with the observation that the Box formatting comes from this function: ...



Only top voted, non community-wiki answers of a minimum length are eligible