% special figures designed for web pages by Anthony Phan.
% file: webfig.tex (testfile, example for using epsf.tex's commands)
% last modification: June 7, 2000.

\input epsf

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% A FAIRLY COMPLEX WAY TO INSERT A BOX IN ONE OR MANY PARAGRAPHS.
%
% SYNTAX:
%
% \insertbox[ optional arguments separated by commas ]
%
% OPTIONAL ARGUMENTS:
%
%	box=...		Some box, or whatever.
%	align=...	something. If it begins with an ``l'',
%			the box will be flushed left, else it'll
%			be flushed right.
%	skip=...	Number of lines to be skipped before
%			insertion. Beware: if this number is
%			positive and if the paragraph is quite
%			long, room would be left empty. Use it
%			with short paragraphs.
%	height=...	Number of lines that the figure should
%			occupy. If not set, it is automatically
%			computed. Setting it by hands may be
%			nice in case of vertical skips. 
% PARAMETERS:
% 
%	\insertboxhsep	space left blank aside the box
%	\insertboxvsep	minimum space left blank above and
%			below the box.
%
% SEE SAMPLES BELOW.
%
% WARNING:
%
% Effects are global. They may go through
% groups and environments. Parameters are
% restored at the end of the job, i.e.
% like they where at its begining.
%
\catcode`\@=11 % private macros
%
\newcount\ins@height
\newcount\ins@skip
\newdimen\ins@dimen
\newdimen\insertboxhsep
\insertboxhsep=1 em  % default value (choose yours).
\newdimen\insertboxvsep
\insertboxvsep=2pt   % default value (choose yours).
%
% Reading informations from input (optional arguments).
%
\def\ins@set@box#1box=#2,#3\@@{\setbox0\hbox{#2}}
\def\ins@set@height#1height=#2,#3\@@{\global\ins@height=#2 }
\def\ins@set@skip#1skip=#2,#3\@@{\global\ins@skip=#2 }
%
% Inserting the box.
%
\def\ins@insert#1align=#2#3\@@{\if#2l
	\setbox0\hbox{\unhbox0\kern\insertboxhsep}%
	\leftline{\vbox to 0pt{\vskip\skip@\copy0\vss}}%
	\global\ins@dimen=\wd0
	\else
	\setbox0\hbox{\kern\insertboxhsep\unhbox0}%
	\rightline{\vbox to 0pt{\vskip\skip@\copy0\vss}}%
	\global\ins@dimen=-\wd0
	\fi}
%
% Main command.
%
\def\insertbox[#1]{\ifhmode\par\fi
	\dimen@=\prevdepth
	\ins@set@box    #1, box=\null, \@@ % by default box is \null.
	\ins@set@height #1, height=0,  \@@ % by default height is checked.
	\ins@set@skip   #1, skip=0,    \@@ % by default skip is 0 lign.
	\ins@dimen=\dp0
	\advance\ins@dimen by \ht0
	\ht0=0pt \dp0=\ins@dimen
	%
	% if height (number of lines) is not given
	% or if it is less than zero, then it is
	% automatically computed.
	%
	\ifnum\ins@height=0
		\advance\ins@dimen by 2\insertboxvsep
		\ins@height=\ins@dimen
		\divide\ins@height by \baselineskip
		\global\advance\ins@height by 1
	\fi
	%
	% Vertical skip for the placement of the box.
	%
	\skip@\baselineskip
	\multiply\skip@ by \ins@skip
	\ins@dimen=\baselineskip
	\multiply\ins@dimen by \ins@height
	\advance\ins@dimen by -\dp0
	\divide\ins@dimen by 2
	\advance\skip@ by \ins@dimen
	\advance\skip@ by 0.25\baselineskip% fine tuning ?.
	%
	% Insertion.
	%
	\nointerlineskip
	\nobreak
	\ins@insert  #1  align=r  \@@ % by default alignment is right.
	\nobreak
	\prevdepth=\dimen@
	%
	% Changes for paragraphs output.
	%
	\ifx\ins@par@save\undefined
 	  \global\let\ins@par@save=\par
	  \gdef\par{\ins@par@save\ins@next}%
	  \global\let\ins@next=\relax
	  \global\edef\ins@parameters@save{%
	    \noexpand\global\noexpand\tolerance=\the\tolerance
	    \noexpand\global\noexpand\hfuzz=\the\hfuzz
	    \noexpand\global\noexpand\hbadness=\the\hbadness
	    \noexpand\global\noexpand\interlinepenalty=\the\interlinepenalty}%
	  \global\tolerance=2000 \global\hfuzz=2pt
	  \global\hbadness=2000 \global\interlinepenalty=\@M
	\else\message{Warning:
		too many insertions in some common paragraphs
		on page \the\pageno}\fi
	\global\everypar={%
		\ifnum\ins@skip>0
		\global\hangindent\ins@dimen
		\global\hangafter\ins@skip
		\gdef\ins@next{\nobreak
		\global\advance\ins@skip by -\prevgraf
		\ifnum\ins@skip<0
		\global\advance\ins@height by\ins@skip\fi}%
		\else
		\global\hangindent\ins@dimen
		\global\hangafter-\ins@height
		\gdef\ins@next{\global\advance\ins@height by -\prevgraf
			\ifnum\ins@height<0
			\allowbreak
			\global\let\par=\ins@par@save
			\global\everypar={}%
			\global\let\ins@par@save=\undefined
			\ins@parameters@save
			\else\nobreak\fi}\fi}}
%
\catcode`\@=12 % no more private macros
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SOME SECTIONING COMMANDS THAT MAY INTEREST NOBODY
%
\catcode`@=11
%
% page setup
%
\magnification=1200
\voffset=0.12 true cm
\hoffset=0.31 true cm
\vsize=24.2 true cm
\hsize=15.3 true cm
\font\nineit=cmti9
\font\twelverm=cmr12
%
% headings
%
\newif\iftitle
\titlefalse
\footline={\hfil}
\headline={\iftitle\hfil\global\titlefalse
	\ifodd\pageno\advance\pageno by 1\fi
	\else\vbox{\line{\ifodd\pageno
		{\nineit\@title}\hfil\llap{\tenrm\folio}
		\else\rlap{\tenrm\folio}\hfill{\nineit\@title}%
		\fi}\vskip6pt\hrule}\fi}
%
% Sectionning
%
\def\begintitle{\bgroup
	\def\title##1{\gdef\@title{##1}}%
	\def\subtitle##1{\def\@subtitle{##1}}%
	\def\author##1{\def\@author{##1}}%
	\title{}\subtitle{}\author{}}
\def\@title{}
%
\def\endtitle{\global\titletrue\parindent=0pt
	\leavevmode
	\ifx\@title\undefined\else{\twelverm\@title}\par\fi
	\vskip 6pt
	\hrule height 6pt
	\vskip 6pt
	\ifx\@subtitle\undefined\else\hfill{\twelverm\@subtitle}\par\fi
	\ifx\@author\undefined\else\hfill{\rm Author: \@author}\par\fi
	\hfill{\rm Last version: \today}\par
	\egroup
	\bigbreak}
%
\def\section#1{\bigbreak\noindent{\bf#1}\par\nobreak\medskip\nobreak}
\def\subsection#1{\medbreak\noindent{\bf#1\unskip}\enspace\ignorespaces}
\long\def\skippingtext#1\endskipping{\relax}
\catcode`@=12
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day,\space\number\year}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\frenchspacing
\parindent=1.5em
\nopagenumbers

\begintitle
\title{Webfig.tex}
\subtitle{How to insert a box in a text.}
\author{Anthony Phan}
\endtitle

\section{The best way to go}

The easiest and the best way to insert a picture
or any kind of large box (array, boxed formula)
is to do so within a display math environment
like the following:
$$
\vcenter{\hsize=100pt \epsfbox{webfig.10}
	\centerline{\it Figure 1.}}
\qquad
\vcenter{\hsize=200 pt% hand tuned
\it
This picture explains how the Bourbaki's Z
may be constructed. It relies on very basic
mathematical knowledge. But it's still a
bit tricky.}
$$
that is to say {\tt\$\$\it\ boxed material\/ \tt\$\$}.
Any other solution would require:

\item{$\bullet$}
a very high skill in \TeX\ programming;

\item{$\bullet$}
a very careful handling of paragraphs.

\noindent
But, alas, people love special effects.
They want text moving around a complex shape,
square boxes within a paragraph. And they want
it to be simple to do. But of course that's
complex because \TeX\ was not really designed
for that, even if it has primitives---like
{\tt\string\parshape}---that help to do such a job
(by hand tuning).
One has to notice the existence of very powerful
commands ({\tt\string\topinsert}, {\tt\string\pageinsert},
{\tt\string\midinsert}) in plain\TeX\ (see the \TeX book).

\section{The awful thing}

While I was trying to design a \TeX\ interpreter
for HTML (I know it sounds dummy), I faced the problem
of implementing the {\tt<img>}'s tag which allows
a very flexible insertion of pictures (in fact blank boxes).
The main problem was how to make it go through many paragraphs.
A part of the solution is given in a very nice book:
{\it La ma\^\i trise de \TeX}\/ by T.~Lachand-Robert.
With a little work more ($\ldots$) I could write
down a quite complex macro that would behave
as {\tt<img>}. (I you don't
know anything about HTML, you have to know that
it's a lot simpler to use than La\TeX.)

I'm not going to explain how it works (see the
mentioned book). I'm not going also to tell
anybody that my macro is wonderfull (it's not).
It may be interesting only to \TeX perts.
If you think you are, read the code of this file.
What is sure is that a good solution of this
kind is needed for complex formats. It is
also needed for those strange people
who don't know where to put their huge
pictures and who want to save some space.
To end those claims, I must say that the following
samples have required a careful tuning. That is:
paragraphs have been adjusted to make it work.
If not, pictures would have gone around in a quite silly way.

\hfill\par\eject

\insertbox[ box=\vbox{\hsize=100pt\parindent=0pt
		\epsfbox{webfig.10}\endgraf
		\nineit Some text: blu blu blu blu blu blu blu blu},
	align=right, skip=2]
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.

Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.

Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
{Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.

\insertbox[ box=\epsfbox{webfig.10}, align=left, skip=2 ]
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.}
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
\par
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.

\insertbox[ box={\epsfbox{webfig.3}}, align=left ]
\noindent
In the margin with no indentation.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.

\insertbox[ box={\epsfbox{webfig.3}}, align=right ]
\noindent
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla bla bla.

\the\hfuzz
\bye