3
$\begingroup$

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]

enter image description here

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]

enter image description here

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]

enter image description here

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]

enter image description here

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.

$\endgroup$
  • $\begingroup$ Looking at your 2nd output from the top (with overrides), it seems that Grid calculates 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 find Alignment and Grid very 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). $\endgroup$ – MarcoB Sep 14 '15 at 15:14
  • $\begingroup$ @MarcoB Should this be marked as a bug? I'm thinking it should be but I'm not 100% certain. $\endgroup$ – Edmund Sep 14 '15 at 20:57
  • $\begingroup$ I am honestly not sure either. To the best of my understanding, and still referring to the second table you showed, this is not the behavior I would expect from the options you specified, so that would make it a bug in my view. I would appreciate some further insight from other members though. Failing that, I would suggest contacting WRI support to see what they have to say in the matter. $\endgroup$ – MarcoB Sep 14 '15 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.