Alexey Popkov
Alexey Popkov

Reputation: 9425

How to align first column and first row in Grid independently from others?

How to specify separate alignments for the first column (excluding first row in this column), first row (excluding first element in this row) and all other elements in the Grid? It is preferable to do this just with Alignment option of Grid preserving tight control given by Item[] with Alignment option for more tight purposes.

P.S. This question comes from previous question but I wish to get individual control over alignments of horizontal and vertical table headings here.

Upvotes: 4

Views: 1458

Answers (3)

Mr.Wizard
Mr.Wizard

Reputation: 24336

If I understand your requirements, I would favor doing this with Item as follows:

x = Array[\[HappySmiley] &, {5, 5}];

x = ReplacePart[x, 
      i : Except[{1, 1}, {_, 1} | {1, _}] :> 
        Item[x~Extract~i, Alignment -> Left]
    ];

Grid[x, ItemSize -> {3, 3}, Frame -> All]

enter image description here

Upvotes: 3

Alexey Popkov
Alexey Popkov

Reputation: 9425

I have found several ways to achieve what I want. The most direct solution is:

Grid[Table[Row@(Range[a]), {a, 1, 4}, {7}], 
 Alignment -> {Right, 
   Automatic, {{{2, -1}, {1, 1}} -> Left, {{1, 1}, {2, -1}} -> 
     Center}}, Dividers -> {{2 -> True}, {2 -> True}}]

enter image description here

Other solutions include:

Grid[Table[Row@Range[a], {a, 1, 4}, {7}], 
 Alignment -> {{Left, {Right}}, 
   Automatic, {{1, 1}, {1, -1}} -> Center}, 
 Dividers -> {{2 -> True}, {2 -> True}}]
Grid[Table[Row@Range[a], {a, 1, 4}, {7}], 
 Alignment -> {Right, 
   Automatic, {1 -> Left, {{1, 1}, {2, -1}} -> Center}}, 
 Dividers -> {{2 -> True}, {2 -> True}}]
Grid[Table[Row@Range[a], {a, 1, 4}, {7}], 
 Alignment -> {Right, 
   Automatic, {1 -> Left, {{1, 1}, {1, -1}} -> Center}}, 
 Dividers -> {{2 -> True}, {2 -> True}}]
Grid[Table[Row@Range[a], {a, 1, 4}, {7}], 
 Alignment -> {Right, 
   Automatic, {{{1, 1}, {1, -1}} -> Center, 1 -> Left}}, 
 Dividers -> {{2 -> True}, {2 -> True}}]

enter image description here

Upvotes: 5

Heike
Heike

Reputation: 24420

It looks like Alignment uses the same syntax as the Background in Grid so it might help to look at Options > Background in the documentation for Grid for examples.

For example, suppose you want to align the item in the first row and first column top-right and all other items bottom-left, you could do something like

Grid[RandomInteger[10, {5, 5}], ItemSize -> {3, 3}, Frame -> All, 
 Alignment -> {Left, Bottom, {{1, 1} -> {Right, Top}}}]

grid with different alignment for item {1,1}

Upvotes: 3

Related Questions