I'd like to format a table in which first column has a single line of text, centered vertically across a variable number of rows. Currently, I am using a IfEqCase
and adding an entry in the first column only if some condition is met. This works as long as I have a fixed number of rows, and that number is odd (otherwise, the entry will not be vertically centered) :
\documentclass[11pt]{article}
\usepackage{multirow}
\usepackage{xstring}
\newcommand{\gridsize}[1]{\ensuremath{#1 \times #1}}
\newcommand{\addrowmpi}[4]{%
\IfEqCase{#1}{%
{1} {&} %
{2} {\gridsize{#2} &} %
{4} {&} %
}
#3 & #4 \%}
\begin{document}
\begin{table}[ht]
\caption{Multi-row table}
\begin{center}
\begin{tabular}{ccc}
\hline
\addrowmpi{1}{512}{462.7}{100} \\
\addrowmpi{2}{512}{231.4}{50}\\
\addrowmpi{4}{512}{115.7}{25}\\
\hline
\end{tabular}
\end{center}
\label{tab:multicol}
\end{table}
\end{document}
The result looks like :
But I'd like to have a variable number of rows, formatted ideally with something like addrowmpi
. The first column entry should be centered vertically across the rows.
Is there a way I can use the multirow
environment with a variable number of rows?
Best Answer
By nesting tables I can do essentially what I had in mind. Here is the code I came up with (simplified somewhat from the above) :
The first column has width 20 + 2*tabcolsep = 40pt, expected. And each column in the nested table has width 30+2*tabcolsep = 50. The
@{}
in the outer tabular command suppresses the additionaltabcolsep
space that would be added around the nested table, making the first and last columns of the nested table too wide.