3
$\begingroup$

I have a table of real numbers and want to format a Grid such that the headlines are centered and the contents are aligned at the decimal point. MWE:

data = Table[RandomReal[{1, 100}], {i, 1, 5}, {j, 2}]

headers:

columns = {"one", "two"};

Rows:

rowsLabels = StringJoin["R", ToString[#]] & /@ Range[5]
rows = Flatten /@ ({rowsLabels, data}\[Transpose])

Table:

tab = Join[{Prepend[columns, "Text "]}, rows]

Then

Grid[tab, 
 Dividers -> {{2 -> True}, {2 -> True}},
 Alignment -> {{Left, Center, Center}, {{Left, ".", "."}}, Baseline}]

delivers:

enter image description here

and

Grid[tab, 
 Dividers -> {{2 -> True}, {2 -> True}},
 Alignment -> {{{Left, Center, Center}, {Left, ".", "."}}, Baseline}]

delivers:

enter image description here

So the second columns is fine, but the first not. Can anyone give me a hint on how I can achieve the desired result.

$\endgroup$
0

1 Answer 1

4
$\begingroup$

It looks like using a list specification doesn't work for this. Instead, you can use the 3 element list form of Alignment:

Grid[tab, Alignment->{{Left,".","."},Baseline, {{1,1},{1,3}}->Center}]

enter image description here

Alternatively, you could use Item to override the headers:

Grid[
    MapAt[Item[#, Alignment->Center]&, tab, {1, All}],
    Alignment->{{Left, ".", "."}, Baseline}
]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank your for this solution, the second one is the one which is very clear to me (one rule - override the headers). $\endgroup$
    – mgamer
    Commented May 10, 2018 at 19:05
  • $\begingroup$ It seems to me that the documentation you point to for the three-argument form suggests the 3rd argument would be {{1, 1} -> Center, {1, 2} -> Center, {1, 3} -> Center}}. Should I be able to deduce from this that {{1, 1}, {1, 3}} -> Center} will work? If not, what is the actual documentation? $\endgroup$
    – Alan
    Commented May 10, 2018 at 21:30
  • 1
    $\begingroup$ @Alan The Alignment docs are a bit sparse on this topic. If you look at one of the other options, e.g., Frame, you will see an example of the syntax I used. $\endgroup$
    – Carl Woll
    Commented May 10, 2018 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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