How to set the font color of quantity objects (Version 11.3 vs version 12)?
Looks like a bug to me:
In version 12.0, the typesetting of Quantity
objects in TraditionalForm
(which is used for legends in Legended
expressions) breaks after typesetting a quantity with the same unit in StandardForm
. To see this, consider the following example: (use a fresh kernel)
TraditionalForm@Quantity[3.5, "Percent"]
Quantity[3.5, "Percent"]
TraditionalForm@Quantity[3.5, "Percent"]
TraditionalForm@Quantity[1, "Percent"]
As you can see, quantities are typeset differently in TraditionalForm
, and the one for TraditionalForm
changes after typesetting in StandardForm
once. The case of 1
is handled differently for some reason and does not break.
In 11.3, the typesetting of the TraditionalForm
variant does not break after typesetting the StandardForm
variant. This is why your example works in 11.3: The style for TraditionalForm
does not set the FontColor
, while the StandardForm
one does (see CurrentValue[{StyleDefinitions, "QuantityPanel"}]
, which is only used for StandardForm
). That this works seems like a lucky coincidence, so I would suggest to use the method from @C.E.'s answer anyway, even if this gets fixed.
Tracking down the issue
The issue is caused by a change to the caching used for quantity typesetting:
(* cache lookup in 11.3 typesetting code for Quantity *)
QuantityUnits`QuantityBox[
Quantity[n_, unit_?QuantityUnits`Private`UnitDisplayCacheContainsQ], form:StandardForm]
] :=
Quiet[QuantityUnits`Private`getUnitDisplayForm[unit, n, form], {Part::partw}]
(* cache lookup in 12.0 typesetting code for Quantity *)
QuantityUnits`QuantityBox[
Quantity[n_, unit_?QuantityUnits`Private`UnitDisplayCacheContainsQ], form_:StandardForm]
] :=
Quiet[QuantityUnits`Private`getUnitDisplayForm[unit, n, form], {Part::partw}]
Note the change from form:StandardForm
to form_:StandardForm
- this extends the caching from StandardForm
to all forms. The reason I think this is a bug is that this is the only definition of QuantityBox
that defaults form
to anything (all the others require an explicit form to be given), which suggests someone "fixed" a typo.
The issue with the caching code is that it ignores form
when performing the cache lookup. Writing to the cache still requires StandardForm
(here, the form:StandardForm
pattern is still present in 12.0). The code was already questionable in 11.3 (since the caching partially supports forms other than StandardForm
), but this was not an issue since it was only ever called with StandardForm
.
To be fully in control of the styling, I would suggest converting the quantity to a number.
I suggest replacing
Row[{"(", #, , ")"}]
with
Row[{"(", QuantityMagnitude[#], "%)"}]