当列中的文本很长时,表格无法正确生成

当列中的文本很长时,表格无法正确生成
\begin{sidewaystable}
\caption{Simulation of the proposed Trust Evaluation process}
\label{table:SimulationTrustEvaluation}
\newcolumntype{R}{>{\raggedright \arraybackslash} X}
\newcolumntype{S}{>{\raggedleft \arraybackslash} X}
\newcolumntype{C}{>{\centering \arraybackslash} X}
\begin{tabularx}{\linewidth} {>{\setlength\hsize{.14\hsize}}R >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C >{\setlength\hsize{.07\hsize}}C}
\toprule
\multicolumn{1}{r}{} & \multicolumn{4}{c}{User first commits a malicious activity and then plans to do a day-long authorized activity} & \multicolumn{4}{c}{User first do day-long authorized activity and then plan to commit malicious activities} & \multicolumn{4}{c}{User starts by random (whether to commit a malicious activity or do the day-long authorized activity)} \\
\cmidrule(l){2-5} \cmidrule(l){6-9} \cmidrule(l){10-13}
\multicolumn{1}{r}{} & \multicolumn{2}{c}{D} & \multicolumn{2}{c}{E} & \multicolumn{2}{c}{F} & \multicolumn{2}{c}{G} & \multicolumn{2}{c}{H} & \multicolumn{2}{c}{I} \\
\cmidrule(l){2-3} \cmidrule(l){4-5} \cmidrule(l){6-7} \cmidrule(l){8-9} \cmidrule(l){10-11} \cmidrule(l){12-13}
Type of users & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD \tabularnewline
\midrule
User with direct trust & 2 & 9 & 2 & 42 & 4 & 68 & 287 & 8977 & 4 & 65 & 37 & 954 \tabularnewline
User with indirect trust & 1 & 0 & 1 & 0 & 3 & 53 & 44 & 1368 & 4 & 44 & 12 & 326 \tabularnewline
\bottomrule
\end{tabularx}
\end{sidewaystable}

答案1

您不应该设置\hsize13 个单独的列,而应该只设置跨越 4 列的标题单元格。

你可能会问,为什么>{\hsize=\dimexpr4\hsize+6\tabcolsep\relax}C是 4 列标题单元格的宽度?因为可用宽度包括4\hsize 三个列间距,每个等于2\tabcolsep

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{tabularx,rotating,booktabs}
\newcolumntype{L}{>{\raggedright\arraybackslash} X}
\newcolumntype{C}{>{\centering\arraybackslash} X}
\newcolumntype{F}{>{\hsize=\dimexpr4\hsize+6\tabcolsep\relax}C}

\begin{document}

\begin{sidewaystable}
\caption{Simulation of the proposed Trust Evaluation process\strut}
\label{table:SimulationTrustEvaluation}
\begin{tabularx}{\linewidth} {@{} L *{12}{C} @{}}
\toprule
Type of users & 
\multicolumn{4}{F}{User first commits a malicious activity and then plans to do a day-long authorized activity} & 
\multicolumn{4}{F}{User first does day-long authorized activity and then plan to commit malicious activities} & 
\multicolumn{4}{F}{User starts at random (whether to commit a malicious activity or do the day-long authorized activity)} \\
\cmidrule(lr){2-5} \cmidrule(lr){6-9} \cmidrule(l){10-13}
 & 
\multicolumn{2}{c}{D} & \multicolumn{2}{c}{E} & \multicolumn{2}{c}{F} & 
\multicolumn{2}{c}{G} & \multicolumn{2}{c}{H} & \multicolumn{2}{c}{I} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} 
\cmidrule(lr){8-9} \cmidrule(lr){10-11} \cmidrule(l){12-13}
 & 
MPMA & CD & MPMA & CD & 
MPMA & CD & MPMA & CD & 
MPMA & CD & MPMA & CD \\
\midrule
Direct trust & 2 & 9 & 2 & 42 & 4 & 68 & 287 & 8977 & 4 & 65 & 37 & 954 \\
\addlinespace
Indirect trust & 1 & 0 & 1 & 0 & 3 & 53 & 44 & 1368 & 4 & 44 & 12 & 326 \\
\bottomrule
\end{tabularx}
\end{sidewaystable}

\end{document} 

答案2

我建议使用以下两个版本之一:

在此处输入图片描述

在此处输入图片描述

\documentclass{article}
\usepackage{caption}
\usepackage{rotating}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{calc}
\newlength{\mylength}
\begin{document}

\begin{sidewaystable}
\caption{Simulation of the proposed Trust Evaluation process}
\label{table:SimulationTrustEvaluation}
\setlength{\tabcolsep}{3.5pt}
\newcolumntype{R}{>{\raggedright \arraybackslash} X}
\newcolumntype{Y}{>{\centering\arraybackslash}p{4\mylength+6\tabcolsep}}
\setlength{\mylength}{\widthof{MPMA}}
\begin{tabularx}{\linewidth} {R *{12}{wc{\mylength}}}
\toprule
  & \multicolumn{4}{Y}{User first commits a malicious activity and then plans to do a day-long authorized activity} 
    & \multicolumn{4}{Y}{User first do day-long authorized activity and then plan to commit malicious activities} 
      & \multicolumn{4}{Y}{User starts by random (whether to commit a malicious activity or do the day-long authorized activity)}
       \\
\cmidrule(r){2-5} \cmidrule(lr){6-9} \cmidrule(l){10-13}
 & \multicolumn{2}{c}{D} & \multicolumn{2}{c}{E} & \multicolumn{2}{c}{F} & \multicolumn{2}{c}{G} & \multicolumn{2}{c}{H} & \multicolumn{2}{c}{I} \\
\cmidrule(r){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} \cmidrule(l){12-13}
Type of users & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD \\
\midrule
User with direct trust & 2 & 9 & 2 & 42 & 4 & 68 & 287 & 8977 & 4 & 65 & 37 & 954 \\
\addlinespace
User with indirect trust & 1 & 0 & 1 & 0 & 3 & 53 & 44 & 1368 & 4 & 44 & 12 & 326 \\
\bottomrule
\end{tabularx}
\end{sidewaystable}

\begin{sidewaystable}
\caption{Simulation of the proposed Trust Evaluation process}
\label{table:SimulationTrustEvaluation}
\setlength{\tabcolsep}{3pt}
\newcolumntype{R}{>{\raggedright \arraybackslash} X}
\setlength{\mylength}{\widthof{MPMA}}
\begin{tabularx}{\linewidth} {R *{12}{wc{\mylength}}}
\toprule
  & \multicolumn{4}{c}{malicious -- authorized} 
    & \multicolumn{4}{c}{authorized -- malicious} 
      & \multicolumn{4}{c}{random}
       \\
\cmidrule(r){2-5} \cmidrule(lr){6-9} \cmidrule(l){10-13}
 & \multicolumn{2}{c}{D} & \multicolumn{2}{c}{E} & \multicolumn{2}{c}{F} & \multicolumn{2}{c}{G} & \multicolumn{2}{c}{H} & \multicolumn{2}{c}{I} \\
\cmidrule(r){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} \cmidrule(lr){8-9} \cmidrule(lr){10-11} \cmidrule(l){12-13}
Type of users & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD & MPMA & CD \\
\midrule
User with direct trust & 2 & 9 & 2 & 42 & 4 & 68 & 287 & 8977 & 4 & 65 & 37 & 954 \\
\addlinespace
User with indirect trust & 1 & 0 & 1 & 0 & 3 & 53 & 44 & 1368 & 4 & 44 & 12 & 326 \\
\bottomrule
\multicolumn{13}{l}{\small malicious -- authorized = User first commits a malicious activity and then plans to do a day-long authorized activity}\\
\multicolumn{13}{l}{\small authorized -- malicious = User first do day-long authorized activity and then plan to commit malicious activities}\\
\multicolumn{13}{l}{\small random = User starts by random (whether to commit a malicious activity or do the day-long authorized activity)}\\
\end{tabularx}
\end{sidewaystable}

\end{document}

相关内容