Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any short syntax for setting the RowSpacing for only the last Row in a Grid?

RowSpacings allows one to change row spacings in a Grid.

Help says:

RowSpacings->{Subscript[s, 12],Subscript[s, 23],...} can be used to specify 
different spacings between different rows. If there are more rows than 
entries in this list, then the last element of the list is used repeatedly 
for the remaining rows

Notice, where it says the remaining rows above.

What I want is to make all starting rows use some spacing, but the last row to use different spacing.

This example does the documented way (the remaining rows)

n = 5;
data = Table[Random[], {n}, {n}];
Grid[data, Frame -> All, RowSpacings -> {6, 1}, Alignment -> Center]

enter image description here

But I wanted to do the reverse, i.e. set the last row to something, and all rows before it to another. Only way I could do it is the long way, i.e. by writing down all the spacings for all the rows until the last one:

n = 5;
data = Table[Random[], {n}, {n}];
Grid[data, Frame -> All, 
 RowSpacings -> {Sequence @@ Table[1, {n - 2}], 6},Alignment -> Center]

enter image description here

The above is just another way to write

Grid[data, Frame -> All, RowSpacings -> {1, 1, 1, 6}, Alignment -> Center]

I also tried things like

Grid[data, Frame -> All, RowSpacings -> {{1}, 6}, Alignment -> Center]
Grid[data, Frame -> All, RowSpacings -> {{1 ;; 3}, 6}, Alignment -> Center]

but they do not work. I could not find a short cut like in the first example above.

Any one knows a trick to tell RowSpacings to set only the last Row to some specific value, and all rows up to it to some other one, without the use of the above hack?

This is not a big deal really to do it as I did, I just was wondering if I am overlooking one of those trick syntax usage in Mathematica, that is all.

thanks,

like image 912
Nasser Avatar asked Dec 31 '11 14:12

Nasser


People also ask

How do you center an element on the last row in CSS grid?

To center the last one, you can double the number of columns to 4 and set each item to span 2 . If the last item is odd, start on the second column so it spans columns 2 and 3. With a bit of math and SCSS, this can be generalized to work with any number of columns.

How do you set rows in grid?

The grid-row CSS shorthand property specifies a grid item's size and location within the grid row by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the inline-start and inline-end edge of its grid area.

Which CSS property is shorthand for the grid row start and grid row end properties?

The grid-area property can be used as a shorthand property for the grid-row-start , grid-column-start , grid-row-end and the grid-column-end properties.

How do I set row height in grid?

To change the row height for the whole grid, set the property rowHeight to a positive number. For example, to set the height to 50px, do the following: const gridOptions = { rowHeight: 50, // other grid options ... } Changing the property will set a new row height for all rows, including pinned rows top and bottom.


1 Answers

Assuming you don't have other reasons to use RowSpacings + GridBox combination, using Spacings option for Grid

Grid[data, Spacings -> {Automatic, Dimensions[data][[1]] -> 6}, 
     Frame -> All, Alignment -> Center]

gives what you need.

EDIT: More conveniently, Grid[data, Spacings -> {Automatic, -2-> 6}, Frame -> All, Alignment -> Center] where -2 refers to the second from last element of the row indices that go from 1 to 1+number of rows

EDIT 2: Using GridBox, the following produces the same output:

GridBox[data, RowLines -> True, ColumnLines -> True, 
  GridFrame -> True, RowAlignments -> Center, 
  ColumnAlignments -> Center, 
  GridBoxSpacings -> {"RowsIndexed" -> {-2 -> 6}}] //DisplayForm
like image 52
kglr Avatar answered Oct 04 '22 14:10

kglr