2

As you can see in this table, I have a problem with the length of \cline. Can any one help me solve this problem? Thank you very much.

enter image description here

I used this code

\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

your problem is caused with @{\extracolsep{fill}}. i suggest to replace tabular* with tabularx table environment:

\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}

enter image description here

however i would design table without vertical lines and \cline, and \hline replace with rules from booktabs package:

\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}

enter image description here

addendum: with use of mhchem package and shading particular cells in table:

enter image description here

\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}
  • @helloWorld998, now i complete my answer. see second part ... – Zarko Aug 22 '17 at 11:09
  • what do you mean by second part Zarko, i'm sorry could you please explain – helloWorld998 Aug 22 '17 at 11:14
  • suggestion for table which is second in my answer – Zarko Aug 22 '17 at 11:31
  • oh i see, this also great, but why the values 340 and 289 are justified – helloWorld998 Aug 22 '17 at 11:43
  • if i want to colour only the box around CO2 how can i do it? – helloWorld998 Aug 22 '17 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.