Tables – How to Add Vertical Lines in a Multirow and Multicolumn Table

longtablemultirowrotatingtables

I am trying to finish this table scheme but I can't figure out what I am doing wrong. I want to convert those discontinued lines into continuous lines and I want to center the label "Total" between the 2 first columns.

\documentclass[a4paper]{article}

\usepackage{longtable}
\usepackage{rotating}
\usepackage{array}
\usepackage{multicol}
\usepackage{multirow}

\begin{document}

\begin{longtable}[c]{ccc|c|c|c|c|}
\cline{4-7}
& & & \multicolumn{4}{c|}{Var Y} \\ 
\cline{4-7}
& & & 
\begin{sideways} Cat Y1 \end{sideways} & 
\begin{sideways} Cat Y2 \end{sideways} & 
\begin{sideways} Cat Y3 \end{sideways} & 
\begin{sideways} Cat Y4 \end{sideways} \\ 
\hline \multirow{6}{*}{\begin{sideways}Var X\end{sideways}} &   
\multicolumn{1}{|c}{\multirow{2}{*}{Cat X1}} &
\multicolumn{1}{c|}{N} & 4 &  &  &  \\
\multicolumn{1}{|c}{}        &                &  
\multicolumn{1}{c|}{\%} & 100.00\% &  &  & \\
\cline{2-7} &
\multicolumn{1}{|c}{\multirow{2}{*}{Cat X2}} &
\multicolumn{1}{c|}{N} &  & 7 & 3 &    \\
\multicolumn{1}{|c}{}        &                &  
\multicolumn{1}{c|}{\%}&  & 70.00\% & 30.00\% &  \\    \cline{2-7} &
\multicolumn{1}{|c}{\multirow{2}{*}{Cat X3}} &
\multicolumn{1}{c|}{N} &  & 25 &  & 10 \\
\multicolumn{1}{|c}{}        &                &  
\multicolumn{1}{c|}{\%}&  & 71.43\% &  & 28.57\% \\
\hline &
\multicolumn{1}{c}{\multirow{2}{*}{Total}} &
%\multicolumn{1}{|c}{\multirow{2}{*}{\multicolumn{2}{c}{Total} }} &
\multicolumn{1}{c|}{N} & 4 & 23 & 3 & 10    \\
\multicolumn{1}{|c}{}        &                &  
\multicolumn{1}{c|}{\%}& 10.00\% & 57.50\% & 7.50\% & 2.50\% \\
\hline
\end{longtable} 

\end{document}

The table output looks like this:
enter image description here

Thank you very much for your help!

Best Answer

enter image description here

\documentclass[a4paper]{article}

\usepackage{longtable}
\usepackage{rotating}
\usepackage{array}
\usepackage{multicol}
\usepackage{multirow}
\begin{document}

\setlength\extrarowheight{3pt}

\begin{longtable}[c]{|c|cc|c|c|c|c|}
\cline{4-7}
\multicolumn{2}{c}{}& & \multicolumn{4}{c|}{Var Y} \\ 
\cline{4-7}
\multicolumn{2}{c}{}& & 
\rotatebox{90}{Cat Y1\ }&
\rotatebox{90}{Cat Y2\ }&
\rotatebox{90}{Cat Y3\ }&
\rotatebox{90}{Cat Y4\ }\\
    \hline \multirow{6}{*}{\begin{sideways}Var X\end{sideways}} &   
\multicolumn{1}{|c}{\multirow{2}{*}{Cat X1}} &
\multicolumn{1}{c|}{N} & 4 &  &  &  \\
    \multicolumn{1}{|c|}{}        &                &  
\multicolumn{1}{c|}{\%} & 100.00\% &  &  & \\
    \cline{2-7} &
\multicolumn{1}{|c}{\multirow{2}{*}{Cat X2}} &
\multicolumn{1}{c|}{N} &  & 7 & 3 &    \\
   \multicolumn{1}{|c|}{}        &                &  
\multicolumn{1}{c|}{\%}&  & 70.00\% & 30.00\% &  \\
    \cline{2-7} &
\multicolumn{1}{|c}{\multirow{2}{*}{Cat X3}} &
\multicolumn{1}{c|}{N} &  & 25 &  & 10 \\
    \multicolumn{1}{|c|}{}        &                &  
\multicolumn{1}{c|}{\%}&  & 71.43\% &  & 28.57\% \\
    \hline
\multicolumn{2}{|c}{\multirow{2}{*}{Total}} &
\multicolumn{1}{c|}{N} & 4 & 23 & 3 & 10    \\
    \multicolumn{2}{|c}{}                 &  
\multicolumn{1}{c|}{\%}& 10.00\% & 57.50\% & 7.50\% & 2.50\% \\
   \hline
\end{longtable} 

\end{document}
Related Question