4

I have a table with lots of columns which will most of the available horizontal space in my page layout. And I would like to add a "note" of sorts to the bottom of the table, which:

  • Spans the width of the whole table but does not influence its width
  • Contains wrapped text, either by wrapping automatically (preferred) or with manually specified line breaks

This is as far as I've been able to reduce the problem so far:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{lcccc}
    {} & {(baseline)} & {case 1} & {case 2} & {case 3}{$^\ast$} \\
    \hline
    var 1 & 327 & 621 & 0.315 & 1189       \\
    var 2 & 1604 & 1395 & -0.224 & 1395     \\
    \hline
    \multicolumn{5}{l}{\footnotesize$^\ast$some explanation on what this means which did \newline not fit into the cell, or in the table width}
\end{tabular}
\end{document} 

This yields the following:

table with 5 columns and some text across the bottom. The last column is stretched to accommodate the text, and a space is missing where the code specified the \newline command

As you can see, \newline is happily ignored (note the missing space), and the last column is stretched to accommodate the text.

I've seen a suggestion to change the alignment to p{<some_width>}. If I do, it will indeed obey the \newline, but it will also impose whatever width I enter on the text, which either stretches the table or wraps it prematurely unless I guess "correctly".

I can of course iteratively adjust the width I'm specifying for the last column, but that is tedious, and I'm almost certain that there is a better way of achieving the following picture, than by changing the width parameter, millimeter by millimeter, to the largest value at which it does not affect the table width:

The text at the bottom now wraps where it should, at whatever width the table would have if the text was not present

=> How can I do this?

In case this is relevant: My full-size table also uses siunitx and booktabs for formatting, and maybe those offer some help. I removed them here to minimize the MWE.

I did try adding \multirow to the cell at the bottom, but that will only increase the height of the cell at the bottom and center the same single line of text vertically.

I'd be content to just have something which allows me to use \newline without specifying cell width, although of course automatic line wraps to "whatever width the tabular already has" would be preferred.

Also of interest: Why on earth is \newline only respected if cell width is also explicitly given? That makes precious little sense to me... I thought that the main use case for it was to specify a line break in order to allow narrower columns, and then let cell width adapt to the new line lengths. Or maybe it conflicts with multirow?

Edit: More things that don't really do it for me:

I could put two cells below the table and put part of the text in each of them, but then the line spacing would be messed up (One of these has wrapped text in a single cell, the other one has two cells, can you tell which is which?): Two almost identical tables side by side. The left one has two cells for the "footnote" with one line of text in each, and the other has one cell with a wrapped note in it. The one on the left has very awkwardly large line spacing for the note.

2
  • You mention "booktabs" in the title of your query, but your code does not load the booktabs package nor does it employ any of the user macros of that package. Please clarify.
    – Mico
    Commented Feb 9 at 19:56
  • 1
    @Mico I have a much larger document which uses booktabs. In order to get a smaller example code, I removed that here, and replaced the \midrule and \bottomrule commands with \hline. The original table is also a good deal more complex, but I've been told not to post big complicated things here if a simple example with fewer package requirements demonstrates the issue just as well. -- but if you have an idea how to solve it using booktabs, I'd be delighted since I already use it anyway.
    – Zak
    Commented Feb 9 at 20:01

3 Answers 3

3

I suggest you look into what the threeparttable package, its user environments (called threeparttable and tablenotes), and its user macro \tnote can do for you. Importantly, the width of the tablenotes environment is set to that of the associated tabular environment -- no need for any \newline directives.

enter image description here

\documentclass{article}
\usepackage{booktabs,siunitx}
\newcolumntype{T}{S[table-format=4.0]} % for 3 of the 4 data columns
\usepackage[flushleft]{threeparttable}

\begin{document}
\begin{center}
    \begin{threeparttable}
    
    \begin{tabular}{@{} l T T S[table-format=-1.3] T }
    \toprule
    & {(baseline)} & {case 1\tnote{$\dagger$}} & {case 2} & {case 3\tnote{*}} \\
    \midrule
    var 1 & 327 & 621 & 0.315 & 1189       \\
    var 2 & 1604 & 1395 & -0.224 & 1395     \\
    \bottomrule
    \end{tabular}

    \smallskip
    \footnotesize
    \begin{tablenotes}
    \item[$\dagger$] Some explanation of what this means which did not fit into the cell, but it fits below the table.
    \item[*]Some explanation of what this means which did not fit into the cell, but it fits below the table.
    \end{tablenotes}
    \end{threeparttable}
\end{center}
\end{document} 

Addendum: The OP has asked me to provide the following two points.

  • A threeparttable environment contains up to 3 formal elements: (a) A tabular-like environment; (b) a \caption directive; and (c) a tablenotes environment. The threeparttable machinery limits the widths of the caption and of the tablenotes material to that of the tabular-like environment.

  • To use a threeparttable environment inside a table environment, one should modify the code shown above as follows: One should change

    \begin{center}
        \begin{threeparttable}
    
        \begin{tabular}{...}
        ...
        \end{tabular}
    
        \smallskip\footnotesize
        \begin{tablenotes}
        ...
        \end{tablenotes}
        \end{threeparttable}
    \end{center}
    

    to

    \begin{table}
    \centering % note: '\centering', not '\begin{center}'
        \begin{threeparttable}
    
        \caption{...} % <-- new
    
        \begin{tabular}{...}
        ...
        \end{tabular}
    
        \smallskip\footnotesize
        \begin{tablenotes}
        ...
        \end{tablenotes}
    
        \end{threeparttable}
    
    \end{table}
    
11
  • That solves the MWE, but when I applied it to my big document, all the line spacings and cell heights were increased. I then pasted my table into the MWE code, and it looks fine. Then I pasted the MWE code into my big document, and line spacings are weird again. Simply switching the environment for any table to threeparttable in my document increases cell height and line spacing ... any clue where I should start looking?
    – Zak
    Commented Feb 9 at 20:47
  • @Zak - "Simply switching the environment for any table to threeparttable ..." -- Please note that I did not advocated switching to a threeparttable. Instead, I suggest encasing the tabular material, plus the explanatory material, inside a threeparttable environment.
    – Mico
    Commented Feb 9 at 22:18
  • I had my tabular inside a table environment which also contained a label and a caption. I then changed from table to threeparttable -- is that now how it should be used?
    – Zak
    Commented Feb 22 at 13:23
  • @Zak - Absolutely not. The sample code you posted did not feature either a table environment or a \caption directive; that's why my answer didn't feature those items either. If your real code does feature a table environment and a \caption directive (along with an associated \label directive), then you should definitely keep these items. What I advocated was to encase the tabular-like environment (along with the \caption statement and a newly-created tablenotes environment) inside a threeparttable environment which, in turn, should be contained in the table environment.
    – Mico
    Commented Feb 22 at 15:13
  • 1
    @Zak - Put differently, the threeparttable environment is not a substitute for a table environment -- or, for that matter, a tabular environment.
    – Mico
    Commented Feb 22 at 15:17
1

Since Mico did not like me adding more detail to his answer, I'll just post it here:

threeparttable is not a floating environment, and its "three parts" are table, notes and caption. So when using table floating environment, the threeparttable needs to be wrapped around just the tabular, the notes and (optionally, see below) the caption, like this:

\begin{table}   
    \centering
    %any other setup, e.g. `\sisetup` when using `siunitx`
    \begin{threeparttable}
        \begin{tabular}
            % ... the actual table, including `\tnote{}` tags
        \end{tabular}
        \footnotesize % optional, of course, otherwise notes have same text size as table content
        \begin{tablenotes}
            % explanations for the `\tnote{}` tags
        \end{tablenotes}
    \caption{the caption for the table}
    \end{threeparttable}
    \label{tab:tpt_example}
\end{table}
    

Although the package documentation states that the caption should be inside the threeparttable, I found that both inside or outside work, and both have use cases:

  • Keeping it inside will cause the caption to have the same width as the table but otherwise work the same as normal table captions.
  • Moving it behind or before the threeparttable environment causes its width to match the table floating environment instead. That may be useful in some cases, particularly with narrow tables, when the caption contains a bit more text and there is enough space left and right.
1
  • Regarding your second bullet point: If the tabular material is quite narrow and if it's desirable to let the width of the \caption material exceed that of the tabular material, this argument probably holds for the tablenotes material as well. And if it's desirable to keep both the caption and the tablenotes outside the threeparttable environment, there isn't a need to employ the threeparttable machinery to begin with.
    – Mico
    Commented Feb 26 at 12:59
1

The package nicematrix provides an environment {NiceTabular} with its own command \tabularnote to address that kind of situation.

\documentclass{article}
\usepackage{enumitem}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}
\begin{table}
\centering
\begin{NiceTabular}{lcccc}
   \toprule
   & (baseline) & case 1 & case 2 & case 3\tabularnote{some explanation on what this means which did not fit into
     the cell, or in the table width} \\ 
   \midrule
   var 1 & 327 & 621 & 0.315 & 1189 \\
   var 2 & 1604 & 1395 & -0.224 & 1395 \\
   \bottomrule
\end{NiceTabular}
\end{table}
\end{document} 

You need several compilations.

Output of the above code

You must log in to answer this question.

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