# Rational display with Quantity

Why is that Power[Quantity["ElectricConstant"],-1] is represent different of Power[Pi,-1] What I am doing wrong ?

• The technical "why" can be seen here: Power[Quantity["ElectricConstant"], -1] // FullForm. I'm unsure how to prevent this though. Aug 25, 2014 at 19:06

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:

CalculateScanUnitScannerPrivateUnitInformation

For example:

Quantity["ElectricConstant"]^-2 // FullForm
Quantity[1,Power["ElectricConstant",-2]]

Which in the course of box formatting eventually becomes a call like:

CalculateScanUnitScannerPrivateUnitInformation[
1,
QuantityUnitsPrivatetoAlphaUnit["ElectricConstant"^-2],
"SingularTest" -> (MatchQ[#1, 1] &),
"Output" -> {"PrefixBoxes", "PostfixBoxes", "UnitBoxes", "DefaultStyle"}
]
{"", RowBox[{"\"/\"", "\[InvisibleSpace]",
SubsuperscriptBox["\"\[CurlyEpsilon]\"", "0", "2"]}], "", Automatic}

Observe the RowBox with the "/". This is used in the higher level call:

QuantityUnitsPrivateQuantityAbbreviation[1, "ElectricConstant"^-1]
StyleBox[RowBox[{#1, "\[InvisibleSpace]",
StyleBox[RowBox[{"\"/\"", "\[InvisibleSpace]",
ShowStringCharacters -> False]

Which defines (part of) a formatting function for the expression, which can be observed with:

Quantity["ElectricConstant"]^-2 // ToBoxes
TemplateBox[{"1"}, "QuantityUnit",
DisplayFunction -> (TooltipBox[
StyleBox[RowBox[{#1, "\[InvisibleSpace]",
StyleBox[RowBox[{"\"/\"", "\[InvisibleSpace]",
SubsuperscriptBox["\"\[CurlyEpsilon]\"", "0", "2"]}],
"QuantityUnitTraditionalLabel"]}], ShowStringCharacters -> False],
"Unit: per electric constants squared"] &),
InterpretationFunction -> (RowBox[{"Quantity", "[",
RowBox[{#1, ",", FractionBox["1", SuperscriptBox["\"ElectricConstant\"", "2"]]}],
"]"}] &), SyntaxForm -> Mod]

As you can see this is all rather complicated. Perhaps later I will continue spelunking to trace the source of the formatting produced by CalculateScanUnitScannerPrivateUnitInformation but at the moment I am fatigued.

• It looks like the formatting for reciprocal units comes from ToPerBox and ToSlashBox in CalculateScanUnitScanner`Private. Aug 26, 2014 at 15:25

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 "reciprocal electric constants" is displayed as $1/\varepsilon _0$ just as units like "meters per second" are displayed as $m/s$ rather than the 2D form $\frac{m}{s}$

I am not aware of any simple method to change the way quantity units are formatted (though I would be delighted to be wrong). In principle you could modify the internal code for generating boxes for Quantity expressions but, as Mr Wizard has shown, the code is rather complex and it would not be easy.