# Combining tufte-latex and threeparttable

I am trying to get threeparttable to work with the tufte-book class. Naturally, when I use the follwing the caption will be placed in the margin. In this case, cleveref has no problems in getting the reference right (i.e. it correctly prints the "Table"):

\documentclass{tufte-book}

\usepackage{threeparttable}
\usepackage{cleveref}

\begin{document}

\begin{table}
\centering
\begin{threeparttable}
\caption{Table caption}
\begin{tabular}{cc}
Some & tabular \\
Contents & \ldots
\end{tabular}
\end{threeparttable}
\label{tab:foo}
\end{table}

See \cref{tab:foo}.

\end{document}


Now I want to put the caption above the table, similar to the requirement in this question.

Since it looks like redefining caption is not feasible, I tried to roll my own caption by defining (as a starting point):

\newcommand{\mycaption}[1]{\refstepcounter{table}Table~\thetable:~#1}


Using \mycaption{Table} instead of \caption{Table caption} in the above example yields:

This puts the caption in the right place, but some features are missing:

1. Centering the caption above the table if it is shorter than one line.
2. Getting the references right with \cref. (This could not be resolved even after rerunning the document several times.)
3. Adding the table to the list of tables.
4. Possibility of providing a short form of the caption for the list of tables as asecond argument.

Is there any way to either a) use the existing \caption functionality and simply change the position to above the tabular or b) extend my \mycaption command to behave just like \caption in the standard classes, i.e. meet requirements 1-4?

EDIT (see below for old answer):

Here is the improved version that I ended up using. I recommend to put it in the tufte-book-local.tex file. It defines a "classic caption style" that fulfils all the requirements posed in the question, and activates this style at the beginnin of each instance of threeparttable:

\RequirePackage{etoolbox}
\makeatletter
\newif\if@tufte@margtab\@tufte@margtabfalse
\AtBeginEnvironment{margintable}{\@tufte@margtabtrue}
\AtEndEnvironment{margintable}{\@tufte@margtabfalse}
\newcommand{\classiccaptionstyle}{%
\long\def\@caption##1[##2]##3{%
\par
{\protect\numberline{\csname the##1\endcsname}{\ignorespaces ##2}}%
\begingroup
\@parboxrestore
\if@minipage
\@setminipage
\fi
\normalsize
\@makecaption{\csname fnum@##1\endcsname}{\ignorespaces ##3}\par
\endgroup}
\long\def\@makecaption##1##2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{\@tufte@caption@font##1: ##2}%
\ifdim \wd\@tempboxa >\hsize
\@tufte@caption@font\if@tufte@margtab\@tufte@caption@justification\fi##1: ##2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}
%   \setcaptionfont{\normalfont}
\let\caption\@tufte@orig@caption%
\let\label\@tufte@orig@label}
\makeatother

\AtBeginEnvironment{threeparttable}{%
\classiccaptionstyle}


Digging through tufte-common.def I found that at the first instance of a float, the original definitions of \caption and \label are saved in the macros \@tufte@orig@caption and \@tufte@orig@label.

Since I could not patch the @tufte@float environment, I used etoolbox to restore the original caption and label commands at the beginning of each threeparttable environment:

    \usepackage{etoolbox}
\AtBeginEnvironment{threeparttable}{%
\let\caption\@tufte@orig@caption%
\let\label\@tufte@orig@label}
\makeatother


This yields the exact same behaviour as in the standard classes (optional argument possible, etc.) except for the alignment. I presume the alignment is different since tufte-altex defines \@makecaption differently than the standard classes.

I don't know if this method has any weird side effects, but so far it has worked for me.