20
$\begingroup$

In general, what are the advantages of using Grid versus a combination of Column and Row? Is Grid better in some situations but not others?

For example, is there any reason why one would do this:

Grid[{{"Some text I want above a formula:"},
      {Integrate[f[x], x]}}]

versus this:

Column[{Row[{"Some text I want above a formula:"}], 
        Row[{Integrate[f[x], x]}]}]
$\endgroup$
1
  • 2
    $\begingroup$ It seems Grid is natural way of dealing with arrays while it is more convinient to use Rows for rows with different quantity of columns. Of course with SpanFrom~ and others You can do whatever You need in the way You want to. I don't see any limitations favouring any of this ways. Do what is shorter and more convinient. $\endgroup$
    – Kuba
    Commented Feb 13, 2013 at 20:06

2 Answers 2

18
$\begingroup$

There are several differences in behaviour. One is that Row will be line-wrapped if it does not fit the notebook window:

Mathematica graphics

Grid will not.

Also with Grid the alignment is global, so the $n^\textrm{th}$ elements in each row will always line up vertically with each other. This is not the case with several Rows in one Column.

table = Table[RandomInteger[13, 10], {5}]

Mathematica graphics

Grid allows merging several grid cells too (see e.g. SpanFromLeft). This is just some of the most obvious differences.

Mathematica graphics

$\endgroup$
1
  • 5
    $\begingroup$ Grid also allows you to algorithmically specify things like background coloring (although you need a degree in math & magic to get your specs right). $\endgroup$ Commented Feb 13, 2013 at 21:33
19
$\begingroup$

If I am making something ad hoc I often use Column and Row but later re-write as Grid once I have settled on what I want. I find Grid easier to maintain and more flexible.

Grid and Column are both GridBox whereas Row is RowBox. There are some limitations with RowBox -- for example one thing that comes to mind is limited FrameStyle compared to what is available in GridBox

Row[{1, 22, 333}, Background -> Red, Frame -> True, 
 FrameStyle -> Directive[Dashed]]

Grid[{{1, 22, 333}}, Background -> Red, Frame -> True, 
 FrameStyle -> Directive[Dashed]]

enter image description here


For some reason I always seem to find it easier to position elements in a Grid cell than a Row -- maybe just lack of perseverance on my part (?). Row allows numbered alignments Alignment->{-0.5,0.5} but I don't really see the point. On the other hand this would be useful in Grid but a known bug since at least V7 prevents it from working.

$\endgroup$
1
  • $\begingroup$ Thank you @mike-honeychurch for your excellent answer. I would like to have accepted your answer to my question as well, but it turns out there can only be one. $\endgroup$ Commented Feb 14, 2013 at 20:57

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.