# Multi-row and Multi-column text alignment and grid

I am trying to create the table below using tabular in latex.

Exploring the stack exchange for similar problems like this one has brought me to my current table:

\usepackage{multirow}

\begin{document}
\begin{table}[htbp]
\caption{Confusion Matrix}
\begin{center}
\begin{tabular}{|c|c|c|c|}\hline\vline
\multirow{1}{*}{N=986}
& \multicolumn{3}{c}{Predicted} \\%
\cline{3-4}
\multirow{1}{*}{}
& No & Yes\\%
\hline
\multirow{2}{*}{Actual}
& \multirow{1}{*}{No} & TN = 27 & FP = 176\\%
\cline{2-4}
& \multirow{1}{*}{Yes} & FN = 9 &  TP = 174\\%
\hline
\end{tabular}
\end{center}
\label{tab:confusion-matrix}
\end{table}

\end{document}


So far, this code produces the given table:

Now, how what should I change in order to make sure that N=986 would occupy a cell merging the first 2 rows and columns, while Predicted occupies Row 1 and Columns 3 & 4, while No and Yes occupies the Row 2 and Columns 3 & 4 respectively? I have already tried to play around with the code but this one was the closest that I got to the intended output.

P.S. I am also unsure why the vline is not there in the rightmost part of the first two cells. I am really having quite a trouble with table manipulation with LaTeX.

after removing unnecessary multirows and \vline, and adding missed ampersand:

\documentclass{article}
\usepackage{multirow}
\usepackage[skip=1ex]{caption}

\begin{document}
\begin{table}[htbp]
\caption{Confusion Matrix}
\label{tab:confusion-matrix}
\centering
\renewcommand\arraystretch{1.2}
\begin{tabular}{|c|c|c|c|}\hline
\multirow{2}{*}{N=986}
& \multicolumn{3}{c|}{Predicted}    \\
\cline{2-4}
&   & No & Yes                      \\
\hline
\multirow{2}{*}{Actual}
&   No  &   TN = 27 & FP = 176      \\
\cline{2-4}
&   Yes &   FN = 9 &  TP = 174      \\
\hline
\end{tabular}
\end{table}
\end{document}

• Is there any way to make N = 986 be merged to Column 2 while Predicted only occupies columns 3 and 4? – Whooper Apr 22 '19 at 18:03
• I also checked the link that you commented earlier and I just wanted to say that that was neat! – Whooper Apr 22 '19 at 18:05

A solution with makecell:

\documentclass{article}

\usepackage{array}
\usepackage{booktabs, caption}
\usepackage{multirow, makecell}
\usepackage{lipsum}

\begin{document}

\begin{table}[htbp]
\renewcommand{\thetable}{\Roman{table}}
\captionsetup{skip = 0pt, font = sc}
\setlength{\extrarowheight}{2pt}
\caption{Confusion Matrix}
\begin{center}
\begin{tabular}{|c|c|c|c|}\hline%\vline
\multicolumn{2}{|c|}{ \multirowcell{2.2}{N=986}}
& \multicolumn{2}{c|}{\bfseries Predicted} \\%
\cline{3-4}
\hline
\multirowcell{2.7}{\bfseries Actual}
& \thead{No} & TN = 27 & FP = 176\\%
\cline{2-4}
& \thead{Yes} & FN = 9 & TP = 174\\%
\hline
\end{tabular}
\end{center}
\label{tab:confusion-matrix}
\end{table}

\end{document}


• I already settled with @Zarko's answer above with a few modifications that I applied. However, this solution is also pretty neat! Thank you! – Whooper Apr 22 '19 at 18:16