為什麼使用多列時會遺失行?

為什麼使用多列時會遺失行?

健康列和總列之間缺少線條

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{makecell}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|c|c|cc|c|c|} 
\hline
& & \multicolumn{2}{c}{BMI} & \\ 
\multirow{-2.5}{*}{\makecell{Activity level}} & 
\multirow{-2.5}{*}{\makecell{Resting heart rate}} & 
{Unhealthy} & {Healthy} & \multirow{-2.5}{*}{\makecell{Total}} \\
\hline
Inactive & Normal   & 9  & 13 & 22\\ 
         & Abnormal & 16 & \cellcolor{pink}8 & 24\\
Active   & Normal   & 3  & 28 & 31 \\ 
         & Abnormal & \cellcolor{gray}6 & \cellcolor{gray}17 & 23 \\ 
\hline
Total    &          & 34 & 56 & \textbf{100} \\ 
\hline
\end{tabular}  
\end{table}
\end{document}

在此輸入影像描述

答案1

你觀察到,

健康列和總列之間缺少線條

您必須更換

\multicolumn{2}{c}{BMI}

\multicolumn{2}{c|}{BMI}

附錄:你的程式碼中有很多廢話;更糟的是,該表的可讀性並不好。這並不能讓您充滿信心\begin{tabular}{|c|c|cc|c|c|},因為該表包含 5 列,而不是 6 列。看看我如何嘗試解決下表中最緊迫的問題。就我自己而言,我不能說我覺得這張桌子有吸引力或吸引人。第二個表取消了所有垂直線,並使用左對齊而不是文字列居中。

在此輸入影像描述

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multirow,makecell}

%% for the second table:
\usepackage{booktabs,siunitx}
\usepackage[skip=0.333\baselineskip]{caption}

\begin{document}
\begin{table}
\centering
\caption{Dreadful}
\begin{tabular}{|c|c|cc|c|} 
\hline
\multirow{2}{*}{Activity level} & 
\multirow{2}{*}{Resting heart rate} & 
\multicolumn{2}{c|}{BMI} & 
\multirow{2}{*}{Total}\\ 
& & Unhealthy & Healthy &  \\
\hline
Inactive & Normal   & 9  & 13 & 22\\ 
         & Abnormal & 16 & \cellcolor{pink}8 & 24\\
Active   & Normal   & 3  & 28 & 31 \\ 
         & Abnormal & \cellcolor{gray}6 & \cellcolor{gray}17 & 23 \\ 
\hline
Total    &          & 34 & 56 & \textbf{100} \\ 
\hline
\end{tabular}  

\bigskip
\caption{Better}
\begin{tabular}{@{} llccc @{}} 
\toprule
Activity level & Resting heart rate & 
\multicolumn{2}{c}{BMI} & Total\\ 
\cmidrule{3-4}
& & Unhealthy & Healthy &  \\
\midrule
Inactive & Normal   & 9  & 13 & 22\\ 
         & Abnormal & 16 & \cellcolor{pink}8 & 24\\
\addlinespace
Active   & Normal   & 3  & 28 & 31 \\ 
         & Abnormal & \cellcolor{gray}6 & \cellcolor{gray}17 & 23 \\ 
\addlinespace
Total    &          & 34 & 66 & \textbf{100} \\ 
\bottomrule
\end{tabular}  
\end{table}
\end{document}

相關內容