# How to vertically center a content spanning multiple rows without using nested tables?

I don't want to use nested tables. My current code produces the following result.

And I want to get the following output.

And the minimal working example:

\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{array,multirow,longtable}

\parindent=0pt
\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax

\newcolumntype{A}[2]{%
>{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
c<{\vspace\tabcolsep\endminipage}}

\newcommand\x{\centering$\displaystyle\int f(x)\,\textrm{d}x=F(x)+C$}

\begin{document}
\begin{longtable}{
|A{0.2}{1.5}    % 0.2 of \linewidth, 1.5 of \arrayrulewidth
|A{0.5}{1}      % 0.5 of \linewidth, 1 of \arrayrulewidth
|A{0.3}{1.5}    % 0.3 of \linewidth, 1.5 of \arrayrulewidth
|}\hline
%%%%%%%%%%%%%%% FIRST ROW %%%%%%%%%%%%%%%
\x & \multicolumn{2}{
A{0.8}{1.5} % 0.8 of \linewidth, 1.5 of \arrayrulewidth
|}{\x} \tabularnewline\hline
%%%%%%%%%%%%%%% SECOND ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5}    % 0.7 of \linewidth, 1.5 of \arrayrulewidth
|}{\multirow{2}{*}{\x}} &
\x \tabularnewline\cline{3-3}
%%%%%%%%%%%%%%% THIRD ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5}    % 0.7 of \linewdith, 1.5 of \arrayrulewidth
|}{}                    &
\x \tabularnewline\hline
\end{longtable}
\end{document}


How to vertically center a content spanning multiple rows without using nested tables?

-
A post bounty of 500 will be given for an accepted answer. The post bounty is one that will be presented after executing a green check mark. If no useful answer, then there will be no such a bounty. :-) – xport Aug 17 '11 at 8:10
If you actually start the bounty, you will get the added benefit of your question being advertised more prominently on the site. And for people willing to work on an answer, it gives the added benefit of knowing that there will actually be at least some points awarded, if necessary by community verdict. – Jake Aug 17 '11 at 8:40
@Jake: It may waste the bounty if no useful answer exists. – xport Aug 17 '11 at 9:20
In that case, the bounty rep was the cost of drawing attention. – lockstep Aug 17 '11 at 9:30
As far as I can see, all provided solutions center the cell content correctly. It would probably help if you would tell each answerer individually what shortcoming you see in their answer. – Jake Aug 24 '11 at 12:49

the package ltablexis available on CTAN: http://www.ctan.org/tex-archive/macros/latex/contrib/ltablex Three X columns must give 3X when you want to have different columns widths: 0.6+1.5+0.9=3 For the 2nd and 3rd row you have 2X+1X=3X

\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{multirow,bigstrut,ltablex,calc}
\parindent=0pt
\tabcolsep=5pt
\newcolumntype{A}[1]{>{\hsize=#1\hsize\rule{0pt}{\tabcolsep}\newline}
X
<{\newline\rule[-\tabcolsep]{0pt}{\tabcolsep}}}

\newcommand\x{\makebox[\hsize]{$\displaystyle\int f(x)\,\textrm{d}x=F(x)+C$}}

\begin{document}

\keepXColumns
\begin{tabularx}{\linewidth}{| A{0.6} | A{1.5} | A{0.9} |}\hline
\x & \multicolumn{2}{A{2.4}|}{\x} \tabularnewline\hline
%%%%%%%%%%%%%%% SECOND ROW %%%%%%%%%%%%%%%
\multicolumn{2}{|A{2}|}{\multirow{4}[8]{*}{\x}} &  \x\tabularnewline\cline{3-3}
%%%%%%%%%%%%%%% THIRD ROW %%%%%%%%%%%%%%%
\multicolumn{2}{|A{2}|}{}                       &  \x \tabularnewline\hline
\end{tabularx}

\end{document}


-
+1 Please wait until the bounty button comes. – xport Aug 17 '11 at 10:02
Next bounty of 450 will automatically be yours. Thanks. – xport Feb 29 '12 at 1:46
@xport: Just to make sure the substantial amount of reputation isn't lost: The bounty will not be assigned automatically, and not even the half-bounty that can be assigned automatically will work in this case, since it's not a new answer. You will have to assign the bounty manually. See meta.tex.stackexchange.com/questions/2124/… – Jake Mar 1 '12 at 2:42

You could obtain the desired result with a simple modification to your original code, replacing \multirow{2}{*}{\x} with \hfil\multirow{4}{*}{\x}\hfil in the second row:

\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{array,multirow,longtable}

\parindent=0pt
\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax

\newcolumntype{A}[2]{%
>{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
c<{\vspace\tabcolsep\endminipage}}

\newcommand\x{\centering$\displaystyle\int f(x)\,\textrm{d}x=F(x)+C$}

\begin{document}
\begin{longtable}{
|A{0.2}{1.5}    % 0.2 of \linewidth, 1.5 of \arrayrulewidth
|A{0.5}{1}      % 0.5 of \linewidth, 1 of \arrayrulewidth
|A{0.3}{1.5}    % 0.3 of \linewidth, 1.5 of \arrayrulewidth
|}\hline
%%%%%%%%%%%%%%% FIRST ROW %%%%%%%%%%%%%%%
\x & \multicolumn{2}{
A{0.8}{1.5} % 0.8 of \linewidth, 1.5 of \arrayrulewidth
|}{\x} \tabularnewline\hline
%%%%%%%%%%%%%%% SECOND ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5}    % 0.7 of \linewidth, 1.5 of \arrayrulewidth
|}{\hfil\multirow{4}{*}{\x}\hfil} & \x \tabularnewline\cline{3-3}
%%%%%%%%%%%%%%% THIRD ROW %%%%%%%%%%%%%%%
\multicolumn{2}{
|A{0.7}{1.5}    % 0.7 of \linewdith, 1.5 of \arrayrulewidth
|}{}  & \x \tabularnewline\hline
\end{longtable}

\end{document}


-

For typesetting such tables, I find that the interface provided by ConTeXt is much easier to read.

\startsetups table:setup
\setupTABLE[each][each][align={middle,middle}, frame=on, framecolor=red]
\setupTABLE[column][1] [width=0.2\hsize]
\setupTABLE[column][2] [width=0.5\hsize]
\setupTABLE[column][3] [width=0.3\hsize]
\stopsetups

\startbuffer
$\displaystyle \int f(x)\,\normal{d}x=F(x)+C$
\stopbuffer

\starttext

\bTABLE[setups=table:setup]
\bTR
\bTD \getbuffer \eTD \bTD[nc=2] \getbuffer \eTD
\eTR
\bTR
\bTD[nc=2, nr=2] \getbuffer \eTD \bTD \getbuffer \eTD
\eTR
\bTR
\bTD \getbuffer \eTD
\eTR
\eTABLE
\stoptext


This gives

-

My proposed solution dispenses with minipages and explicit \vspace instructions. This is achieved by inserting a "strut" automatically in each row; the height of the strut exceeds that of the math expressions by a certain amount.

\documentclass{article}
\usepackage[a4paper,margin=1cm,showframe]{geometry}
\usepackage{array,multirow,tabularx}

\parindent=0pt
\arrayrulewidth=1pt
\tabcolsep=5pt

%% 'rbstrut' is short for 'really big strut'
\newcommand{\rbstrut}{\ensuremath{\vphantom{\displaystyle \int\limits_a^t}}}

\newcolumntype{A}[1]{>{\hsize=#1\hsize \centering \arraybackslash\rbstrut}X}

\newcommand\x{\ensuremath{\displaystyle\int f(x)\,\textrm{d}x=F(x)+C}}

\begin{document}

%% in tabularx terminology, the sum of the widths (arguments of A) have to
%% add up to the integer (in this case 3) that equals the number of columsn
\begin{tabularx}{\textwidth}{| A{0.6} | A{1.5} | A{0.9} |}
\hline
%%%% FIRST ROW
\x & \multicolumn{2}{c|}{\x} \\
\hline
%%%% SECOND ROW
\multicolumn{2}{|A{2.1}|}{ \multirow{4}{*}{\x} } & \x \\
\cline{3-3}
%%%% THIRD ROW
\multicolumn{2}{|c|}{}  &  \x \\
\hline
\end{tabularx}
\end{document}


-