# threeparttablex table notes for tabu tabulars with less than \textwidth

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.

-

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}


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.

-
Thanks, problem solved. I've added the reason for having used threeparttablex to the question. Your patch does interfere with other threeparttablex tables (now: narrow table notes for wide tables), however, since besides the note issue the patch does make threeparttable work with tabu, I replaced threeparttablex. I'd be interested in what exactly the patch does, could you give a hint on that? –  dgs May 20 '12 at 8:20
Thanks again, @egreg. –  dgs May 20 '12 at 10:32
Great answer! Perhaps it should be mentioned that loading the caption` package before the patch results in compilation errors. Loading it afterwards though, seems to be ok though. –  pmav99 Jun 19 '13 at 11:32