3

I have a one-row table with a number of columns. Each cell in this row is another table, which can be either 2x1 or 3x1. I want to make sure that all these sub-tables are aligned to the top of the main row. e.g.:

-------------------
|  1  |  2  |  3  |
|  a  |  b  |  c  |
|  A  |     |  C  |
-------------------

A simple enough solution would be to add a phantom line to middle cell. However, if all the cells are 2x1 I don't want to have an empty row:

-------------------
|  1  |  2  |  3  |
|  a  |  b  |  c  |
|     |     |     |
-------------------

The above is not good for me, and should be

-------------------
|  1  |  2  |  3  |
|  a  |  b  |  c  |
-------------------

Any ideas how to accomplish this?

giladrv
  • 195

2 Answers2

3

Use the [t]-alignment for your nested tabular, wrapped in a macro to avoid inserting forced line breaks:

enter image description here

\documentclass{article}
\newcommand{\vtable}[2][c]{% \vtable[<col align>]{<stuff>}
  \begin{tabular}[t]{@{}#1@{}}#2\end{tabular}}
\begin{document}
\begin{tabular}{|c|c|c|}
  \vtable{1 \\ a \\ A} & \vtable{2 \\ b} & \vtable{3 \\ c \\ C}
\end{tabular}

\bigskip

\begin{tabular}{|c|c|c|}
  \vtable{1 \\ a } & \vtable{2 \\ b} & \vtable{3 \\ c}
\end{tabular}
\end{document}

The definition of \vtable takes an optional argument that specifies the alignment within the nested tabular. The default here is centred (as indicated by [c] in \newcommand{\vtable}[2][c]). So, \vtable{...} defaults to \vtable[c]{...}, which centres the nested tabular. Using \vtable[l]{...} would impose a left-alignment within the nested tabular.

The second arguments takes the regular tabular definition for cells/rows, which could include \\. Using a nested tabular column specification of @{}.@{} removes the default padding around the column - LaTeX inserts \tabcolsep on either side of a column, unless otherwise specified. Using @{} removes this. As a reference, see Column padding in tables.

The makecell package provides something similar through \makecell.

Werner
  • 603,163
  • two questions: (1) what is the purpose of [c] in the definition of vtable. (2) what are the '@{}' parts for? – giladrv Feb 07 '13 at 22:34
  • @giladrv: I've added some discussion in that regard. Let me know if you need more... – Werner Feb 07 '13 at 22:43
  • If I understand correctly, LaTeX pads a table with spaces, and we don't want more space for inner tables so that's what @{} is for? – giladrv Feb 07 '13 at 23:38
  • regardless, the main question was answered here, thanks! – giladrv Feb 07 '13 at 23:39
  • @giladrv: Yes. Your outer tabular adds the spaces already. So, if your inner tabular just used c (instead of @{}c@{}), it would double the space. – Werner Feb 07 '13 at 23:50
2

just use

\begin{tabular}[t]

for the inner tables so they line up on their top row.

David Carlisle
  • 757,742