[Tex/LaTex] Wrap text in the first column of a table, before a multicolumn environment

multicolumntableswrap

I'm using this code snippet to create a table with multicolumn. Since the headers of the columns are long, I've rotated them 90 degrees.

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

However, now I have some long text in the first entries, and the table keeps spilling out of the page. I want to wrap the text in the first column, but I can't seem to find the right place to put the p{Xcm} command, and I also don't know how to correctly limit the width of the column. I'm kind of new to LaTeX, so maybe this is a newbie question, but I'd really appreciate some help. Thanks!

Best Answer

It is my impression that almost all people strongly dislike having to crane their necks to read rotated material -- including header cells of a table. Fortunately, there's no need to make your readers crane their necks: Just transpose the table's rows and columns.

enter image description here

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

Addendum: if you prefer having the table spanning the full width of the text block (and placing more information in the header cells, then the following approach may be of interest to you. It uses a tabularx environment along with a centered version of the X column type.

enter image description here

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