[Tex/LaTex] How to vertically center a content spanning multiple rows without using nested tables

tablesvertical alignment

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

enter image description here

And I want to get the following output.

enter image description here

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?


I have tried all answers (excluding Aditya's answer) and all don't vertically center the cell spanning multiple rows. Please kindly recheck your answer, or I might be wrong. 🙂

Best Answer

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}

enter image description here