% \filename{mfigmacros.tex}
% \edited{last by stolfi on Wed Sep  2 00:16:38 1987}

% Marginal figures

\catcode`@=11\endlinechar=-1

  %  Poor man's marginal figures

    %  Use \rfig5cm by 3cm{\flower}{Figure 1: A Boojum}{Text...},
    %  Use \lfig5cm by 3cm{\flower}{Figure 1: A Boojum}{Text...}
    %    for a figure 5 cm high and 3cm wide on the right/left margin,
    %    with the given legend typeset by its sideat the bottom,
    %    and the given text on the other side.
    
    \long\def\rfig#1by#2#{\mf@g{R}{#1}{#2}}
    \long\def\lfig#1by#2#{\mf@g{L}{#1}{#2}}

    % Paramters to \mf@g:
    %   #1 = L or R
    %   #2 = height
    %   #3 = width
    %   #4 = figure contents --> \box0
    %   #5 = caption --> \box2
    %   #6 = text by side --> \box4
    
    \long\def\mf@g#1#2#3#4#5#6{\relax
      \ifomitfigures
        #6
      \else
        \begingroup
          \ifhmode\def\mf@gzz{\noindent\strut}\else\def\mf@gzz{}\fi
          \dimen@=#3\relax 
          \setbox4\vbox{
            \dimen2=\dimen@ \advance\dimen2 by 2pt
            \if R#1 \rightglue=\dimen2\else \leftglue=\dimen2\fi
            \mf@gzz #6\strut
          }
          \dimen2=#3\relax
          \advance\dimen2 by -2\textindentglue
          \setbox2\hbox to \dimen2{
            \vbox{\hsize=\dimen2
              \leftglue=0pt\rightglue=0pt
              \parindent=0pt
              \smallsize #5
            }
          }
          \ifdim 1\dp2>1\dp4 \dp4=\dp2\fi
          \dp2=0pt
          \dimen@=#3\relax
          \setbox0\hbox to \hsize{
            \hss
            \vrule height 0pt depth 1\dp4 width 0pt
            \valign{##\cr
              \vrule height 0.85\ht4 depth 0pt width 0pt\cr
              \vfil
                \ifcaptionsonly\else
                  \hbox to \dimen@{
                    \hss
                    \vbox to #2{
                      \hsize=\dimen@
                      \rightglue=0pt\leftglue=0pt
                      #4
                    }
                    \hss
                  }
                  \vfil
                \fi
                \ifdim 1\ht2>0pt
                  \vskip2ex plus0.5ex
                  \hbox to \dimen@{\hss\box2\hss}
                  \vskip 1.0ex
                \fi
              \cr
            }
          }
          \ht0=0pt
          \interlinepenalty 10000
          \ifhmode
            \unskip\unskip\unskip
            \vadjust{\unvbox4
              \nointerlineskip
              \penalty10000\kern-\dp0\penalty10000
              \box0
            }
            \null
            \hbreak\required
          \else
            \vskip3\parsep % Mystery. Why 3?
            \unvbox4
            \nointerlineskip
            \penalty10000\kern-\dp0\penalty10000
            \box0
          \fi
        \endgroup
      \fi}

\catcode`@=12\endlinechar=`\^^M

% END
