3
$\begingroup$

I have data in two Grids with a different number of rows each and want the output to be formatted to have the two grids in a row aligned at the top.

Here is a MWE:

First the headers (same number of column in the grids)

headersLeft = {"h11", "h12", "h13"};
headersRight = {"h21", "h22", "h23"};

next the data:

dataLeft = 
  Flatten /@ 
   Transpose@({{"one", "two", "three"}, Table[
       RandomReal[10, 2], {3}]});
dataRight = 
  Flatten /@ Transpose@({{"one", "two", "three", "four", 
       "five"}, Table[RandomReal[10, 2], {5}]});

then:

Row[{Grid[Prepend[dataLeft, headersLeft]],
  Grid[Prepend[dataRight, headersRight]]
  }, Spacer[20], Alignment -> {Center, Top}]

I get:

enter image description here

Question: How can I get the grids aligned at top? I tried with ItemSize, but this is only good for the sizes of each item (in the Grid). Also Frame is not displayed when given as an option. Same output as shown when using:

Row[{Grid[Prepend[dataLeft, headersLeft]],
  Grid[Prepend[dataRight, headersRight]]
  }, Spacer[20], Alignment -> {Center, Top}, Frame -> All]
$\endgroup$
6
$\begingroup$
Grid[{{Grid[Prepend[dataLeft, headersLeft]], Spacer[20], 
   Grid[Prepend[dataRight, headersRight]]}}, 
 Alignment -> {Center, Top}, Dividers -> All]

enter image description here

$\endgroup$
  • $\begingroup$ Sometimes solutions are obvious - if you know them ;-) $\endgroup$ – mgamer Apr 25 '18 at 6:38
5
$\begingroup$

Give each Grid the baseline position 1 or Top:

Row[
    {
    Grid[Prepend[dataLeft,headersLeft],BaselinePosition->1],
    Grid[Prepend[dataRight,headersRight],BaselinePosition->1]
    },
    Spacer[20]
]

enter image description here

Row[
    {
    Grid[Prepend[dataLeft,headersLeft],BaselinePosition->Top],
    Grid[Prepend[dataRight,headersRight],BaselinePosition->Top]
    },
    Spacer[20]
]

enter image description here

If you want a frame around the whole Row object, use Frame->True, not Frame->All. If you want a divider between the two grids, then you should use Grid instead of Row, as @Anton does.

$\endgroup$
  • $\begingroup$ Thank you for the hint about Frame, I messed up with Row and Grid... $\endgroup$ – mgamer Apr 25 '18 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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