在多列环境之前,在表格的第一列中换行

在多列环境之前,在表格的第一列中换行

我正在使用此代码片段创建一个多列表格。由于列标题很长,我将它们旋转了 90 度。

\begin{table}[H] 
\centering
\caption{Accuracy of Bayesian networks, only for setting 2. Values depict percentage of correct classification for the different ALSFRS values.}
\begin{tabular}{@{}  cl*{10}c @{}}
    \toprule
    & & \multicolumn{10}{}{} \\[2ex]
    & Learning algorithm & \rot{Speech} & \rot{Respiratory} & \rot{Salivation} & \rot{Swallowing} 
    & \rot{Handwriting} & \rot{Cutting} & \rot{\shortstack[l]{Dressing\\Hygiene}} 
    & \rot{\shortstack[l]{Turning\\in Bed}} & \rot{Walking} & \rot{\shortstack[l]{Climbing\\Stairs}} \\
    \cmidrule{2-12}
    & PC & $81.4$ & $82.3$ & $82.0$ & $82.2$ & $75.0$ & $78.8$ & $80.1$ & $77.3$ & $79.2$ & $82.3$ \\
    & RMCV (empty initialization) & $82.5$ & $83.2$ & $84.7$ & $82.4$ & $80.1$ & $79.7$ & $83.8$ & $82.9$ & $82.3$ & $83.7$ \\
    & RMCV (naive Bayes initialization) & $79.5$ & $74.5$ & $75.9$ & $78.3$ & $71.0$ & $71.6$ & $78.8$ & $73.3$ & $78.4$ & $77.8$ \\
    \cmidrule[1pt]{2-12}
\end{tabular}
\end{table} 

但是,现在我在前几项中有一些长文本,表格总是溢出页面。我想在第一列中换行,但我似乎找不到放置 p{Xcm} 命令的正确位置,而且我也不知道如何正确限制列的宽度。我对 还不熟悉LaTeX,所以这可能是一个新手问题,但我真的很感激有人能帮助我。谢谢!

答案1

在我看来,几乎所有人都非常不喜欢伸长脖子阅读旋转的内容——包括表格的标题单元格。幸运的是,没有必要让读者伸长脖子:只需转置表格的行和列即可。

在此处输入图片描述

\documentclass{article}
\usepackage{float}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup{labelfont=sc}
\usepackage{threeparttable,siunitx}
\usepackage{microtype}

\begin{document}

\begin{table}[H]
\centering
\begin{threeparttable}
    \caption{Accuracy of Bayesian networks, only for Setting~2. 
    \\Values depict percentage of correct classification for the different ALSFRS values.}
\begin{tabular}{@{} l *{3}{S[table-format=2.1]} }
\toprule
   & \multicolumn{3}{c@{}}{Learning algorithm}\\
   \cmidrule(l){2-4}
   & {PC} & {RMCV\,\tnote{a}} & {RMCV\,\tnote{b}} \\
\midrule 
Speech           & 81.4 & 82.5 & 79.5 \\
Respiratory      & 82.3 & 83.2 & 74.5 \\
Salivation       & 82   & 84.7 & 75.9 \\
Swallowing       & 82.2 & 82.4 & 78.3 \\
Handwriting      & 75   & 80.1 & 71   \\
Cutting          & 78.8 & 79.7 & 71.6 \\
Dressing Hygiene & 80.1 & 83.8 & 78.8 \\
Turning in Bed   & 77.3 & 82.9 & 73.3 \\
Walking          & 79.2 & 82.3 & 78.4 \\
Climbing Stairs  & 82.3 & 83.7 & 77.8 \\
\bottomrule
\end{tabular}
\begin{tablenotes}[flushleft]\small
      \item[a] Empty initialisation.
      \item[b] Naive Bayes initialization.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}

附录:如果您希望表格跨越文本块的整个宽度(并在标题单元格中放置更多信息,那么以下方法可能会引起您的兴趣。它使用环境tabularx以及X列类型的居中版本。

在此处输入图片描述

\documentclass{article}
\usepackage[labelfont=sc]{caption}
\usepackage{float,booktabs,tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X} % centered version of "X" column type
\newcommand\mc[1]{\multicolumn{1}{@{} C @{}}{#1}} % handy shortcut macro
\usepackage{microtype}
\begin{document}

\begin{table}[H]
\caption{Accuracy of Bayesian networks, Setting~2 only.}
Values denote fractions of correct classification for the different ALSFRS values, in percent.

\medskip
\begin{tabularx}{\textwidth}{@{} lccc @{}}
\toprule
   & \multicolumn{3}{c@{}}{Learning algorithm}\\
   \cmidrule(l){2-4}
   & \mc{PC} 
   & \mc{RMCV, Empty initialisation} 
   & \mc{RMCV, Naive Bayes initialization} \\
\midrule 
Speech           & 81.4 & 82.5 & 79.5 \\
Respiratory      & 82.3 & 83.2 & 74.5 \\
Salivation       & 82.0 & 84.7 & 75.9 \\
Swallowing       & 82.2 & 82.4 & 78.3 \\
Handwriting      & 75.0 & 80.1 & 71.0 \\ 
Cutting          & 78.8 & 79.7 & 71.6 \\
Dressing Hygiene & 80.1 & 83.8 & 78.8 \\
Turning in Bed   & 77.3 & 82.9 & 73.3 \\
Walking          & 79.2 & 82.3 & 78.4 \\
Climbing Stairs  & 82.3 & 83.7 & 77.8 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

答案2

我建议另一种方法:可以将括号中的初始化细节放在一个表格注释中,并附上包threeparttable。然后我们可以将表格调整为\linewidth带有tabularx,并使用makecell包,列头接受换行符:

\documentclass{article}
\usepackage{float}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup{labelfont=sc}
\usepackage{graphicx}
\newcommand{\rot}[1]{\rotatebox[origin=l]{90}{#1}}
\usepackage{siunitx}
\usepackage{array, tabularx,threeparttable, makecell}
\renewcommand\cellalign{lb}
\usepackage{ragged2e}
\usepackage{microtype}
\usepackage{rotating}

\begin{document}

\begin{table}[H]
  \centering\setlength{\tabcolsep}{5pt}\settowidth\rotheadsize{Handwriting}
  \begin{threeparttable}
    \caption{Accuracy of Bayesian networks, only for setting 2. Values depict percentage of correct classification for the different ALSFRS values.}
    \begin{tabularx}{\linewidth}{@{}X*{10}{c} @{}}
      \toprule%
      \makecell{Learning\\algorithm} & {\rotcell{Speech}} & {\rotcell{Respiratory}} & {\rotcell{Salivation}} & {\rotcell{Swallowing}} & {\rotcell{Handwriting}} & {\rotcell{Cutting}} & {\rotcell{Dressing\\Hygiene}}
      & {\rotcell{Turning\\in Bed}} & {\rotcell{Walking}} & {\rotcell{Climbing\\Stairs}} \\
      \midrule
      PC & 81.4 & 82.3 & 82.0 & 82.2 & 75.0 & 78.8 & 80.1 & 77.3 & 79.2 & 82.3 \\
      RMCV\,\tnote{a}
         & 82.5 & 83.2 & 84.7 & 82.4 & 80.1 & 79.7 & 83.8 & 82.9 & 82.3 & 83.7 \\
      RMCV\,\tnote{b}
         & 79.5 & 74.5 & 75.9 & 78.3 & 71.0 & 71.6 & 78.8 & 73.3 & 78.4 & 77.8 \\
      \bottomrule\addlinespace[0.5ex]
    \end{tabularx}
    \begin{tablenotes}[flushleft]\small
      \item[a] Empty initialisation.
      \item[b]Naive Bayes initialization.
    \end{tablenotes}
  \end{threeparttable}
\end{table}

\end{document} 

在此处输入图片描述

答案3

只需将第一列(c在您的例子中)替换为某个固定宽度的列,例如pmb。该列的宽度就在该说明符后面设置。由于您没有给出 MWE(尤其是 的定义\rot),我不得不猜测一下。我希望我得到了你想要的东西。

% arara: pdflatex

\documentclass{article}
\usepackage{float}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{graphicx}
\newcommand{\rot}[1]{\rotatebox[origin=l]{90}{#1}}
\usepackage{siunitx}
\usepackage{array}
\usepackage{ragged2e}
\usepackage{microtype}

\begin{document}
\begin{table}[H] 
    \centering
    \caption[you should use a short form for LoT!]{Accuracy of Bayesian networks, only for setting 2. Values depict percentage of correct classification for the different ALSFRS values.}
    \begin{tabular}{@{}>{\RaggedRight}b{1.5cm}*{10}{S[table-format=2.1]}@{}}
        \toprule
        Learning algorithm & {\rot{Speech}} & {\rot{Respiratory}} & {\rot{Salivation}} & {\rot{Swallowing} }
        & {\rot{Handwriting}} & {\rot{Cutting}} & {\rot{\parbox{1.4cm}{Dressing\\Hygiene}}} 
        & {\rot{\parbox{1.4cm}{Turning\\in Bed}}} & {\rot{Walking}} & {\rot{\parbox{1.4cm}{Climbing\\Stairs}}} \\
        \midrule
        PC & 81.4 & 82.3 & 82.0 & 82.2 & 75.0 & 78.8 & 80.1 & 77.3 & 79.2 & 82.3 \\
        RMCV (empty initialization) & 82.5 & 83.2 & 84.7 & 82.4 & 80.1 & 79.7 & 83.8 & 82.9 & 82.3 & 83.7 \\
        RMCV (naive Bayes initialization) & 79.5 & 74.5 & 75.9 & 78.3 & 71.0 & 71.6 & 78.8 & 73.3 & 78.4 & 77.8 \\
        \bottomrule
    \end{tabular}
\end{table} 
\end{document}

在此处输入图片描述

相关内容