4
$\begingroup$

Further to the problem noted in Grid - Item - Alignment to decimal problem I feel that the Alignment below should be decimal for the lower right and centre for the left and top headings. You will note that the top headings have also been decimal aligned.

heads = "head_" <> ToString[#] & /@ Range[4];
cols = "col_" <> ToString[#] & /@ Range[4];
data = Join[{Join[{"Table Name"}, heads]}, Join[{#},
             RandomReal[{-5, 20}, 4]] & /@ cols];

Grid[data, Frame -> All, FrameStyle -> Directive[Thin, Gray],
     Background -> {Yellow, 1 -> LightGray, 1 -> LightGray},
     Alignment ->  {".",    1 -> Center,    1 -> Center.  }]

You will note that the Alignment entries mimic the Background entries but the result is not satisfactory. The "head_1" is not center aligned but has also been aligned on the decimal point.

enter image description here

I have been searching for a solution to this without success and so a solution would be most welcome.

$\endgroup$
4
$\begingroup$

You can wrap row and column headers with Item and use the option Alignment -> Center:

heads = Item["head_" <> ToString[#], Alignment -> Center] & /@ Range[4];
cols = Item["col_" <> ToString[#], Alignment -> Center] & /@ Range[4];
data = Join[{Join[{"Table Name"}, heads]}, Join[{#}, RandomReal[{-5, 20}, 4]] & /@ cols];

Grid[data, Frame -> All, FrameStyle -> Directive[Thin, Gray], 
 Background -> {Yellow, 1 -> LightGray, 1 -> LightGray}, 
 Alignment -> {"."}]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks you very much. I was exploring Item for use in colourising the data. It didn't occur to me that I could use that for overriding alignment as well. Although this works for alignment of the headings, you may notice when studying the picture that the column widths and positioning of the numbers are not quite right. $\endgroup$ – NigelKg Aug 19 '18 at 8:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.