# Problem with captions at top of page for tufte-latex

I'm using the tufte-latex layout, and running into problems with the double-sided mode. I found a proposed bug-fix here and applied it to the most recent version of tufte-latex available on github

% Define Tuftian float styles (with the caption in the margin)
% original \floatc@tufteplain command
%    \newcommand{\floatc@tufteplain}[2]{%
%      \begin{lrbox}{\@tufte@caption@box}%
%        \begin{minipage}[\floatalignment]{\marginparwidth}\hbox{}%
%          \@tufte@caption@font{\@fs@cfont #1:} #2\par%
%        \end{minipage}%
%      \end{lrbox}%
%      \smash{\hspace{\@tufte@caption@fill}\usebox{\@tufte@caption@box}}%
%    }

% bugfix? - http://code.google.com/p/tufte-latex/issues/detail?id=50
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\floatc@tufteplain}[2]{%
\begin{lrbox}{\@tufte@caption@box}%
\begin{minipage}[\floatalignment]{\marginparwidth}\hbox{}%
\@tufte@caption@font{\@fs@cfont #1:} #2\par%
\end{minipage}%
\end{lrbox}%
\@tufte@checkoddpage%
\ifthenelse{\boolean{@tufte@odd@page}}%
{\gsetboolean{@tufte@float@recto}{true}\@tufte@float@debug{Detected page: [recto/odd]}}%
{\gsetboolean{@tufte@float@recto}{false}\@tufte@float@debug{Detected page: [verso/even]}}%
\ifthenelse{\NOT\boolean{@tufte@symmetric}\OR\boolean{@tufte@float@recto}}{%
% asymmetric or page is odd, so caption is on the right
\smash{\hspace{\@tufte@caption@fill}\usebox{\@tufte@caption@box}}%
\@tufte@float@debug{Caption position: [right]}%
}{% symmetric pages and page is even, so caption is on the left
\smash{\hspace{-\@tufte@overhang}\usebox{\@tufte@caption@box}}%
\@tufte@float@debug{Caption position: [left]}%
}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\fs@tufteplain}{%
\def\@fs@cfont{\@tufte@caption@font}%
\let\@fs@capt\floatc@tufteplain%
\def\@fs@pre{}%
\def\@fs@post{}%
\def\@fs@mid{}%
\let\@fs@iftopcapt\iftrue%
}
\let\fs@tufteplaintop=\fs@tufteplain
\let\floatc@tufteplaintop=\floatc@tufteplain
\newcommand\floatc@tufteruled[2]{%
{\@fs@cfont #1} #2\par%
}
\newcommand\fs@tufteruled{%
\def\@fs@cfont{\@tufte@caption@font}%
\let\@fs@capt\floatc@tufteplain%
\def\@fs@pre{\hrule height.8pt depth0pt width\textwidth \kern2pt}%
\def\@fs@post{\kern2pt\hrule width\textwidth\relax}%
\def\@fs@mid{}%
\let\@fs@iftopcapt\iftrue%
}
\newcommand\fs@tufteboxed{%
\def\@fs@cfont{}%
\let\@fs@capt\floatc@tufteplain%
\def\@fs@pre{%
\hrule \hbox to\hsize{\vrule\kern3pt
\vbox{\kern3pt\box\@currbox\kern3pt}\kern3pt\vrule}\hrule}}
}%
\def\@fs@mid{\kern2pt}%
\def\@fs@post{}%
\let\@fs@iftopcapt\iftrue%
}
}{%
% Nothing to do
}
}


I commented out what I thought was the relevant section, and pasted in the code that I found. However, I'm not sure if I have commented out all of the relevant parts as my problem still exists. For example, here you can see the caption on the wrong side of the margin:

I've noticed that the problem only occurs with tables or figures that are positioned at the top of the page. Captions elsewhere in the page are fine. I have also found that running \clearpage does result in the offending caption and figure or table to move to the correct side of the page, but this is not my preferred way of solving the problem for something book length!

-

You might try the \forceversofloat macro inside your figure environment. It will bypass Tufte-LaTeX's autodetection and force the caption to appear to the left of the figure. There's also a \forcerectofloat macro that does the opposite. More information about the use of these macros may be found on pages 25–26 of the Tufte-LaTeX sample book.
Thanks @godbyk - I missed that advice in the sample book. It's unfortunate that it won't do it automatically but it's very useful to know how to fix it and a lot better than \clearpage. – celenius Dec 9 '11 at 20:55
@celenius: Normally it does do it automatically. However, at the top of the page it can sometimes get confused due to LaTeX's asynchronous page-breaking. When the figure was created, it was on the previous page. LaTeX then decided at the last minute to move it to the next page (after the figure had been typeset). Oftentimes, rerunning latex, pdflatex, or xelatex, will correct this but not always. As a last resort, using \forceversofloat and \forcerectofloat will tell Tufte-LaTeX to assume the figure is on the left- or right-hand page, respectively. – godbyk Dec 10 '11 at 2:00
@JakobH I'm afraid not. It relies on LaTeX placing the \marginpar in the right place. Near the tops of pages, LaTeX can misplace it because its output routine runs asynchronously with TeX's page builder. You might try the mparhack package to see if it solves the issue. (I haven't tried it with TL, so I don't know if it'll work or not.) – godbyk Apr 1 at 19:52