如何让文本自动换行到该 LaTeX 表的最后一行,如图所示?

如何让文本自动换行到该 LaTeX 表的最后一行,如图所示?

最后一行应该如图所示换行

\documentclass[a4paper,headsepline,12pt,toc=index,
toc=bibliography,ngerman]{scrreprt}

\usepackage{multirow}
\usepackage{graphicx}


\begin{document}

{
\renewcommand{\arraystretch}{2}
\begin{table}[htbp]
    \centering
    \caption{My caption}
    \label{my-label}
    \resizebox{\textwidth}{!}{%
        \begin{tabular}{|c|c|c|c|c|c|c|}
            \hline
            \multirow{2}{*}{\textbf{Method}}                                               & \multicolumn{2}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Non-frictional  Parallelepiped\end{tabular}}} & \multicolumn{2}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Non-frictional  Knight\end{tabular}}} & \multicolumn{2}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Frictional  Knight\end{tabular}}} \\ \cline{2-7} 
            & \multicolumn{2}{c|}{1880 Experimental Cases}                                                                    & \multicolumn{2}{c|}{21357 Experimental Cases}                                                           & \multicolumn{2}{c|}{21357 Experimental Cases}                                                       \\ \hline
            \multirow{2}{*}{\begin{tabular}[c]{@{}c@{}}Modified  NEW Method \end{tabular}} & Final Quality                                                                        & Time (s)        & Final Quality                                                                     & Time (s)   & Final Quality                                                                   & Time (s) \\ \cline{2-7} 
            & \begin{tabular}[c]{@{}c@{}}0.200 in 30 samples  with pop size 400\end{tabular}        & 35.0            & \begin{tabular}[c]{@{}c@{}}0.126 in 20 samples  with pop. size 200\end{tabular}    & 63.8       & \begin{tabular}[c]{@{}c@{}}0.146 in 20 samples  with pop. size 200\end{tabular}  & 368.8    \\ \hline
        \end{tabular}%
    }
\end{table}

}


\end{document}

答案1

我建议您使用tabularx具有 7 个等宽列的环境。这样您就可以省去所有这些内部tabular环境。这种方法还省去了\resizebox仅使表格材料适合文本块宽度的指令。采用这种方法的结果显示在下面的第一个表中。

在这个主题的变体中,您可以决定没有必要(甚至不需要)使所有列都具有相同的宽度。相反,您可能会决定,例如,“最终质量”列应该是“时间”列的三倍宽。这也可以通过环境来处理tabularx,如下面第二个表所示。第二个tabularx环境与第一个环境仅在设置行中不同。而第一个环境是通过

\begin{tabularx}{\textwidth}{|*{7}{C|}}

第二个有以下更复杂的定义:

\begin{tabularx}{\textwidth}{|C|*{3}{>{\hsize=1.5\hsize}C|>{\hsize=0.5\hsize}C|}}

与切换到 结合\footnotesize,这种方法似乎可以实现您想要的格式。

这两种方法中的任何一种似乎都比使用\resizebox并最终得到超小字母要好得多;参见第三个表,它显示了原始代码的结果。

在此处输入图片描述

\documentclass[a4paper,headsepline,12pt,toc=index,
               toc=bibliography,ngerman]{scrreprt}
\usepackage{tabularx,graphicx,multirow}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}

\begin{table}[htbp]
\renewcommand{\arraystretch}{1.5}
\setlength\tabcolsep{3pt}
\caption{Equal-width columns, \texttt{\string\normalsize}} \label{label-one}
\begin{tabularx}{\textwidth}{|*{7}{C|}}
\hline
\textbf{Method} 
& \multicolumn{2}{>{\hsize=2\hsize}C|}{\bfseries Non-frictional Parallelepiped} 
& \multicolumn{2}{>{\hsize=2\hsize}C|}{\bfseries Non-frictional Knight} 
& \multicolumn{2}{>{\hsize=2\hsize}C|}{\bfseries Frictional Knight} \\ 
\cline{2-7} 
& \multicolumn{2}{>{\hsize=2\hsize}C|}{1880 Experimental Cases} 
& \multicolumn{2}{>{\hsize=2\hsize}C|}{21357 Experimental Cases} 
& \multicolumn{2}{>{\hsize=2\hsize}C|}{21357 Experimental Cases}  \\ 
\hline
Mod.\ new Method
& Final Quality  & Time (s)        
& Final Quality  & Time (s)   
& Final Quality  & Time (s) \\ 
\cline{2-7} 
& 0.200 in 30~samples with pop.\ size 400 & 35.0  
& 0.126 in 20~samples with pop.\ size 200 & 63.8       
& 0.146 in 20~samples with pop.\ size 200 & 368.8 \\ 
\hline
\end{tabularx}

\bigskip
\footnotesize
\caption{Unequal-width columns, \texttt{\string\footnotesize}} \label{label-two}
 \begin{tabularx}{\textwidth}{|C|
   *{3}{>{\hsize=1.5\hsize}C|>{\hsize=0.5\hsize}C|}}
 \hline
 \textbf{Method} 
 & \multicolumn{2}{>{\hsize=2\hsize}C|}{\bfseries Non-frictional Parallelepiped} 
 & \multicolumn{2}{>{\hsize=2\hsize}C|}{\bfseries Non-frictional Knight} 
 & \multicolumn{2}{>{\hsize=2\hsize}C|}{\bfseries Frictional Knight} \\ 
 \cline{2-7} 
 & \multicolumn{2}{>{\hsize=2\hsize}C|}{1880 Experimental Cases} 
 & \multicolumn{2}{>{\hsize=2\hsize}C|}{21357 Experimental Cases} 
 & \multicolumn{2}{>{\hsize=2\hsize}C|}{21357 Experimental Cases}  \\ 
 \hline
 Mod.\ new Method
 & Final Quality  & Time (s)        
 & Final Quality  & Time (s)   
 & Final Quality  & Time (s) \\ 
 \cline{2-7} 
 & 0.200 in 30~samples with pop.\ size 400 & 35.0  
 & 0.126 in 20~samples with pop.\ size 200 & 63.8       
 & 0.146 in 20~samples with pop.\ size 200 & 368.8 \\ 
 \hline
 \end{tabularx}

\bigskip
\normalsize
\renewcommand{\arraystretch}{2}
\caption{Original solution, with \texttt{\string\resizebox}}\label{label-three}
\resizebox{\textwidth}{!}{%
        \begin{tabular}{|c|c|c|c|c|c|c|}
        \hline
        \multirow{2}{*}{\textbf{Method}}
        & \multicolumn{2}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Non-frictional  Parallelepiped\end{tabular}}} 
        & \multicolumn{2}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Non-frictional  Knight\end{tabular}}} 
        & \multicolumn{2}{c|}{\textbf{\begin{tabular}[c]{@{}c@{}}Frictional  Knight\end{tabular}}} \\ 
        \cline{2-7} 
        & \multicolumn{2}{c|}{1880 Experimental Cases}                                                                    
        & \multicolumn{2}{c|}{21357 Experimental Cases}                                                           
        & \multicolumn{2}{c|}{21357 Experimental Cases}                                                       
        \\ \hline
        \multirow{2}{*}{\begin{tabular}[c]{@{}c@{}}Modified  NEW Method \end{tabular}} 
        & Final Quality & Time (s) 
        & Final Quality & Time (s)   
        & Final Quality & Time (s) \\ 
        \cline{2-7} 
        & \begin{tabular}[c]{@{}c@{}}0.200 in 30 samples  with pop size 400\end{tabular}        & 35.0            
        & \begin{tabular}[c]{@{}c@{}}0.126 in 20 samples  with pop. size 200\end{tabular}    & 63.8       
        & \begin{tabular}[c]{@{}c@{}}0.146 in 20 samples  with pop. size 200\end{tabular}  & 368.8    \\ 
        \hline
        \end{tabular}%
}
\end{table}
\end{document}

相关内容