# Modify the @tufte@margin@float environment to automatically align floats

The Tufte classes provide the @tufte@margin@float environment which is used for tables and figures, but its result it's not automatically aligned with the text. The \marginnote command provide a margin note that it's aligned with the text on wich you put your note on, but however, it does not take a marginfigure environment as input for example. So how can I modify this environment in order to have a figure that's automatically aligned with the text?

\documentclass[nofonts]{tufte-handout}

\usepackage{graphicx}

\newcommand*{\fig}
{
\marginnote
{
\includegraphics[width=\marginparwidth]{image2}
This is a margin figure.
}
}

\begin{document}

test\fig

\end{document}

• Which should be aligned with the text: the caption, the top of the figure, the bottom of the figure? – godbyk Jul 25 '14 at 1:19
• @godbyk you are right, I'm sorry, the top of the figure! As a the top of a margin note is aligned! – Aurelius Jul 25 '14 at 23:47

\makeatletter

\catcode\V=14 % V' is a comment character unless [verbose]

\def\FloatBarrier{\begingroup \let\@elt\relax
V\edef\@tempa{\write\m@ne{Package placeins Info: Float barrier, from
V  input line \the\inputlineno, processed on page \thepage, lands on
V  page \noexpand\thepage. }}\@tempa
\edef\@tempa{\@fb@botlist\@deferlist\@dbldeferlist}%
\ifx\@tempa\@empty V\PackageInfo{placeins}{No floats held,}%
\else
\ifx\@fltovf\relax % my indicator of recursion
\if@firstcolumn V\PackageWarning{placeins}{Some floats are stuck,}%
\clearpage
\else V\PackageInfo{placeins}{Eject a column and check again:}%
\null\newpage\FloatBarrier
\fi
\else V\PackageInfo{placeins}{Must dump some floats}%
\newpage \let\@fltovf\relax V\PackageInfo{placeins}{Check again:}%
\FloatBarrier % recurse once only
\fi\fi \endgroup
\@fb@topbarrier }

\catcode\V=11

\renewenvironment{@tufte@margin@float}[2][0pt]{%
\FloatBarrier% process all floats before this point so the figure/table numbers stay in order.
\begin{lrbox}{\@tufte@margin@floatbox}%
\begin{minipage}[t]{\marginparwidth}% top-aligned minipage
\@tufte@caption@font
\def\@captype{#2}%
\hbox{}\vspace*{#1}%
\@tufte@caption@justification
\@tufte@margin@par
\noindent
}{%
\end{minipage}%
\end{lrbox}%
\marginpar{\usebox{\@tufte@margin@floatbox}}%
}

%%
% Margin figure environment

\renewenvironment{marginfigure}[1][-7pt]%
{\begin{@tufte@margin@float}[#1]{figure}}
{\end{@tufte@margin@float}}

\makeatother


The only difference between this code and the code from the Tufte-LaTeX document classes is that the minipage environment in the new code is top-aligned as opposed to center-aligned and the default offset was changed from -1.2ex to 0pt.

• it works but it breaks the line of type on which I put the command! I think because of this \FloatBarrier – Aurelius Jul 27 '14 at 22:21
• @FormlessCloud: Yes, the \FloatBarrier macro will cause a line break. You can remove that from the definition and things will be okay. The \FloatBarrier macro flushes any pending floats before it typesets the marginfigure so that the figure numbers are kept in the proper order. You could write \FloatBarrier at the start of the paragraph containing your marginfigure if the float numbers are getting out of order. – godbyk Jul 28 '14 at 2:18
• I've updated your answer with a code that fix every problem. take a look at it end if you think it's a good idea accept my correction. I've just removed \par from \FloatBarrier and changed to -7pt the offset of the marginfigure environment... – Aurelius Jul 29 '14 at 0:15
• @FormlessCloud: I'm not familiar enough with the inner mechanisms of \FloatBarrier to know if the \par` is required or not. – godbyk Jul 29 '14 at 2:29