# Margin figure sometime looks awkward

I'm typesetting my document with tufte-book class which has an environment called marginfigure. it's good but sometime it doesn't fit my picture in document well. for example in this MW:

% !TEX TS-program = xelatex
% !TEX encoding = UTF-8
\documentclass{biditufte-book}

\usepackage{ptext}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,arrows.meta,decorations.markings}

\usepackage[localise=on]{xepersian}
\settextfont{Times New Roman}
\setdigitfont{Times New Roman}

\begin{document}
\chapter{مشاهده‌‌پذیری}
\ptext[1-4]
\section{مقدمه}
\begin{marginfigure}
\centering
\begin{tikzpicture}[domain=-2:2]
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\coordinate (o) at (0,0);
\coordinate (a) at (1,{sin(1 r)});
\coordinate (b) at (-1,{sin(-1 r)});
\draw[dashed] (a|-o) node[below] {$x$} -- (a) -- (a-|o) node[left] {$f(x)$};
\draw[dashed] (b-|o) node[right] {$-f(x)$} -- (b) -- (b|-o) node[above] {$-x$};
\draw[color=blue] plot (\x,{sin(\x r)});
\end{tikzpicture}
\caption{ نمونه ای از یک تابع فرد}
\end{marginfigure}
\begin{marginfigure}
\centering
\begin{tikzpicture}[domain=-2:2]
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\coordinate (o) at (0,0);
\coordinate (a) at (1,{cos(1 r)});
\coordinate (b) at (-1,{cos(-1 r)});
\draw[dashed] (a|-o) node[below] {$x$} -- (a) -- (a-|o);
\draw[dashed] (b-|o) node[above] {$f(x)$} -- (b) -- (b|-o) node[below] {$-x$};
\draw[color=blue] plot (\x,{cos(\x r)});
\end{tikzpicture}
\caption{ نمونه ای از یک تابع زوج}
\end{marginfigure}
\section{مقدمه}
\ptext[1]

\end{document}


My picture is not completely in page. Is there a way of preventing that happening?

• The key is that it (or at least memoir) uses \marginpar to place the image (which is saved using lrbox). – John Kormylo Dec 20 '16 at 14:15
• – John Kormylo Dec 20 '16 at 14:20
• Could you put both tikzpictures into one \marginfigure? Two captions in one figure (or minipage) is no problem. – John Kormylo Dec 20 '16 at 15:03
• As far as I can see \marginpar cannot move stuff up, only down. – daleif Dec 20 '16 at 15:20

Macro \marginpar is fed a minipage by \marginfigure which causes the center of the figure to be aligned to the baseline of the text. This means that the bottom of the figure can protrude below the text area. One can raise it using \raisebox, but one must first determine how much space is left on the page.

This can be done several ways, but the easiest is to use tikzpagenodes.

Note: I am unable to get biditufte-book to work, so am using tufte-book instead.

\documentclass{tufte-book}
\usepackage{tikzpagenodes}

\usepackage{showframe}% MWE only
\usepackage{lipsum}% MWE only

\newlength{\DistanceToBottom}
\newcommand{\GetDistanceToBottom}{\tikz[remember picture,overlay]{%
\pgfextracty{\DistanceToBottom}{\pgfpointdiff{\pgfpointanchor{current page text area}{south}}%
{\pgfpointorigin}}%
\global\DistanceToBottom=\DistanceToBottom}}

\makeatletter
\renewenvironment{@tufte@margin@float}[2][-1.2ex]%
{\FloatBarrier% process all floats before this point so the figure/table numbers stay in order.
\begin{lrbox}{\@tufte@margin@floatbox}%
\begin{minipage}{\marginparwidth}%
\@tufte@caption@font%
\def\@captype{#2}%
\hbox{}\vspace*{#1}%
\@tufte@caption@justification%
\@tufte@margin@par%
\noindent%
}
{\end{minipage}%
\end{lrbox}%
\marginpar{\GetDistanceToBottom
\ifdim\DistanceToBottom>\dp\@tufte@margin@floatbox
\usebox{\@tufte@margin@floatbox}%
\else
\raisebox{\dimexpr \dp\@tufte@margin@floatbox-\DistanceToBottom}%
{\usebox{\@tufte@margin@floatbox}}%
\fi
}}
\makeatother

\begin{document}

\noindent\rule{1em}{40\baselineskip}
\begin{marginfigure}
\rule{\marginparwidth}{20\baselineskip}
\end{marginfigure}

\the\DistanceToBottom\par
\lipsum[1-3]

\end{document}