3

I am trying to vertically center the subtables in the cells of column 2:

enter image description here

As you can see, the subtable with a1, a2 is vertically top centered. Analogous situation regarding the cell bellow.

I would like to keep the structure of table of each cell of column2.

I have tried the following code, but I do not know how to progress:

\documentclass{article}
\usepackage{multirow}
\usepackage{longtable}

\begin{document}

\begin{longtable}{|c|c|c|}
    \caption{Your table caption} \label{tab:my_label} \\
    \hline
    header 1 & header 2 & header 3 \\
    \hline
    \endhead
    \multirow{3}{*}{row1} & a1 & b1 \\
    & a2 & b2 \\
    & & b3 \\
    \hline
    \multirow{3}{*}{row2} & a3 & b4 \\
    & a4 & b5 \\
    & & b6 \\
    \hline
\end{longtable}

\end{document}

Thanks in advance.

2 Answers 2

2

The multirow package is not necessary in this case. You can nest tabular environments and easily control between cells alignment, such as top, middle and bottom

\documentclass{article}
\usepackage{longtable}

\newcommand\TB[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

\renewcommand*{\arraystretch}{1.15}


\begin{document}
\begin{longtable}{|c|c|c|}
  \caption{Your table caption} \label{tab:my_label} \\
  \hline
  header 1  & header 2    & header 3 \\
  \hline
  \endhead
  row1      & \TB{a1\\a2} & \TB{b1\\b2\\b3} \\
  \hline
  row2      & a3          & \TB[t]{b4\\b5} \\
  \hline
  row3      & a4          & \TB[b]{b6\\b7} \\
  \hline
\end{longtable}
\end{document}

enter image description here

0
4

The way you're entering the data in the longtable environment, LaTeX "knows" about individual cells -- and about shifting some of the down (or up) if they're the argument of a \multirow instruction. By default, LaTeX does not know that vertically arranged cells sandwiched between \hline directives are supposed to form a "sub-table".

If you want to have LaTeX operate on subtables rather than on cells, you need to provide the crucial pieces of information yourself. In the code below, this is done via subtab instructions (defined in the preamble of the test document in terms of a tabular environment; by default, adjacent tabular environments are centered vertically relative to each other). Note that it's not necessary to encase row1 and row2 in \subtab instructions.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{array}    
\newcommand\subtab[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}

\begin{document}

\begin{tabular}{|c|c|c|}
    \hline
    header 1 & header 2 & header 3 \\
    \hline
    row1 & \subtab{a1\\a2} & \subtab{b1\\b2\\b3} \\
    \hline
    row2 & \subtab{a3\\a4} & \subtab{b4\\b5\\b6} \\
    \hline
\end{tabular}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .