多列多行表问题

多列多行表问题

正如您在这张表中看到的,我对 的长度有疑问\cline。有人能帮我解决这个问题吗?非常感谢。

在此处输入图片描述

我用了这个代码

\begin{table}[!h]
\begin{center}
 \captionsetup{justification=centering}
  \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{\textbf{Energy consumption \& emissions}} & \textbf{ Van }  & \textbf{Truck} \\ \hline
\multirow{4}{*}{2009}   & Energy consumption (MJ/km) & 4.6   & 13.3  \\ \cline{2-4}
                        & (CO\textsubscript{2}) (g/km)                & 340   & 973.5 \\ \cline{2-4}
                        & (NO\textsubscript{x})(g/km)                 & 1.281 & 8.048 \\ \cline{2-4}

                        & (SO\textsubscript{2}) (mg/km)               & 2.6   & 7.45  \\ \hline
\multirow{4}{*}{2014}   & Energy consumption (MJ/km)  & 3.9   & 12.55 \\ \cline{2-4}
                        & (CO\textsubscript{2})              & 289   & 933.5 \\ \cline{2-4}
                        & (NO\textsubscript{x})(g/km)                 & 0.944 & 4.035 \\ \cline{2-4}
                        & (SO\textsubscript{2}) (mg/km)               & 1.75  & 5.55  \\ \hline
\end{tabular*}
\caption{bb}\label{cc}
\end{center}
\end{table}

答案1

您的问题是由 引起的@{\extracolsep{fill}}。我建议tabular*tabularx表环境替换:

\documentclass{article}
\usepackage{multirow,tabularx}
    \newcolumntype{C}{>{\centering\arraybackslash}X}

\usepackage{caption}

\begin{document}
\begin{table}[ht]
\centering
 \captionsetup{justification=centering}
  \begin{tabularx}{\linewidth}{@{}|C|l|C|C| @{}}
\hline
\multicolumn{2}{|c|}{\textbf{Energy consumption \& emissions}}
                                    & \textbf{Van}  & \textbf{Truck}    \\ \hline
\multirow{4}{*}{2009}
    & Energy consumption (MJ/km)    & 4.6           & 13.3              \\ \cline{2-4}
    & (CO\textsubscript{2}) (g/km)  & 340           & 973.5             \\ \cline{2-4}
    & (NO\textsubscript{x})(g/km)   & 1.281         & 8.048             \\ \cline{2-4}
    & (SO\textsubscript{2}) (mg/km) & 2.6           & 7.45              \\ \hline
\multirow{4}{*}{2014}
    & Energy consumption (MJ/km)    & 3.9           & 12.55             \\ \cline{2-4}
    & (CO\textsubscript{2})         & 289           & 933.5             \\ \cline{2-4}
    & (NO\textsubscript{x})(g/km)   & 0.944         & 4.035             \\ \cline{2-4}
    & (SO\textsubscript{2}) (mg/km) & 1.75          & 5.55              \\ \hline
\end{tabularx}
\caption{bb}\label{cc}
\end{table}

\end{document}

在此处输入图片描述

不过,我会设计没有垂直线的表格 \cline,并\hline用包中的规则替换booktabs

\documentclass{article}
\usepackage{booktabs,tabularx}
    \newcommand\mcx[1]{\multicolumn{1}{>{\bfseries\centering\arraybackslash}X}{#1}}
\usepackage{siunitx}

\usepackage{caption}

\begin{document}
\begin{table}[ht]
\centering
\captionsetup{justification=centering}
\begin{tabularx}{\linewidth}{X l *{2}{S[table-format=5.3]} }
    \toprule
\multicolumn{2}{c}{\textbf{Energy consumption \& emissions}}
                                    & \mcx{Van}  & \mcx{Truck}    \\ 
    \midrule
2009
    & Energy consumption (MJ/km)    & 4.6           & 13.3              \\ 
    & (CO\textsubscript{2}) (g/km)  & 340           & 973.5             \\
    & (NO\textsubscript{x})(g/km)   & 1.281         & 8.048             \\
    & (SO\textsubscript{2}) (mg/km) & 2.6           & 7.45              \\ 
    \midrule
2014
    & Energy consumption (MJ/km)    & 3.9           & 12.55             \\ & (CO\textsubscript{2})         & 289           & 933.5             \\ & (NO\textsubscript{x})(g/km)   & 0.944         & 4.035             \\ & (SO\textsubscript{2}) (mg/km) & 1.75          & 5.55              \\ 
    \bottomrule
\end{tabularx}
\caption{bb}\label{cc}
\end{table}

\end{document}

在此处输入图片描述

附录: 使用mhchem包装并在表中阴影特定单元格:

在此处输入图片描述

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs,tabularx}
    \newcommand\mcx[1]{\multicolumn{1}{>{\bfseries\centering\arraybackslash}X}{#1}}
\usepackage{siunitx}
\usepackage{mhchem}

\usepackage{caption}

\begin{document}
    \begin{table}[ht]
\centering
\captionsetup{justification=centering}
\begin{tabularx}{\linewidth}{X l *{2}{S[table-format=5.3]} }
    \toprule
\multicolumn{2}{c}{\textbf{Energy consumption \& emissions}}
                                    & \mcx{Van} & \mcx{Truck}       \\ 
    \midrule
2009
    & Energy consumption (MJ/km)    & 4.6       & 13.3              \\ 
    & \cellcolor{gray!20}{\ce{CO2}} 
                    (g/km)          & 340       & 973.5             \\
    & \ce{NO_{x}}   (g/km)          & 1.281     & 8.048             \\
    & \ce{SO2}      (mg/km)         & 2.6       & 7.45              \\ 
    \midrule
2014
    & Energy consumption (MJ/km)    & 3.9       & 12.55             \\ 
    & \cellcolor{gray!20}{\ce{CO2}} & 289       & 933.5             \\
    & \ce{NO_{x}}   (g/km)          & 0.944     & 4.035             \\ 
    & \ce{SO2}      (mg/km)         & 1.75      & 5.55              \\
    \bottomrule
\end{tabularx}
\caption{bb}\label{cc}
    \end{table}
\end{document}

相关内容