2

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?

enter image description here

4
  • The key is that it (or at least memoir) uses \marginpar to place the image (which is saved using lrbox). Commented Dec 20, 2016 at 14:15
  • See also tex.stackexchange.com/questions/292394/… Commented Dec 20, 2016 at 14:20
  • Could you put both tikzpictures into one \marginfigure? Two captions in one figure (or minipage) is no problem. Commented Dec 20, 2016 at 15:03
  • As far as I can see \marginpar cannot move stuff up, only down.
    – daleif
    Commented Dec 20, 2016 at 15:20

1 Answer 1

4

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}

full page

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .