% Macro for more comfortable treating pictures (pictures placed
% on left or right side of page are imbedded into paragraphs, pictures
% that are centered can be imbedded between lines of paragraph).
% For more details see the end of this file.
%
% Oldrich Ulrych                                     July 30, 1992, Prague
%
%
\edef\catcodeat{\the\catcode`\@ }     \catcode`\@=11
%
\newbox\p@ctbox                       % box for the current picture to be placed in
\newbox\t@mpbox                       % box for temporary storing
\newbox\@uxbox                        % auxiliary box
\newbox\s@vebox                       % stack of scanned pictures
%
\newtoks\desct@ks \desct@ks={}        % stack for of descriptions under pictures
\newtoks\@ppenddesc                   % toks for appended local description
\newtoks\sh@petoks                    % token for description of picture
%
\newif\ifallfr@med  \allfr@medfalse   % true if all picture are being fr@med
\newif\if@ddedrows                    % true if round brackets appeared
\newif\iffirstp@ss  \firstp@ssfalse   % flag for paragraph test
\newif\if@mbeeded                     % flag to indicate just to copy picture
\newif\ifpr@cisebox                   % flag to copy picture in natural width
\newif\ifvt@p                         % inserted picture is \vtop
\newif\ifvb@t                         % inserted picture is \vbot
\newif\iff@nished    \f@nishedtrue    % text is around the picture
\newif\iffr@med                       % picture will be framed
\newif\ifj@stbox     \j@stboxfalse    % only framed box is shown
%
\newcount\helpc@unt                   % auxiliary counter
\newcount\p@ctpos                     % position of the picture (0-l,1-c,2-r)
%
\newdimen\r@leth      \r@leth=0.4pt   % thickness of rules
\newdimen\x@nit                       % x-size of user's horizontal units
\newdimen\y@nit                       % y-size of user's vertical units
\newdimen\xsh@ft                      % x-shift of user's coordinates
\newdimen\ysh@ft                      % y-shift of user's coordinates
\newdimen\@uxdimen                    % auxiliary dimension
\newdimen\t@mpdimen                   % auxiliary dimension
\newdimen\t@mpdimeni                  % auxiliary dimension
\newdimen\b@tweentandp                % marginal space between text and picture
\newdimen\b@ttomedge                  % bottom edge of the picture on page
\newdimen\@pperedge                   % upper edge of the picture on page
\newdimen\@therside                   % space on the outer side of picture
\newdimen\d@scmargin                  % space around marks inside picture
\newdimen\p@ctht                      % height+depth of picture
\newdimen\l@stdepth
\newdimen\in@tdimen
\newdimen\l@nelength
\newdimen\re@lpictwidth
%
\def\justframes{\global\j@stboxtrue}  % only framed empty place is shown
\def\picturemargins#1#2{\b@tweentandp=#1\@therside=#2\relax}
\def\allframed{\global\allfr@medtrue} % all picturew will be fr@med
\def\emptyplace#1#2{\pl@cedefs        % the empty vbox will be created
    \setbox\@uxbox=\vbox to#2{\n@llpar% all paragraph values are reset
        \hsize=#1\vfil \vrule height0pt width\hsize}% vbox to prescribed dimensions
    \e@tmarks}
\def\boxplace{\pl@cedefs\afterassignment\re@dvbox\let\n@xt= }
\def\re@dvbox{\setbox\@uxbox=\vbox\bgroup% all is stored in vbox
         \n@llpar\aftergroup\e@tmarks}
\def\fontcharplace#1#2{\pl@cedefs     % picture is in font
    \setbox\@uxbox=\hbox{#1\char#2\/}%
    \xsh@ft=-\wd\@uxbox               % width of character and shift of origin
    \setbox\@uxbox=\hbox{#1\char#2}%
    \advance\xsh@ft by \wd\@uxbox     % horizontal shift of the origin
    \helpc@unt=#2
    \advance\helpc@unt by -63         % fontdimen code-63: size of user's x-unit
    \x@nit=\fontdimen\helpc@unt#1%
    \advance\helpc@unt by  20         % fontdimen code-43: size of user's y-unit
    \y@nit=\fontdimen\helpc@unt#1%
    \advance\helpc@unt by  20         % fontdimen code-23: y-shift of origin
    \ifnum\helpc@unt<51
      \ysh@ft=-\fontdimen\helpc@unt#1%
    \fi
    \e@tmarks}
\def\n@llpar{\parskip0pt \parindent0pt% paragraph values are reset
    \leftskip=0pt \rightskip=0pt
    \everypar={}}
\def\pl@cedefs{\xsh@ft=0pt\ysh@ft=0pt}% by default user's origin is not shifted
\def\e@tmarks#1{\setbox\@uxbox=\vbox{ % reset the box with picture
      \n@llpar
      \hsize=\wd\@uxbox               % to preserve its width
      \noindent\copy\@uxbox           % the box itself
      \kern-\wd\@uxbox                % jump to the left margin
      #1\par}%                        % mark into the picture
    \st@redescription}
\def\t@stprevpict#1{\ifvoid#1\else    % if previous picture is not finished
   \errmessage{Previous picture is not finished yet.}\fi} % then error message

\def\st@redescription#1\par{%         % puts picture's description on the stack
    \global\setbox\s@vebox=\vbox{\box\@uxbox\unvbox\s@vebox}%
    \desct@ks=\expandafter{\the\desct@ks#1\@ndtoks}}%remembers the description
%\def\@ndtoks{}
\def\def@ultdefs{\p@ctpos=1         % picture is centered by default
      \def\lines@bove{0}%           % no full lines above picture by default
      \@ddedrowsfalse               % no extra full lines read by default
      \@mbeededfalse                % paragraph flows around picture by default
      \pr@ciseboxfalse
      \vt@pfalse                    % output box will be vbox
      \vb@tfalse                    % output box will be vbox
      \@ppenddesc={}%               % no append to description
      \ifallfr@med\fr@medtrue\else\fr@medfalse\fi
      }

\def\descriptionmargins#1{\global\d@scmargin=#1\relax}
\def\@dddimen#1#2{\t@mpdimen=#1\advance\t@mpdimen by#2#1=\t@mpdimen}
\def\placemark#1#2 #3 #4 #5 {\unskip    % ignore previous spaces
      \setbox1=\hbox{\kern\d@scmargin#5\kern\d@scmargin}% form the mark
      \@dddimen{\ht1}\d@scmargin        % add some space above mark
      \@dddimen{\dp1}\d@scmargin        % add some space below mark
      \ifx#1l\dimen3=0pt\else           % the correction
        \ifx#1c\dimen3=-0.5\wd1\else
          \ifx#1r\dimen3=-\wd1
     \fi\fi\fi
     \ifx#2u\dimen4=-\ht1\else          % and @dd the correction
       \ifx#2c\dimen4=-0.5\ht1\advance\dimen4 by 0.5\dp1\else
         \ifx#2b\dimen4=0pt\else
           \ifx#2l\dimen4=\dp1
     \fi\fi\fi\fi
     \advance\dimen3 by #3%             % x position
     \advance\dimen4 by #4%             % y position
     \advance\dimen4 by-\dp1
     \advance\dimen3 by \xsh@ft         % influence of shifting of the origin
     \advance\dimen4 by \ysh@ft         % influence of shifting of the origin
     \kern\dimen3\vbox to 0pt{\vss\copy1\kern\dimen4}% put the mark
     \kern-\wd1                        % jump back of the width of mark
     \kern-\dimen3                     % jump back of the x position
     \ignorespaces}                    % ignore following spaces
\def\fontmark #1#2 #3 #4 #5 {\placemark #1#2 #3\x@nit{} #4\y@nit{} {#5} }
\def\fr@msavetopict{\global\setbox\s@vebox=\vbox{\unvbox\s@vebox
      \global\setbox\p@ctbox=\lastbox}%
    \expandafter\firstt@ks\the\desct@ks\st@ptoks}
\def\firstt@ks#1\@ndtoks#2\st@ptoks{%
    \global\desct@ks={#2}%
    \def\t@mpdef{#1}%
    \@ppenddesc=\expandafter\expandafter\expandafter
                        {\expandafter\t@mpdef\the\@ppenddesc}}
\def\testf@nished{{\let\s@tparshape=\relax
    \s@thangindent}}
\def\inspicture{\t@stprevpict\p@ctbox
    \def@ultdefs                  % set default values
    \fr@msavetopict
    \iff@nished\else\testf@nished\fi
    \iff@nished\else
      \immediate\write16{Previes picture is not finished yet}%
    \fi
    \futurelet\N@xt\t@stoptions}  % look ahead
\def\t@stoptions{\let\n@xt\@neletter% scans parameters
  \ifx\N@xt l\p@ctpos=0\else                % picture on left inside par
   \ifx\N@xt c\p@ctpos=1\else               % picture copied centered
    \ifx\N@xt r\p@ctpos=2\else              % picture on right inside par
     \ifx\N@xt(\let\n@xt\e@tline\else        % number of full row above
      \ifx\N@xt!\@mbeededtrue\else           % just copy the picture
       \ifx\N@xt|\fr@medtrue\else            % picture will be framed
        \ifx\N@xt^\vt@ptrue\vb@tfalse\else  % picture as vtop
         \ifx\N@xt_\vb@ttrue\vt@pfalse\else % picture as vbot
          \ifx\N@xt\bgroup\let\n@xt\@ddgrouptodesc\else
           \let\n@xt\@dddescription % all parameters are read
  \fi\fi\fi\fi\fi\fi\fi\fi\fi\n@xt}
\def\e@tline(#1){\def\lines@bove{#1}% define number of full rows above picture
    \@ddedrowstrue
    \futurelet\N@xt\t@stoptions}
\def\@neletter#1{\futurelet\N@xt\t@stoptions} % eats one parameter
\def\@ddgrouptodesc#1{\@ppenddesc={#1}\futurelet\N@xt\t@stoptions}
\def\fr@medpict{\setbox\p@ctbox=
    \vbox{\n@llpar\hsize=\wd\p@ctbox
       \iffr@med\else\r@leth=0pt\fi
       \ifj@stbox\r@leth=0.4pt\fi
       \hrule height\r@leth \kern-\r@leth
       \vrule height\ht\p@ctbox depth\dp\p@ctbox width\r@leth \kern-\r@leth
       \ifj@stbox\hfill\else\copy\p@ctbox\fi
       \kern-\r@leth\vrule width\r@leth\par
       \kern-\r@leth \hrule height\r@leth}}
\def\@dddescription{\fr@medpict     %  adds description to the picture
    \re@lpictwidth=\the\wd\p@ctbox
    \advance\re@lpictwidth by\@therside
    \advance\re@lpictwidth by\b@tweentandp
    \ifhmode\ifinner\pr@ciseboxtrue\fi\fi
    \createp@ctbox
    \let\N@xt\tr@toplacepicture
    \ifhmode                         % if inside paragraph then use \vadjust
      \ifinner\let\N@xt\justc@py
      \else\let\N@xt\vjustc@py
      \fi
    \else
      \ifnum\p@ctpos=1               % if centered then just copy the box
        \let\N@xt\justc@py
      \fi
    \fi
    \if@mbeeded\let\N@xt\justc@py\fi % if forced to copy, just copy the box
    \firstp@sstrue
    \N@xt}
\def\createp@ctbox{\global\p@ctht=\ht\p@ctbox
    \advance\p@ctht by\dp\p@ctbox
    \advance\p@ctht by 6pt
    \setbox\p@ctbox=\vbox{%        % make new vbox
      \n@llpar                     % reset par's parameters
      \t@mpdimen=\@therside          % and set the leftskip and rightskip
      \t@mpdimeni=\hsize             %  according to the position of picture
      \advance\t@mpdimeni by -\@therside
      \advance\t@mpdimeni by -\wd\p@ctbox
      \ifpr@cisebox
        \hsize=\wd\p@ctbox
      \else
        \ifcase\p@ctpos
               \leftskip=\t@mpdimen    \rightskip=\t@mpdimeni
        \or    \advance\t@mpdimeni by \@therside
               \leftskip=0.5\t@mpdimeni \rightskip=\leftskip
        \or    \leftskip=\t@mpdimeni   \rightskip=\t@mpdimen
        \fi
      \fi
      \hrule height0pt             % invisible rule and nobreak penalty
      \kern6pt                     % small space above
      \penalty10000
      \noindent\copy\p@ctbox\par     % put the picture with marks
      \kern3pt                       % small space between pict and description
      \hrule height0pt
      \hbox{}%
      \penalty10000
      \interlinepenalty=10000
      \the\@ppenddesc\par            % follows text of description
      \penalty10000                  % prohibite page break
      \kern3pt                       % small space below
      }%
      \ifvt@p
       \setbox\p@ctbox=\vtop{\unvbox\p@ctbox}%
      \else
        \ifvb@t\else
          \@uxdimen=\ht\p@ctbox
          \advance\@uxdimen by -\p@ctht
          {\vfuzz=\maxdimen
           \global\setbox\p@ctbox=\vbox to\p@ctht{\unvbox\p@ctbox}%
          }%
          \dp\p@ctbox=\@uxdimen
        \fi
      \fi
      }
\def\picname#1{\unskip\setbox\@uxbox=\hbox{\bf\ignorespaces#1\unskip\ }%
      \hangindent\wd\@uxbox\hangafter1\noindent\box\@uxbox\ignorespaces}
\def\justc@py{\ifinner\box\p@ctbox\else\kern\parskip\unvbox\p@ctbox\fi
  \global\setbox\p@ctbox=\box\voidb@x}
\def\vjustc@py{\vadjust{\kern0.5\baselineskip\unvbox\p@ctbox}%
      \global\setbox\p@ctbox=\box\voidb@x}
\def\tr@toplacepicture{%             % tries to find the good place
      \ifvmode\l@stdepth=\prevdepth  % remember the depth of last box
      \else   \l@stdepth=0pt         % otherwise the depth of last box is zero
      \fi
      \vrule height.85em width0pt\par% to define exact totalpage
      \r@memberdims                  % remembers the significant dimensions
      \global\t@mpdimen=\pagetotal
      \t@stheightofpage              % tests if the picture fits here
      \ifdim\b@ttomedge<\pagegoal    % if the picture ends on the current page
         \let\N@xt\f@gurehere        % the picture can be placed here
         \global\everypar{}%         % clear up the everypar token
      \else
         \let\N@xt\relax             % we must wait till the new page
         \penalty10000
         \vskip-\baselineskip        % but jump back to the reference point
         \vskip-\parskip             % of the last line of the previous par
         \immediate\write16{Picture will be shifted down.}%
         \global\everypar{\sw@tchingpass}%  % check the begin of every paragraph
      \fi
      \penalty10000
      \N@xt}
\def\sw@tchingpass{%                 % on every odd pass t@sts the possibility
    \iffirstp@ss                     %   to place picture here
      \let\n@xt\relax
      \firstp@ssfalse                % on every even pass does nothing
    \else
      \let\n@xt\tr@toplacepicture
      \firstp@sstrue
    \fi  \n@xt}
\def\r@memberdims{\global\in@tdimen=0pt
    \ifnum\p@ctpos=0
        \global\in@tdimen=\re@lpictwidth
      \fi
      \global\l@nelength=\hsize
      \global\advance\l@nelength by-\re@lpictwidth
      }
\def\t@stheightofpage{%
     \global\@pperedge=\t@mpdimen
     \advance\t@mpdimen by-0.7\baselineskip % jump back to the begin of par
     \advance\t@mpdimen by \lines@bove\baselineskip % and down by full lines
     \advance\t@mpdimen by \ht\p@ctbox      % and down by the height of new box
     \advance\t@mpdimen by \dp\p@ctbox      % and down by the depth of new box
     \advance\t@mpdimen by-0.3\baselineskip % and down by the depth of last box
     \global\b@ttomedge=\t@mpdimen          % and remember bottom edge
     }
\def\f@gurehere{\global\f@nishedfalse
      \t@mpdimen=\lines@bove\baselineskip   % and remember the amount of used
      \advance\t@mpdimen-0.7\baselineskip   %   space
      \kern\t@mpdimen
      \advance\t@mpdimen by\ht\p@ctbox
      \advance\t@mpdimen by\dp\p@ctbox
      {\t@mpdimeni=\baselineskip
       \offinterlineskip
       \unvbox\p@ctbox
       \global\setbox\p@ctbox=\box\voidb@x
       \penalty10000   \kern-\t@mpdimen     % and jump back to the reference
       \penalty10000   \vskip-\parskip      % point of the last line of previous
       \kern-\t@mpdimeni                    % paragraph
%       \hbox{\vrule height\l@stdepth width0pt}%
      }%
      \penalty10000                         % prevent page breaking
      \global\everypar{\s@thangindent}%     % and mark that paragraph is special
      }
\def\s@thangindent{%
    \ifdim\pagetotal>\b@ttomedge\global\everypar{}%
      \global\f@nishedtrue             % if we continue under the picture
      \else
        \advance\@pperedge by -1.2\baselineskip
        \ifdim\@pperedge>\pagetotal\global\everypar{}%
          \global\f@nishedtrue
        \else
          \s@tparshape                 % if we continue around the picture
        \fi
        \advance\@pperedge by 1.2\baselineskip
      \fi}
\def\s@tparshape{\t@mpdimen=-\pagetotal% minus upperedge of the picture
   \advance\t@mpdimen by\b@ttomedge    % plus bottomedge of the pictured
   \divide\t@mpdimen by\baselineskip   % divided by baselineskip gives the
   \helpc@unt=\t@mpdimen               %  number of shorter lines
   \advance \helpc@unt by 2            % rounding correction and one line more
   \sh@petoks=\expandafter{\the\helpc@unt\space}% \sh@petoks is for \parshape
   \t@mpdimeni=\lines@bove\baselineskip
   \t@mpdimen=\pagetotal
   \gdef\lines@bove{0}%              % in next paragraphs no lines are full
   \loop \ifdim\t@mpdimeni>0.999\baselineskip % counts the full lines
     \advance\t@mpdimen  by \baselineskip
     \advance\t@mpdimeni by-\baselineskip
     \sh@petoks=\expandafter{\the\sh@petoks 0pt \the\hsize}%
   \repeat
   \loop \ifdim\b@ttomedge>\t@mpdimen         % counts the shorter lines
     \advance\t@mpdimen by \baselineskip
     \sh@petoks=\expandafter{\the\sh@petoks \in@tdimen \l@nelength }%
   \repeat
   \sh@petoks=\expandafter
      {\the\sh@petoks 0pt \the\hsize}%        % and next lines are full again
   \expandafter\parshape\the\sh@petoks
   }

\descriptionmargins{2pt}
\picturemargins{15pt}{0pt}

\catcode`\@=\catcodeat        \let\catcodeat=\undefined

\endinput
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Short syntax of control sequences:
%
% \allframed  all following pictures will be framed by thin line
%
% \picturemargins{<dimen1>}{<dimen2>}  sets the space between text
%      of paragraph and picture to the value <dimen1> and space on the
%      other side of picture to the value <dimen2>. Default values are
%      15pt and 0pt. These values are taken into account only when
%      pictures are placed inside paragraphs.
%
% \descriptionmargins{<dimen>}  sets the space around marks to the value
%      <dimen> (so the mark seems a little bigger and doesn't stick to the
%      point where placed (if placed by point on its border). Default
%      value is 1pt.
%
% \justframes means that all following pictures will be shown only by
%      the outlines.
%
% Control sequences that create object and put it onto the stack of pictures
%
% \emptyplace{<width><whatever>}{<height>}{<marks>}<description>\par
%
% \boxplace{<vbox>}{<marks>}<description>\par
%
% \fontcharplace{<font>}{<char>}{<marks>}<description>\par
%      <width>   is the width of free place
%      <whatever> material placed into created vbox
%      <height>  is the height of free place
%      <vbox>    the vbox with picture
%      <font>    is the cs of font with picture
%      <char>    number of char with picture (65,66,...
%      <marks>   are either \placemark's with their parameters
%                or \fontmark's with their parameters (in the case of
%                \fontcharplace)
%      <description> is text that will be placed under the picture
%                as the description of the picture
%
% \placemark <ph><pv> <xdim> <ydim> <mark>
%
% \fontmark  <ph><pv> <xrel> <yrel> <mark>
%      <ph>     one of the letter  l, c, r  indicates that the left
%               margin, center or right margin (after adding value
%               determined by \descriptionmargins) will be placed into
%               the prescribed position.
%      <pv>     one of the letter  u, c, b, l  indicates that the upper
%               edge, center, bottom line or lower edge (after adding value
%               determined by \descriptionmargins) will be placed into
%               the prescribed position.
%       <xdim> or <ydim> are dimensions describing the position of
%               mark with respect to the lower left corner of
%               picture (this corner can be seen when you use
%               \allframed at the begin of your file)
%       <xrel> or <yrel> are relative dimensions describing the
%               position of mark with respect to the origin of user's
%               coordinate system (as specified in METAFONT macro
%               INCPIC.MF). It works only in \fontcharplace and for
%               pictures that are created by METAFONT using macro INCPIC.MF.
%       <mark>  (terminated by space) is the text of mark.
%
% \picname{<title>}  the <title> is set in bold face font in front of
%       the <description> from the left margin of the picture and the
%       <description> is indented by the width of the title. (This macro
%       could be used in the description of the picture.)
%
% Control sequences take the picture from the stack and put it into text.
%
% \inspicture lcr(<n>)!|^_{<description>} takes the first picture from
%      the stack and puts it into the text. All parameters are optional
%      and their meaning is:
%      l           the picture is placed on the left side of the pages
%      c           the picture is centered (this is default)
%      r           the picture is placed on the right side of the pages
%      <n>         number of full rows (default is 0)
%                  above the picture when the picture is placed on the
%                  left or right side of page.
%       !          forces to copy the picture here. In this case the
%                  options lrc(<n>) are ignored.
%       |          picture will be framed by thin line
%       ^          the picture forms \vtop (influnces the aligning with
%                  surrounding text.
%       _          the picture forms \vbox with zero depth
%                  (influnces the aligning with surrounding text.
%       When \inspicture is used inside a paragraph, it is
%       recomended to put empty group after its last parameter.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
