% threeparttable.sty   (or 3parttable) (or 3parttab on DOS)
% by Donald Arseneau   Updated on May 22, 2001.
% This file may be distributed, modified, and used in other works with just
% one restriction: modified versions must clearly indicate the modification
% (preferably by a name change).
% 
% This package facilitates tables with titles (captions) and notes. The
% title and notes are given a width equal to the body of the table (a
% tabular environment).  By itself, a threeparttable does not float, but
% you can put it in a {table} or a {table*} or some other environment.
% (This causes extra typing, but gives more flexibility.)
%
% Inside a threeparttable there should be a caption, followed by a tabular
% environment, possibly followed by a series of itemized "tablenotes".  At
% present, there is nothing automatic about the notes; you must specify the
% identifier in the body of the table ("\tnote{a}") and in the notes below
% ("\item[a]...").  I chose this method because automatic numbering with
% \footnote would be very hard to use, particularly because many tables
% make repeated reference to a single note.  If someone has a suggestion
% for a convenient, elegant, automatic system, I'll listen!  \tnote
% commands can be given in the caption too, and they will *NOT* appear in
% the list of tables. 
% 
% \begin{table}
%  \begin{threeparttable}
%   \caption{...}
%   \begin{tabular}...% or {tabular*}
%    ...\tnote{1}&....
%    ...
%   \end{tabular}
%   \begin{tablenotes}
%    \item [1] the first note
%    ...
%   \end{tablenotes}
%  \end{threeparttable}
% \end{table}

\edef\endtablenotes % make a scratch macro for restoring catcodes
  {\catcode\string `\string @=\the\catcode\string`\@
   \catcode\string `\string *=\the\catcode\string`\*}
\catcode`\@=11
\catcode`\*=11

\@ifundefined{@tempboxb}{\@nameuse{newbox}\@tempboxb}{}

\newenvironment{threeparttable}{% 3 parts: title, tabular environment, notes
\relax \ifx\\\@centercr \leavevmode \fi
\vbox\bgroup\sloppy
\@ifundefined{@captype}{\def\@captype{table}}{}%
\topsep\z@
\@enumdepth\z@
\global\let\TPT@hsize\@empty
\let\TPT@LA@caption\@caption \let\@caption\TPT@caption
\TPT@hookin\tabular\TPT@LA@tabular 
\TPT@hookarg\tabular*\TPT@LA@tabular*
\TPT@hookarg\tabularx\TPT@LA@tabularx
\let\TPTrlap\relax}%
{\egroup}

% "hookin" hooks into tabular to set the alignment in a box,
% measure the box.  It gets inserted after the \begingroup
% and does the work after the \endgroup produced by \end.
%
% That's no good: I want \tabular...\endtabular to work.
%
% Try wrapping definitions around both \tabular and \endtabular?
% That's not good because I want them unhooked for nesting,
% which deactivates the \endtabular hook.  This can be handled
% with complicated macros.
%
% What I had originally was a hook via \everyhbox.

\def\TPT@hookin#1#2{\let#2= #1\relax
 \TPT@unhook=\expandafter{\the\TPT@unhook \let#1=#2\relax}%
 \def#1{\the\TPT@unhook
  \setbox\@tempboxb\hbox\bgroup \begingroup \aftergroup\TPT@endtab
  \let\TPTrlap\rlap #2}}%

\def\TPT@endtab{\egroup
 \xdef\TPT@hsize{\hsize\the\wd\@tempboxb \parindent 1em
   \noexpand\@parboxrestore}\TPT@hsize
 \ifx\TPT@docapt\@undefined\else
  \TPT@docapt \vskip.2\baselineskip
 \fi \par \box\@tempboxb 
 \ifvmode \prevdepth\z@ \fi
 \endgroup \TPT@hsize \@ignoretrue}

\newtoks\TPT@unhook \TPT@unhook={}


\def\TPT@hookarg#1#2{\let#2= #1%
 \TPT@unhook=\expandafter{\the\TPT@unhook \let#1=#2}%
 \def#1##1{\the\TPT@unhook
   \gdef\TPT@hsize{\setlength\hsize{##1}\parindent 1em
   \@parboxrestore}\TPT@hsize
 \ifx\TPT@docapt\@undefined\else
   \TPT@docapt \vskip.2\baselineskip
 \fi \par \aftergroup\TPT@hsize
 #2{##1}}}%

\gdef\TPT@hsize{}

\def\TPT@caption#1[#2]#3{\gdef\TPT@docapt
 {\par\global\let\TPT@docapt\@undefined \TPT@LA@caption{#1}[{#2}]{#3}}%
 \ifx\TPT@hsize\@empty\else \TPT@docapt \fi \ignorespaces}

\def\tablenotes{\par \prevdepth\z@ \TPT@hsize 
\list{}{\topsep\z@skip \partopsep\z@skip 
\itemsep\z@ \parsep\z@ \itemindent\z@ \leftmargin1.5em
%  for superscript enumeration, use:
\def\makelabel##1{\tnote{##1}\hfil}\labelwidth .5em\labelsep .3em%
%  for on-the-line enumeration use:
% \labelwidth 1em\labelsep .5em%
\relax}}

\def\tnote#1{\protect\TPTrlap{$^{\fam\z@ #1}$}}% 
\def\TPTrlap#1{} % notes in caption will disappear in list of tables!


\endtablenotes % restore catcode of @ and * to starting value

\let\endtablenotes\endlist % and assign proper meaning for \endtablenotes!

\endinput

Example:

Here is some paragraph before the table.  Note that this table does not
float because it is not in a {table} or {table*} environment.

\begin{center}
\begin{threeparttable}
\caption{The Skewing Angles ($\beta$) for $\fam0 Mu(H)+X_2$ and
   $\fam0 Mu(H)+HX$~\tnote{a}}
\begin{tabular}{rlcc}
\hline
&   & $\fam0 H(Mu)+F_2$ & $\fam0 H(Mu)+Cl_2$ \\
\hline
&$\beta$(H)  & $80.9^\circ\tnote{b}$ & $83.2^\circ$ \\
&$\beta$(Mu) & $86.7^\circ$ & $87.7^\circ$ \\
\hline
\end{tabular}
\begin{tablenotes}
\item[a] for the abstraction reaction, $\fam0 Mu+HX \rightarrow MuH+X$.
\item[b] 1 degree${} = \pi/180$ radians.
\end{tablenotes}
\end{threeparttable}

\end{center}

Notes:  

May 15, 2001: changed hook-in to use \setbox in a wrapper macro (or
explicit width of tabular*).  I don't remember why I used \everyhbox
and \lastbox before.  Something may break now.  At least it works with
tabularx.

