How to set the font color of quantity objects (Version 11.3 vs version 12)?

TD = 8 + 12 + 12 + 12 + 33 + (61 + 1/4) + (41 + 1/4)/2 +
15/2 + (18 + 3/4) + (18 + 3/4)/2 + 14/2;
CM = 15/2 + (61 + 1/4) + (41 + 1/4)/2 +
24 + (3 + 3/4) + (18 + 3/4) + (18 + 3/4)/2 + 14/2 + (24 + 3/4) +
9/2;
TP = 2*90;

countsDim1 = {CM, TD, TP}/(TD + TP + CM)

n = 384 + 179;
labels = Placed[
Row /@ Thread[{"" <> ToString@# <>
" H" & /@ {N[(n countsDim1[[1]]), 5],
N[(n countsDim1[[2]]), 5], Round@N[(n countsDim1)[[3]]]}}],
legend = Placed[
SwatchLegend[Automatic,
Row /@ Thread[{{"CM ", "TD ", "TP "},
Row[{"(", #, , ")"}] & /@
Quantity[Round[100 countsDim1], "Percent"]}],
LabelStyle -> {FontSize -> 26, FontFamily -> "Latin Modern Roman",
FontColor -> Black, FontWeight -> Bold},
LegendMarkerSize -> 26], {After, Bottom}, Framed];

type = Framed[
PieChart[countsDim1, ChartLabels -> labels, ChartLegends -> legend,
PlotRange -> All,
LabelStyle -> {FontSize -> 26, FontFamily -> "Latin Modern Roman",
FontColor -> White, FontWeight -> Bold}, PlotTheme -> "Business",
ImageSize -> Medium], Background -> LightGreen]


In Mathematica 11.3 above code would produce a Pie Chart with the percentage of the Legend black. In Mathematica 12, I get the text of the percentage with gray color.

Below the same figure with Mathematica 11.3 (albeit without the bold text of the LegendLabel)

What is different in Mathematica 12?

How can I get the percentage in black color (and bold)?

To be fully in control of the styling, I would suggest converting the quantity to a number.

I suggest replacing

Row[{"(", #, , ")"}]


with

Row[{"(", QuantityMagnitude[#], "%)"}]

• Or eliminate Quantity: Row[{"(", #, , "%)"}] & /@ Round[100 countsDim1] – Bob Hanlon Apr 28 '19 at 19:28

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"]


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 *)
QuantityUnitsQuantityBox[
Quantity[n_, unit_?QuantityUnitsPrivateUnitDisplayCacheContainsQ], form:StandardForm]
] :=
Quiet[QuantityUnitsPrivategetUnitDisplayForm[unit, n, form], {Part::partw}]

(* cache lookup in 12.0 typesetting code for Quantity *)
QuantityUnitsQuantityBox[
Quantity[n_, unit_?QuantityUnitsPrivateUnitDisplayCacheContainsQ], form_:StandardForm]
] :=
Quiet[QuantityUnitsPrivategetUnitDisplayForm[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.

• Should I change the title of the question since it is misleading? – Dimitris Apr 28 '19 at 13:54
• Well, it describes the original issue you were having - you could change it to "How to set the font color of quantity objects" to make it a bit more descriptive. This answer was more intended as an explanation of what changed, rather than as a direct solution to your problem – Lukas Lang Apr 28 '19 at 14:06