4
$\begingroup$

I generate different Grids with different columns. Automatically ImageSizes are fixed depending upon the data. So every Grid has a different size.

grid1=Grid[{{Item["Name", Frame -> True, Background -> LightBlue, Alignment -> Left], 
             Item["Branch", Frame -> True, Background -> LightBlue,Alignment -> Left], 
             Item["Section", Frame -> True, Background -> LightBlue,Alignment -> Left]},
            {"s", "IT", "A"}, 
            {"u", "IT", "A"}, 
            {"b","IT", "A"}, 
            {"b", "IT", "A"},
            {"b", "IT", "A"}}, Frame -> True
         ];

 grid2=Grid[{{Item["Name", Frame -> True, Background -> LightBlue, Alignment -> Left], 
             Item["Branch", Frame -> True, Background -> LightBlue,Alignment -> Left], 
             Item["Section", Frame -> True, Background -> LightBlue,Alignment -> Left]},
            {"subrahmanyam", "IT", "A"}, 
            {"Madhu", "IT", "A"}, 
            {"Kiran","IT", "A"}, 
            {"KalyanKumar", "IT", "A"},
            {"Pavan", "IT", "A"}}, Frame -> True
         ];
  grid3 = Grid[{{"Main", SpanFromLeft, SpanFromLeft, 
 SpanFromLeft}, {Item["Name", Frame -> True, 
  Background -> LightBlue, Alignment -> Left], 
 Item["Branch", Frame -> True, Background -> LightBlue, 
  Alignment -> Left], 
 Item["Section", Frame -> True, Background -> LightBlue, 
  Alignment -> Left], 
 Item["Section", Frame -> True, Background -> LightBlue, 
  Alignment -> Left]}, {"subrahmanyam", "IT", "A", "g"}, {"Madhu",
  "IT", "A"}, {"Kiran", "IT", "A"}, {"KalyanKumar", "IT", 
 "A"}, {"Pavan", "IT", "A"}}, Frame -> True, 
  ItemSize -> {{10, 7, 7, 7}, 1}];

 finalGrid=Grid[{{grid3},{grid1}, {grid2}, {grid1}, {grid2}, {grid1}, {grid2}}]

grid1Image=

enter image description here

grid2Image=

enter image description here

grid3Image=

enter image description here

finalImage=

enter image description here

If I place all the Grids columnwise within Grid, it doesn't look good.

So, I want to fix all the Grids with the same sizes. I tried various things, but nothing seems to work.

Can anybody help me?

$\endgroup$

1 Answer 1

7
$\begingroup$

Update 3: Dealing with subgrids with different number of columns:

The main difficulty with differing number of columns is that both Spacings and ItemSizes in the subgrids have to be taken into account to get the same total width for all the subgrids. With $m$ subgrids indexed $i=1, ..., m$, where subgrid $g_i$ has $n_i$ columns with column widths

$w_{i j}, \left(j = 1, ..., n_i \right) $

and spacings

$s_{i j}, \left(j = 1, ..., n_i +1 \right) $,

total width $\tau_i$ of subgrid $i$ is

$ \tau_i =\sum _{j=1}^{n_i} \left(s_{i j} + w_{i j} \right) + s_{i \left(n_i +1 \right)} $

In the following I added the following option settings to the three subgrids:

  (* grids 1 and 2 :*)
  Spacings -> 4, ItemSize -> {{15, {6}}, 1} 
  (* grid3 *)
  Spacings -> 3, ItemSize -> {{15, {4}}, 1}

With these settings:

 Grid[{{grid3}, {grid2}, {grid1}, {grid2}, {grid1}, {grid2}}]

gives

enter image description here

Note: using the following settings

 Spacings -> 4, ItemSize -> {{Scaled[6/24], {Scaled[3/24]}}, 1} (* grids 1 and 2*)
 Spacings -> 3, ItemSize -> {{Scaled[6/24], {Scaled[2/24]}}, 1}  (* grid 3 *)

produces the same result.


Original post: Subgrids with the same number of columns

You need to set common values for the column widths using ItemSize in the two grids.

 grid1 = Grid[{{Item["Name", Frame -> True, Background -> LightBlue,
     Alignment -> Left], 
    Item["Branch", Frame -> True, Background -> LightBlue, Alignment -> Left], 
    Item["Section", Frame -> True, Background -> LightBlue,  Alignment -> Left]},
     {"s", "IT", "A"}, {"u", "IT", "A"}, 
     {"b", "IT", "A"}, {"b", "IT", "A"}, {"b", "IT", "A"}}, Frame -> True, 
   ItemSize -> {{10, 7, 7}, 1}]; 
  grid2 = Grid[{{Item["Name", Frame -> True, Background -> LightBlue, 
    Alignment -> Left], 
   Item["Branch", Frame -> True, Background -> LightBlue,  Alignment -> Left], 
   Item["Section", Frame -> True, Background -> LightBlue,  Alignment -> Left]}, 
   {"subrahmanyam", "IT", "A"}, {"Madhu", "IT",  "A"}, 
   {"Kiran", "IT", "A"}, {"KalyanKumar", "IT", "A"}, 
   {"Pavan",  "IT", "A"}}, Frame -> True, 
   ItemSize -> {{10, 7, 7}, 1}]; 
  finalGrid = Grid[{{grid1}, {grid2}, {grid1}, {grid2}, {grid1}, {grid2}}]

enter image description here

Alternatively, you can use

   ItemSize -> {{Scaled[.1], Scaled[.05], Scaled[.05]}, 1} 

Note: If the length of the list colwidths in ItemSize->{colwidths, rowheights} is less than the number of columns in the grid, the remaining column widths will be determined by the default settings. If you put an extra layer of {} around colwidths, (that is,ItemSize->{{colwidths}, rowheights}) the column widths will be determined by cycling through the numbers provided in the list colwidths.

Update: Finer details of Grid option settings:

From Docs >> Grid >> Details and Options

enter image description here

Illustration with a simple example adapted from the documentation examples:

data = {{"first", 1, 2, 3, 4, 5},  {"second", 11, 5, 6, 4, 2},
  {"third", 111, 8, 9, 3, 3},   {"fourth", 1111, 10, 11, 2, 2}}; 

 Grid[Prepend[data, {"name", "a", "b", "c", "d", "e"}],
  Background -> {None, {Lighter[Yellow, .9], 
   {White, Lighter[Blend[{Blue, Green}], .8]}}},
 Dividers -> All,  Alignment -> {{Left, Right, {Left}}},
 ItemSize -> {{{7, 3, 5}}, 1}]

enter image description here

 Grid[Prepend[data, {"name", "a", "b", "c", "d", "e"}], Dividers -> All,
 Background -> {None, {Lighter[Yellow, .9], {White, 
     Lighter[Blend[{Blue, Green}], .8]}}},
 Alignment -> {{Left, Right, {Left}}},
 ItemSize -> {{10, 5, {2}}, 1}]

enter image description here

Further reading:

From Mathematica documentation pages:

Grid
ItemSize
Tutorial- Grids Rows and Columns

Related Q/As on Mathematica.SE:

Setting equal column widths in Grid
Setting row heights in Grid

$\endgroup$
10
  • $\begingroup$ ,if Grid have only 3 columns,it is working fine.If Grid have more number of 3 columns,it is not working.How can I solve that? $\endgroup$
    – subbu
    Dec 26, 2012 at 7:35
  • $\begingroup$ sorry still it was not working.. $\endgroup$
    – subbu
    Dec 26, 2012 at 8:08
  • $\begingroup$ @subbu, you can change {10,7,7} to match the number of columns you have, setting the width of each column. If the list you provide for column widths is shorter than the number of columns, widhts of remaining columns are determined by the default settings. By putting column width numbers in another layer of braces, you can cycle through these numbers to set the all the column widths. That is, if you have six columns, say, setting ItemSize->{{{10,5}}, 1} is equivalent to setting ItemSize->{{10,5,10,5,10,5}, 1}. $\endgroup$
    – kglr
    Dec 26, 2012 at 8:26
  • $\begingroup$ ... Or, if you use ItemSize->{{10,5,{2}}, 1}, then the first column will have width 10, second 5 and third and remaining columns will have width 2. Please see my edits to the post. $\endgroup$
    – kglr
    Dec 26, 2012 at 8:27
  • $\begingroup$ @subbu, sorry I was missing a set of braces in my earlier comment. If it is still not working, could you please post an example of your input and what you are getting? $\endgroup$
    – kglr
    Dec 26, 2012 at 8:30

Your Answer

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

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