2

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!

3 Answers 3

3

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

I suggest another approach: the details for initialisation in brackets can be put a table notes, with the threeparttable package. Then we can adjust the table to \linewidth with tabularx, and using the makecell package, colmumn heads accept line breaks:

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

enter image description here

1
  • Perfect, thank you. I like the addition of the notes as well, looks much better. Thanks a lot for your help!
    – JonGor
    Aug 25, 2015 at 14:39
1

Just replace the first column (c in your case) by some fixed width column like p, m or b. The width of that column is set just behind that specifier. As you did not give an MWE (especially the definition of \rot), I had to guess around a bit. I hope, I got what you want.

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

enter image description here

2
  • That looks very good, thanks. Just another question, so that next time I can post my questions better: What do you mean by MWE and the definition of \rot? In any case, thanks a lot for your help
    – JonGor
    Aug 25, 2015 at 14:40
  • @JonGor Glad you like it. About \rot: I had to guess what you mean by this, as this command does not exist. Just take a look on all the lines the answerers have typed in order to reproduce the output of your snippet. Regarding the MWE: meta.tex.stackexchange.com/q/228
    – LaRiFaRi
    Aug 25, 2015 at 14:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .