4

I want to center the merged rows ac vertically. How to do so?

enter image description here

\documentclass[preview,border=3pt]{standalone}
\usepackage{array,multirow}
\renewcommand\arraystretch{1.5}
\newcommand{\cell}[2]{%
    $\displaystyle
    \begin{array}{c}
     {} #1 m \\
     {} #2 n
    \end{array}$}
\begin{document}
\begin{tabular}{|*{4}{c|}}\hline
\multicolumn{1}{|c|}{\multirow{2}{*}{$ac$}} &
            $+$ &
            \cell{+}{+} &
            \cell{-}{-} \\\cline{2-4}
\multicolumn{1}{|c|}{} &
            $-$ &
            \cell{-}{+} &
            \cell{+}{-} \\\hline                                            
\multicolumn{2}{c|}{} &
            $+$ &
            $-$ \\\cline{3-4}
\multicolumn{2}{c|}{} &
            \multicolumn{2}{c|}{$b$}    \\\cline{3-4}
\end{tabular}
\end{document}
1
  • the multirow can take any number of rows as an option for fine tuning -- have a look below -- \multirow{3.1}{*}{$ac$}}
    – js bibra
    Feb 27, 2021 at 1:25

3 Answers 3

2

Unnecessarily complicated:

mwe

\documentclass{article}
\renewcommand\arraystretch{1.5}
\usepackage{array,multirow}
\begin{document}
\begin{tabular}{*{4}{|>{ $}c<{$ }}|}\hline
\multirow{4}{*}{$ac$} & \multirow{2}{*}{+}    & +\ m  & -\ m \\
                      &                       & +\ n  & -\ n \\\cline{2-4}
                      &  \multirow{2}{*}{$-$} & +\ m  & -\ m \\
                      &                       & +\ n  & -\ n \\\cline{1-4}
\multicolumn{2}{c|}{}                         & +    & -    \\\cline{3-4}
\multicolumn{2}{c|}{} & \multicolumn{2}{c|}{b} \\\cline{3-4}
\end{tabular}
\end{document}
1
  • It took a lot of time to think which answer should be accepted. And finally Albert Einstein inspired me to choose the simpler solution. Sep 3, 2021 at 18:53
4

You can do easily that tabular with {NiceTabular} of nicematrix.

In {NiceTabular}, you merge cells both horizontally and vertically with the command \Block. For the rows, you give the number of logical rows (and not the number of physical lines as with \multicolumn).

With the key hvlines, all the rules are drawn, excepted in the blocks (constructed by \Block) and in the corners specified by the key corners (the corners are computed automatically).

\documentclass[preview,border=3pt]{standalone}
\usepackage{nicematrix}
\renewcommand\arraystretch{1.5}
\newcommand{\cell}[2]{%
    $\displaystyle
    \begin{array}{c}
     {} #1 m \\
     {} #2 n
    \end{array}$}
\begin{document}
\begin{NiceTabular}{cccc}[hvlines,corners=SW]% SW : south west
\Block{2-1}{$ac$} &
            $+$ &
            \cell{+}{+} &
            \cell{-}{-} \\
&
            $-$ &
            \cell{-}{+} &
            \cell{+}{-} \\
& &         $+$ &
            $-$ \\
& & \Block{1-2}{$b$}    \\
\end{NiceTabular}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

2

enter image description here

\documentclass[preview,border=3pt]{standalone}
\usepackage{array,multirow}
\renewcommand\arraystretch{1.5}
\newcommand{\cell}[2]{%
    $\displaystyle
    \begin{array}{c}
        {} #1 m \\
        {} #2 n
    \end{array}$}
\begin{document}
    \begin{tabular}{|*{4}{c|}}\hline
        \multicolumn{1}{|c|}{\multirow{3.1}{*}{$ac$}} &
        $+$ &
        \cell{+}{+} &
        \cell{-}{-} \\\cline{2-4}
        \multicolumn{1}{|c|}{} &
        $-$ &
        \cell{-}{+} &
        \cell{+}{-} \\\hline                                            
        \multicolumn{2}{c|}{} &
        $+$ &
        $-$ \\\cline{3-4}
        \multicolumn{2}{c|}{} &
        \multicolumn{2}{c|}{$b$}    \\\cline{3-4}
    \end{tabular}
\end{document}
7
  • it is approximation -- you can bracket it by using 2 and then 4 and then narrow down to 3.1 -- 3.2 will take it a bit too far down
    – js bibra
    Feb 27, 2021 at 1:25
  • The exact value is $\sqrt{10}$. I think. :-) Feb 27, 2021 at 1:28
  • 1
    @MoneyOrientedProgrammer would you like to accept the answer and upvote the answer if it meets the requirement projected in the question
    – js bibra
    Feb 27, 2021 at 5:43
  • As usual, it will take a couple of hours, days, months, years, decades, or maybe centuries to decide the accepted answer. Please be patient. :-) Feb 27, 2021 at 5:46
  • not an issue -- however do let us know if the answer meets the projected requirement -- I want to center the merged rows ac vertically. -- do let us know if there was any other requirement projected which was not addressed -- thank you for the eons
    – js bibra
    Feb 27, 2021 at 5:49

You must log in to answer this question.

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