[Tex/LaTex] threeparttablex table notes for tabu tabulars with less than \textwidth

notestablestabu

threeparttablex can be used to add table notes to tabu tabulars*. If the tabular spans \textwidth, the output is as expected. It seems however that the tablenotes environment does not adjust its width if the tabular is narrow, although of course this would be desirable.

1) Is there any option/command which is intended to automatically adjust the tablenotes environment's width?

2) If not, is there a workaround?

@ 2) According to this answer, threeparttablex does know the tabular's width. Moreover, given that for the threeparttable package, which threeparttablex supposedly builds upon, the package documentation states "you can redefine the whole tablenotes environment", it seems that it should be possible to find a workaround.

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabu}
\usepackage{threeparttablex}

\begin{document}    
\centering
\begin{ThreePartTable}
    \begin{tabu} to .4\textwidth {XX}
        a & b   \\\toprule
        0 & 1   \\\bottomrule
    \end{tabu}
    \begin{tablenotes}
        \footnotesize
        \item[*] This is a long table note text, long enough to exceed the table's width.
    \end{tablenotes}
\end{ThreePartTable}
\end{document}

*threeparttablex is designed to extend threeparttable to longtable. Even though the manual does not mention it, threeparttablex works with tabu, which threeparttable does not. Replacing threeparttablex's ThreePartTable environment in the above code with a threeparttable environment gives an "Extra }, or forgotten \endgroup." error.

Best Answer

I don't know how to do it with threeparttablex, but it seems that the old threeparttable should be sufficient: the former is for longtable and you don't seem to be using longtabu (I'm afraid that one should rewrite threeparttablex for using the longtabu environment).

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabu}
\usepackage{threeparttable}

\usepackage{xpatch}
\makeatletter
\chardef\TPT@@@asteriskcatcode=\catcode`*
\catcode`*=11
\xpatchcmd{\threeparttable}
  {\TPT@hookin{tabular}}
  {\TPT@hookin{tabular}\TPT@hookin{tabu}}
  {}{}
\catcode`*=\TPT@@@asteriskcatcode
\makeatother

\begin{document}
\centering
\begin{threeparttable}
    \begin{tabu} to .4\textwidth {XX}
        a & b   \\\toprule
        0 & 1   \\\bottomrule
    \end{tabu}
    \begin{tablenotes}
        \footnotesize
        \item[*] This is a long table note text, long enough to exceed the table's width.
    \end{tablenotes}
\end{threeparttable}

\end{document}

enter image description here


If you don't have xpatch, then change the line

\xpatchcmd{\threeparttable}

into

\expandafter\patchcmd\csname\string\threeparttable\endcsname

and load etoolbox instead of xpatch.


How does the patch work? There is, in threeparttable.sty a macro called \TPT@hookin which wants as argument the name of the table making environment. The threeparttable environment executes \TPT@hookin{tabular}, \TPT@hookin{tabular*} and \TPT@hookin{tabularx}, so we simply want to add \TPT@hookin{tabu}.

There's a small complication in the fact that Donald Arsenau changes the category code of * to 11 when defining \threeparttable. The patching macros provided by etoolbox, xpatch or even regexpatch all disassemble the command to patch and rebuild it in order to see if there's something with category codes that would make the patched macro behave differently from what's expected, so we need to take care of that change: the * is set to category code 11 and than its original category code is restored.