如何完美地将文本放入列中

如何完美地将文本放入列中

我正在尝试制作一个表格,其中的文本超出了标题列的边界,并且第一列中包含超长的文本。 \tiny使其太小。我希望文本完全适合标题和第一列的文本,我不介意它占用额外的行。此外,我想使数据条目垂直居中。

以下是我迄今为止尝试过的:

    \footnotesize
    \begin{tabularx}{\columnwidth}{|X|X|X|X|X|X|X|}
         \hline
          Learning Algorithms & Train=50\% Test=50\% Outlier Removal=No & Train=66\% Test=34\% Outlier Removal = No & Train=50\% Test=50\% Outlier Removal=1428 & Train=66\% Test=34\% Outlier Removal=1428 & Train=50\% Test=50\% Outlier Removal=958 & Train=66\% Test=34\% Outlier Removal=958 \\
          \hline
          Linear Regression Train RMSE & 5616.62 & 5622.72 & 217.92 & 274.63 & 138.74 & 204.55\\
          \hline
          Test RMSE & 7035.95 & 7619.36 & 1134.16 & 252.82 & 57532.62 & 185.13\\
          \hline
          RandomForest Regressor Train RMSE & 4.05 & 4.20 & 10.04 & 10.620 & 2.15 & 6.60\\
          \hline
          Test RMSE & 1860 & 2137.75 & 745.95 & 255.290 & 626.78 & 147.36\\
          \hline
          GradientBoosting Regressor Train RMSE & 429.40 & 448 & 37.17 & 51.54 & 11.83 & 26.53\\
          \hline
          Test RMSE & 2010.69 & 2002.19 & 804.25 & 251.78 & 621.26 & 222.77\\
          \hline

    \end{tabularx}
  
    \caption{Fault Prediction Comparison Table Using Different Algorithms and Train-Test Ratio}

\end{table}

非常欢迎新的建议。

谢谢

在此处输入图片描述

这是我在 Word 中制作的同一张表格,非常好。在此处输入图片描述

答案1

那么,下面的重新设计如何通过删除重复来避免列标题中的长文本呢?我还删除了大部分行,以使表格看起来更加开放和吸引人:

在此处输入图片描述

\documentclass{report}
\usepackage{tabularx,siunitx, caption}
\usepackage{booktabs}

\begin{document}
\begin{table}
\caption{Fault Prediction Comparison Table using various Algorithms and Train-Test Ratios}
\sisetup{group-digits=false,
         table-format=4.2,
         round-mode=places,
         round-precision=2}
\begin{tabularx}{\columnwidth}{  X  *{3}{S} 
                   S[table-format=3.2] 
                   S[table-format=5.2] 
                   S[table-format=3.2]  }
 \toprule
\multicolumn{1}{r@{}}{Train:}           & {50\%}   & {66\%} & {50\%}  & {66\%}  & {50\%}   & {66\%} \\
\multicolumn{1}{r@{}}{Test:}            & {50\%}   & {34\%} & {50\%}  & {34\%}  & {50\%}   & {34\%} \\
\multicolumn{1}{r@{}}{Outlier Removal:} & {No}     & {No}   & {1428}  & {1428}  & {958}    & {958}  \\
 \midrule
\multicolumn{5}{l}{Linear Regression}\\ \addlinespace
 Train RMSE                             & 5616.62 & 5622.72 &  217.92 & 274.63  &   138.74 & 204.55 \\
 Test RMSE                              & 7035.95 & 7619.36 & 1134.16 & 252.82  & 57532.62 & 185.13 \\
 \midrule
\multicolumn{5}{l}{RandomForest Regressor}\\ \addlinespace
 Train RMSE                             &  4.05   &    4.20 &   10.04 &  10.620 &     2.15 &   6.60 \\
 Test RMSE                              & 1860    & 2137.75 &  745.95 & 255.290 &   626.78 & 147.36 \\
 \midrule
\multicolumn{5}{l}{GradientBoosting Regressor}\\ \addlinespace
 Train RMSE                             &  429.40 &  448    &   37.17 &  51.54  &    11.83 &  26.53 \\
 Test RMSE                              & 2010.69 & 2002.19 &  804.25 & 251.78  &   621.26 & 222.77 \\
 \bottomrule
\end{tabularx}
\end{table}
\end{document}

这是使用垂直线和水平线的不同版本的表格,同时仍然删除了列标题中的重复内容:

在此处输入图片描述

\documentclass{report}
\usepackage{tabularx,siunitx, caption}
\usepackage{makecell}

\begin{document}
\begin{table}
\caption{Fault Prediction Comparison Table using various Algorithms and Train-Test Ratios}
\sisetup{group-digits=false,
         table-format=4.2,
         round-mode=places,
         round-precision=2}
\renewcommand{\theadfont}{\normalsize}
\setlength{\tabcolsep}{5.2pt}
\begin{tabularx}{\columnwidth}{ | X|  *{3}{S|} 
                   S[table-format=3.2] |
                   S[table-format=5.2]| 
                   S[table-format=3.2]|  }
 \hline
\multicolumn{1}{|r|}{Train:}           & {50\%}   & {66\%} & {50\%}  & {66\%}  & {50\%}   & {66\%} \\
\multicolumn{1}{|r|}{Test:}            & {50\%}   & {34\%} & {50\%}  & {34\%}  & {50\%}   & {34\%} \\
\multicolumn{1}{|r|}{Outlier Removal:} & {No}     & {No}   & {1428}  & {1428}  & {958}    & {958}  \\
 \hline
\thead[l]{Linear\\ Regression}&&&&&&\\ 
 Train RMSE                             & 5616.62 & 5622.72 &  217.92 & 274.63  &   138.74 & 204.55 \\
 Test RMSE                              & 7035.95 & 7619.36 & 1134.16 & 252.82  & 57532.62 & 185.13 \\
 \hline
\thead[l]{RandomForest\\ Regressor}&&&&&&\\ 
 Train RMSE                             &  4.05   &    4.20 &   10.04 &  10.620 &     2.15 &   6.60 \\
 Test RMSE                              & 1860    & 2137.75 &  745.95 & 255.290 &   626.78 & 147.36 \\
 \hline
\thead[l]{GradientBoosting\\ Regressor}&&&&&&\\ 
 Train RMSE                             &  429.40 &  448    &   37.17 &  51.54  &    11.83 &  26.53 \\
 Test RMSE                              & 2010.69 & 2002.19 &  804.25 & 251.78  &   621.26 & 222.77 \\
 \hline
\end{tabularx}
\end{table}
\end{document}

答案2

我的主要建议是修改列类型的属性X,以启用表格单元格中第一个单词的连字符,并放弃完全对齐,转而采用右对齐外观。或者,考虑将六个数据列中的数字按小数点对齐。

在此处输入图片描述

\documentclass[twocolumn]{report}
\usepackage{tabularx,ragged2e,siunitx,caption}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X} % modified 'X' col. type
\newcolumntype{C}{>{\Centering\hspace{0pt}}X} % modified 'X' col. type
\renewcommand\tabularxcolumn[1]{m{#1}} % vertical centering of cells
\newcommand\mC[1]{\multicolumn{1}{C|}{#1}} % handy shortcut macro

\begin{document}
\begin{table}
\captionsetup{font=footnotesize}
\scriptsize
\setlength\tabcolsep{1pt} % default: 6pt
\setlength\extrarowheight{2pt}
\sisetup{group-digits=false,
         table-format=4.2, % default format
         round-mode=places,
         round-precision=2}
\begin{tabularx}{\columnwidth}{ | L | 
              *{3}{S |} 
                   S[table-format=3.2] |
                   S[table-format=5.2] |
                   S[table-format=3.2] | }
 \hline
 Learning Algorithms & 
 \mC{Train\hspace{1sp}=50\%; Test\hspace{1sp}=50\%; Outlier Removal\hspace{1sp}=No} & 
 \mC{Train\hspace{1sp}=66\%; Test\hspace{1sp}=34\%; Outlier Removal\hspace{1sp}=No} & 
 \mC{Train\hspace{1sp}=50\%; Test\hspace{1sp}=50\%; Outlier Removal\hspace{1sp}=1428} & 
 \mC{Train\hspace{1sp}=66\%; Test\hspace{1sp}=34\%; Outlier Removal\hspace{1sp}=1428} & 
 \mC{Train\hspace{1sp}=50\%; Test\hspace{1sp}=50\%; Outlier Removal\hspace{1sp}=958}  & 
 \mC{Train\hspace{1sp}=66\%; Test\hspace{1sp}=34\%; Outlier Removal\hspace{1sp}=958} \\
 \hline
 Linear Regression Train RMSE          & 5616.62 & 5622.72 &  217.92 & 274.63 &   138.74 & 204.55\\
 \hline
 Test RMSE                             & 7035.95 & 7619.36 & 1134.16 & 252.82 & 57532.62 & 185.13\\
 \hline
 RandomForest Regressor Train RMSE     &    4.05 &    4.20 &   10.04 &  10.620&     2.15 &   6.60\\
 \hline
 Test RMSE                             & 1860    & 2137.75 &  745.95 & 255.290&   626.78 & 147.36\\
 \hline
 GradientBoosting Regressor Train RMSE &  429.40 &  448    &   37.17 &  51.54 &    11.83 &  26.53\\
 \hline
 Test RMSE                             & 2010.69 & 2002.19 &  804.25 & 251.78 &   621.26 & 222.77\\
 \hline
\end{tabularx}
\caption{Fault Prediction Comparison Table using various Algorithms and Train-Test Ratios}
\end{table}
\end{document}

答案3

@leandriis 答案中第一个例子的变体:

  • 对于表使用tabularray
  • 行组由虚线分隔
\documentclass{report}
\usepackage{ragged2e}
\usepackage[skip=1ex,
            font=footnotesize,
            labelfont=bf]{caption}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\usepackage{lipsum}

\begin{document}
\lipsum[1]
    \begin{table}[ht]
\caption{Fault Prediction Comparison Table using various Algorithms and Train-Test Ratios}
\label{tab:tblr}
    \small
    \sisetup{group-minimum-digits=4}
\begin{tblr}{colspec ={@{} X[l,m] *{3}{S[table-format=4.2]}
                                       S[table-format=3.2]
                                       S[table-format=5.2]
                                       S[table-format=3.2]
                       @{} },
             }
    \toprule
\SetCell{r} Train:  
        & {{{\qty{50}{\%}}}}  
                    & {{{\qty{66}{\%}}}}
                                & {{{\qty{50}{\%}}}}
                                            & {{{\qty{66}{\%}}}}
                                                        & {{{\qty{50}{\%}}}}
                                                                    & {{{\qty{66}{\%}}}}    \\
\SetCell{r} Test:   
        & {{{\qty{50}{\%}}}}  
                    & {{{\qty{34}{\%}}}}
                                & {{{\qty{50}{\%}}}}
                                            & {{{\qty{34}{\%}}}}
                                                        & {{{\qty{50}{\%}}}}
                                                                    & {{{\qty{34}{\%}}}}    \\
\SetCell{r} Outlier Removal: 
        & {{{No}}}            
                    & {{{No}}}  & 1428      & 1428      & 958       & 958           \\
    \midrule
\SetCell[c=7]{l,font=\bfseries}    Linear Regression:           &   &   &   &   &   \\
Train RMSE          
        & 5616.62   & 5622.72   &  217.92   & 274.63    & 138.74    & 204.55        \\
Test RMSE                             
        & 7035.95   & 7619.36   & 1134.16   & 252.82    & 57532.62  & 185.13        \\
    \hline[dashed]
\SetCell[c=7]{l,font=\bfseries}    Random Forest Regressor:     &   &   &   &   &   \\
Train RMSE     
        & 4.05      & 4.20      & 10.04     & 10.620    & 2.15      & 6.60          \\
Test RMSE                             
        & 1860      & 2137.75   & 745.95    & 255.290   & 626.78    & 147.36        \\
    \hline[dashed]
\SetCell[c=7]{l,font=\bfseries}    Gradient-Boosting Regressor: &   &   &   &   &   \\
Train RMSE 
        &  429.40   &  448      & 37.17     & 51.54     & 11.83     & 26.53         \\
Test RMSE                             
        & 2010.69   & 2002.19   & 804.25    & 251.78    & 621.26    & 222.77        \\
    \bottomrule
\end{tblr}
\end{table}
\lipsum[2]
\end{document}

在此处输入图片描述

相关内容