I would like to use Grid to display the results of a series of calculations and an information graphic on many items. The grid has a name and a category column with a few numbers following and then the info graphic. I am having problems with the Alignment option when overriding the alignments of the first row from that of the main grid using the rules specification available to Grid with Alignment.
(* Example data *)
img = Graphics[{Blue, Rectangle[{0, 0}, {1, .25}]}, ImageSize -> 80];
dat = {
{"Name", "Cat", "Big Num Value1", "Num Value2", "Image Col"},
{"A name of something", "Cat Apples", 2341230978.2, 30.1, img},
{"A name of something else", "Cat Oranges", 234135.34, 150.4, img}
};
When I first displayed this in Grid with the column alignments I notice that the header bar was aligning to the end of the string.
Grid[dat,
Alignment -> {{Left, Left, Sequence @@ ConstantArray[".", 2], Center}, Baseline},
Frame -> All]
I tried to fix this by adding alignment rules for header the columns that hand numbers in them.
Grid[dat,
Alignment -> {{Left, Left, Sequence @@ ConstantArray[".", 2],
Center}, Baseline,
{1, #} -> {Center, Baseline} & /@ Range[3, 4]
},
Frame -> All]
This sort of worked. It has aligned the header column to centre but has not resized the column width. Instead it is leaving tons of empty space on the right-hand side.
Things improve if AccountingForm is used on the numbers.
Function[{col},
dat[[2 ;;, col]] =
AccountingForm[#, DigitBlock -> 3] & /@ dat[[2 ;;, col]]] /@ {3, 4};
Grid[dat,
Alignment -> {{Left, Left, Sequence @@ ConstantArray[".", 2], Center}, Baseline},
Frame -> All]
However, the alignment overrides are still needed.
Grid[dat,
Alignment -> {{Left, Left, Sequence @@ ConstantArray[".", 2],
Center}, Baseline,
{1, #} -> {Center, Baseline} & /@ Range[3, 4]
},
Frame -> All]
Even with this adjustment the issue is still there that the column widths with the numbers are not resizing narrower when the alignment override is added.
What exactly is happening here? Can I get the grid without use of AccountingForm to size the width appropriately?
I am using Grid because it has more formatting and layout options than other table-type layouts. For example, I'm using its Background option for a row shading pattern.




Gridcalculates column widths first, using global alignment settings and ignoring any overrides. It then applies the overrides, "cosmetically" so to speak, but does not reconsider the column widths accordingly. I findAlignmentandGridvery time-consuming because of unexpected behavior such as this, which to me is bug-like. Here are a few examples of similar buggy behavior that needed workarounds: (8923), (13447). – MarcoB Sep 14 '15 at 15:14