4

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.

Community
  • 1
  • 1
Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93

3 Answers3

5

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

Alexey Popkov
  • 9,355
  • 4
  • 42
  • 93
  • Somehow I did not see this answer before I replied. Nevertheless, I think I show a good general way to make specific alignments in a table. – Mr.Wizard Aug 17 '11 at 16:00
3

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}

Heike
  • 24,102
  • 2
  • 31
  • 45
  • You have achieved something opposite to what is stated in the question: I need not to modify the alignment of the first element in the table but the alignment of all others elements in the first row and the first column. – Alexey Popkov Aug 17 '11 at 14:23
  • 1
    @Alexey: Setting `{{1,1}->None}` instead of `{{1,1}->{Right, Top}` should work. – Heike Aug 17 '11 at 14:32
  • Again, it is not what I want. But just now I have solved the problem myself. Thank you in any case. – Alexey Popkov Aug 17 '11 at 15:32
3

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

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • +1 for elegant use of `ReplacePart`. Another way is to use `Part`: `x[[2;;-1,1]]=Item[#,Alignment->Left]&/@x[[2;;-1,1]];x[[1,2;;-1]]=Item[#,Alignment->Center]&/@x[[1,2;;-1]];Grid[x,Alignment->Right]`. – Alexey Popkov Aug 17 '11 at 16:20
  • Thanks. In this case I prefer the `SparseArray` style index pattern that is possible with `ReplacePart`. – Mr.Wizard Aug 17 '11 at 16:26
  • Your solution with `ReplacePart` is more appropriate than more obvious with `Part` since it does not require creation of temporary variable for storing of the original matrix. – Alexey Popkov Aug 17 '11 at 16:33