1
% W R A P F I G . S T Y ver 3.1 (Nov 2, 1995)
3
% Copyright (C) 1991,1992,1995 by Donald Arseneau <asnd@Triumf.ca>
4
% These macros may be freely transmitted, reproduced, or modified,
5
% but not sold. This notice must be left intact.
7
% Environments "wrapfigure" and "wraptable" place a figure or table
8
% at the side of the page and wrap text around it.
10
% \begin{wrapfigure}[12]{r}[34pt]{5cm} <figure> \end{wrapfigure}
12
% [number of narrow lines] {placement} [overhang] {width of figure}
14
% Placement is one of r, l, i, o, R, L, I, O, for right, left,
15
% inside, outside, (here / FLOAT).
16
% The figure sticks into the margin by `overhang', if given, or by the
17
% length \wrapoverhang, which is normally zero.
18
% The number of wrapped text lines is normally calculated from the height
19
% of the figure, but may be specified manually ("12" above).
21
% More detailed instructions are given below, following the definitions.
22
% Please direct any problem reports to asnd@Triumf.ca
24
%%%%% ----- Begin definitions ----- %%%%%
26
\@ifundefined{c@WF@wrappedlines}{}{\endinput}
28
\newdimen\wrapoverhang \wrapoverhang\z@
30
\newcount\c@WF@wrappedlines % used globally
34
\let\@@parshape\parshape
35
\let\WF@@everypar\everypar
37
\def\wrapfigure{\def\@captype{figure}\@ifnextchar[\WF@wr{\WF@wr[]}}
38
\def\wraptable{\def\@captype{table}\@ifnextchar[\WF@wr{\WF@wr[]}}
40
\def\WF@wr[#1]#2{% first two args: #1=num lines, #2=placement
41
\xdef\WF@wfname{wrap\@captype\space}%
42
\ifvoid\WF@box\else \WFclear \WF@collision \fi
43
\xdef\WF@place{\string`\@car#2r\@nil}%
44
\ifnum\lccode\WF@place=\WF@place \global\WF@floatfalse
45
\else \global\WF@floattrue \fi
46
\ifx\parshape\WF@fudgeparshape \ifWF@float\else\WF@collision\fi \else
47
\ifx\par\@@par \ifnum\@@parshape>\z@\WF@conflict\fi \else \WF@conflict\fi
49
\@ifnextchar[\WF@rapt{\WF@rapt[\wrapoverhang]}}
51
\def\WF@rapt[#1]#2{% final two args: #1 = overhang, #2 = width,
52
\gdef\WF@ovh{#1}% hold overhang for later, when \width is known
53
\global\setbox\WF@box\vtop\bgroup \setlength\hsize{#2}\@parboxrestore}
55
\def\endwrapfigure{\par\hrule\@width\hsize\@height\z@ % force width
56
\egroup % end the \vtop; width is known so now is "later"
57
\def\width{\wd\WF@box}\setlength\wrapoverhang{\WF@ovh}%
58
\xdef\WF@ovh{\the\wrapoverhang}% save until wrapping
59
\ifdim\ht\WF@box>\topskip \ht\WF@box\z@ \fi % too much height, set flag.
60
\ifdim\ht\WF@box<.5\p@ % too tall (starts with \vbox) or too short
61
\global\setbox\WF@box\vtop{\vskip-1.4ex\unvbox\WF@box}\fi
62
\global\WF@size\dp\WF@box % box is guaranteed to have little height.
63
\global\advance\WF@size1.5\baselineskip \global\advance\WF@size\tw@\intextsep
64
\aftergroup\WF@startfloating % even when not really floating!
65
\ifWF@float\else \ifhmode
66
{\unskip \parfillskip\z@skip \par \vskip-\parskip}\aftergroup\noindent
67
\fi\fi \global\@ignoretrue}
68
\let\endwraptable\endwrapfigure
70
% Subvert \everypar to float fig and do wrapping. Also for non-float.
71
\def\WF@startfloating{%
72
\WF@everypar\expandafter{\the\everypar}\let\everypar\WF@everypar
73
\WF@@everypar{\ifvoid\WF@box\else\WF@floathand\fi \the\everypar
78
\ifx\parshape\WF@fudgeparshape \WF@fltmes\else
79
\ifx\par\@@par\ifnum\@@parshape=\z@\ifdim\hangindent=\z@
80
\setbox\z@\lastbox \begingroup
81
\@@par \WF@@everypar{}\WF@putfigmaybe
82
\endgroup % start wrapping
83
\ifvoid\z@\else\box\z@\fi % replace indentation
84
\else\WF@fltmes\fi\else\WF@fltmes\fi\else\WF@fltmes\fi\fi}
86
% Put fig here if it fits or if it can't float
89
\vskip-\parskip \global\WF@floatfalse
90
\let\pagetotal\maxdimen % kludge flag for "not top of page"
92
\@tempdima\pagedepth % save page depth
93
{\advance\parskip\@tempdima\vskip-\parskip}% back up to baseline
94
\penalty\interlinepenalty % update pg. parameters
95
\@tempdimb\pagegoal \advance\@tempdimb-\pagetotal % room left on page
96
\ifdim\@tempdimb<\z@ % \WF@info{Page overfull already;}%
98
\ifdim-\@tempdimb>\pageshrink \else \pagebreak \fi
100
\ifdim\WF@size>\@tempdimb
101
% \WF@info{Size \the\WF@size\space does not fit in \the\@tempdimb}%
102
\ifWF@float \dimen@.5\baselineskip \else \dimen@ 2\baselineskip\fi
103
\ifdim\pagestretch>\dimen@ \dimen@\pagestretch \fi
104
\ifdim\pagefilstretch>\z@ \dimen@\@tempdimb \fi
105
\ifdim\pagefillstretch>\z@ \dimen@\@tempdimb \fi
106
\advance\dimen@.5\baselineskip
107
\ifdim\dimen@>\@tempdimb % \WF@info{Page nearly full; can stretch}%
108
\global\WF@floatfalse \pagebreak
110
\else % \WF@info{Fits in \the\@tempdimb;}%
111
\global\WF@floatfalse
113
\vskip\@tempdima\relax % (return erased page depth)
118
\else % putting here;
119
\WF@info{Put \WF@wfname here:}%
120
{\ifodd\if@twoside\c@page\else\@ne\fi % assign l/r to i/o placement
121
\lccode`i`l\lccode`o`r\else \lccode`i`r\lccode`o`l\fi
122
\xdef\WF@place{\the\lccode\lccode\WF@place}}% twice to get only l or r
123
\hbox to\z@{% llap or rlap depending on {l} or {r}; calc effective width
124
\@tempdima\wd\WF@box \@tempdimb\WF@ovh
125
\advance\@tempdima-\@tempdimb \advance\@tempdima\columnsep
126
\@tempdimb\hsize \advance\@tempdimb-\@tempdima
127
\ifnum `l=\WF@place % fig on left
128
\hss % figure overlaps space to the left
129
\def\@tempa{\kern\columnsep}% position to left of the gap
131
\@tempdima\z@ % no left indentation
132
\kern\@tempdimb \kern\columnsep
133
\def\@tempa{\hss}% figure overlaps space to the right
135
\ifdim\@tempdimb<\hsize
136
\xdef\WF@wrapil{\the\@tempdima \the\@tempdimb}% indentation and length
138
\xdef\WF@wrapil{\z@ \the\hsize}%
140
\ifdim\pagetotal=\z@ % \WF@info{Put \WF@wfname at top of p.\thepage}%
141
\global\advance\WF@size-\intextsep
142
\else % \WF@info{Putting \WF@wfname in middle of page}%
143
\setbox\WF@box\hbox{\lower\intextsep\box\WF@box}%
144
\fi \dp\WF@box\z@ \box\WF@box \@tempa
146
\aftergroup\WF@startwrapping % after the \endgroup which immediately follows
149
\def\WF@startwrapping{%
151
{\advance\WF@size1.1\baselineskip
152
\divide\WF@size\baselineskip \global\c@WF@wrappedlines\WF@size}%
154
\setcounter{WF@wrappedlines}{\WF@wli}\global\advance\c@WF@wrappedlines\@ne
156
\ifnum\c@WF@wrappedlines>\@ne
157
\let\parshape\WF@fudgeparshape \let\WF@pspars\@empty \let\WF@@par\par
158
\def\@setpar##1{\def\WF@@par{##1}}\def\par{\@par}\let\@par\WF@mypar
159
\xdef\WF@restoretol{\tolerance\the\tolerance}\tolerance9999
163
\ifnum\c@WF@wrappedlines<\tw@ \WF@finale
164
\else \begingroup % Create \parshape command:
165
\@tempcnta\@ne \let\WF@wrapil\relax \gdef\WF@ps{}%
166
\@whilenum \@tempcnta<\c@WF@wrappedlines\do{% repeated indentation, length
167
\xdef\WF@ps{\WF@ps\WF@wrapil}\advance\@tempcnta\@ne
169
\ifx\WF@pspars\@empty
170
\@@parshape\c@WF@wrappedlines \WF@ps \WF@noil
171
\else % use external `parshape' values to modify my parshape
176
\WF@@par % what the rest of LaTeX expects \par to be (usually \@@par)
177
\ifnum\@@parshape=\z@ \let\WF@pspars\@empty \fi % reset `parshape'
178
\global\advance\c@WF@wrappedlines-\prevgraf \prevgraf\z@
179
\ifnum\c@WF@wrappedlines<\tw@ \WF@finale \fi}
181
\def\WF@modps{\begingroup
182
\afterassignment\@tempdimb \@tempdima\WF@pspars % a=ind, b=wid
183
\afterassignment\dimen@\advance\@tempdima\WF@wrapil
184
\advance\@tempdimb\dimen@ \advance\@tempdimb-\hsize
185
\def\WF@wrapil{\the\@tempdima \the\@tempdimb}%
186
\edef\@tempb{\@@parshape\c@WF@wrappedlines \WF@ps \WF@pspars}%
187
\expandafter\endgroup\@tempb}
189
\let\@@setpar\@setpar
190
\def\WF@noil{\z@ \hsize}
191
\let\WF@pspars\@empty
193
\def\WF@fudgeparshape{\relax \ifnum\c@WF@wrappedlines<\tw@ \WF@finale
194
\else \afterassignment\WF@fudgeparshapee \fam \fi}
195
\def\WF@fudgeparshapee{\ifnum\fam=\@ne \expandafter \WF@parshapeee \else
196
\WF@conflict \@@parshape\fam \fi}
197
\def\WF@parshapeee#1#2{\begingroup
198
\delimitershortfall#1\nulldelimiterspace#2\edef\@tempa{%
199
\def\noexpand\WF@pspars{\the\delimitershortfall \the\nulldelimiterspace}}%
200
\expandafter\endgroup\@tempa \WF@wraphand}
202
\def\WF@finale{\ifx\parshape\WF@fudgeparshape
203
\WF@restoretol \let\@setpar\@@setpar \let\par\WF@@par
204
\WF@info{Finish wrapping text}%
205
\ifx\par\@@par \def\@par{\let\par\@@par\par}\else \let\@par\WF@@par \fi
206
\let\parshape\@@parshape
207
\parshape\ifx\WF@pspars\@empty \z@ \else \@ne \WF@pspars\fi \fi
208
\ifvoid\WF@box \ifx\everypar\WF@everypar
209
\let\everypar\WF@@everypar \everypar\expandafter{\the\WF@everypar}%
212
\newcommand{\WFclear}{\ifvoid\WF@box\else \vskip\bigskipamount \box\WF@box
213
\let\everypar\WF@@everypar \everypar\expandafter{\the\WF@everypar}%
214
\fi \global\c@WF@wrappedlines\z@ \WF@finale}
217
\toks0={\let\everypar\WF@@everypar \everypar\expandafter{\the\WF@everypar}%
218
\let\parshape\@@parshape \let\@setpar\@@setpar }
219
\toks1=\expandafter{\@arrayparboxrestore}
220
\toks2=\expandafter{\clearpage}
221
\edef\@tempa{\def\noexpand\@arrayparboxrestore{\the\toks0 \the\toks1}%
222
\def\noexpand\clearpage{\noexpand\protect\noexpand\WFclear \the\toks2}}
226
\def\WF@conflict{\WF@warning
227
{\WF@wfname used inside a conflicting environment}}
228
\def\WF@collision{\WF@warning{Collision between wrapping environments}}
229
\def\WF@fltmes{\ifWF@float \WF@info{\WF@wfname floats}%
230
\else \WF@warning{Stationary \WF@wfname forced to float}\fi}
232
\let\WF@warning\@warning
235
\@ifundefined{DeclareOption}{\endinput}{}
237
\def\WF@warning{\PackageWarning{wrapfig}}
238
\ProvidesPackage{wrapfig}[1995/11/02 \space v 3.1]
239
\DeclareOption{verbose}{\def\WF@info{\PackageInfo{wrapfig}}}
241
\AtEndDocument{\WFclear}
245
%%%%% ----- End definitions ----- %%%%%
247
%%%%% ----- Begin Instructions ----- %%%%%
250
W R A P F I G . S T Y \ \ ver 3.1 \ \ (Nov 2, 1995)
252
Copyright (C) 1991,1992,1995 by Donald Arseneau (asnd@Triumf.ca)
254
Wrapfig.sty provides the environments "wrapfigure" and "wraptable" for
255
typesetting a narrow float at the edge of the text, and making the text
256
wrap around it. The "wrapfigure" and "wraptable" environments interact
257
properly with the "\caption" command to produce proper numbering, but
258
they are not regular floats like "figure" and "table", so (beware!) they
259
may be printed out of sequence with the regular floats. There are four
260
parameters for "\begin{wrapfigure}", two optional and two required, plus
261
the text of the figure, with a caption perhaps:
263
\begin{wrapfigure}[12]{r}[34pt]{5cm} <figure> \end{wrapfigure}
265
[number of narrow lines] {placement} [overhang] {width}
269
- You must not specify a wrapfigure in any type of list environment or
270
or immediately before or immediately after one. It is OK to follow
271
a list if there is a blank line ("\par") in between.
273
- If you put a wrapfigure in a parbox or a minipage, or any other type
274
of grouping, the text wrapping should end before the group does.
276
- It does work in two-column format, but are your figures that small?
278
- It may be out of sequence with regular floats.
280
- The hlines that may be printed above and below floats are ignored;
281
you must insert them manually if desired.
283
- "\linewidth" is not adjusted within the wrapped text (because it can
284
only be set for whole paragraphs at a time). It is set within the
287
\section{Placement and Floating}
289
Parameter "#2" (required) is the figure placement code, but the valid
290
codes are different from regular figures. They come in pairs: an
291
uppercase version which allows the figure to float, and a lowercase
292
version that puts the figure ``exactly here''.
294
r R - the right side of the text
295
l L - the left side of the text
296
i I - the inside edge--near the binding (if "[twoside]" document)
297
o O - the outside edge--far from the binding
299
You should specify one code only, not a list. The figure or table must
300
be on one side or the other; it cannot be in the middle with text on
301
both sides. The "i" and "o" options refer to the inside and outside of
302
the whole page, not individual columns.
304
The ability to float is somewhat restricted, and you will get best results
305
by giving exact manual placement, but floating is more convenient while
306
revising the document. Any changes to the formatting can ruin your manual
307
positioning so you should adjust the placement just before printing a
308
final copy. Here are some tips for good placement:
310
- The environment should be placed so as to not run over a page break.
312
- The environment must not be placed in special places like lists.
314
- Only plain text should wrap around the figure. Section titles and
315
big equations are bad; lists are bad if the figure is on the left.
316
(These are legal, they just look poor.) Small equations look fine.
318
- It is convenient to begin the environment between paragraphs, but if
319
you want placement in the middle of a paragraph, you must put the
320
environment between two words where there is a natural line break.
322
When floating, \LaTeX\ tries to apply these rules. More specifically,
323
a floated wrapping environment will only begin...
325
- at the beginning of a paragraph,
327
- when there is enough room on the page, or it is possible to go on
330
- if the `paragraph' is not in a section title or a list,
332
- if the paragraph is not wrapping around another figure,
334
- in the main text (not in a minipage etc.)
336
It is possible that a non-floating wrapfigure will be forced to float
337
when an earlier one is still being processed. A warning will be written
338
in that case. You can have more information about the floating process
339
written to the log file by specifying "\usepackage[verbose]{wrapfig}".
341
If there is a lot of flexibility on a page, a floating wrapfigure may
342
be placed badly; you must turn to manual placement. A rare problem is
343
that floats and footnotes specified within the wrapping text can also
344
cause poor placement and bad formatting.
347
\section {Sizing and optional overhang}
349
Parameter "#4" (the second required parameter) is the width of the figure
350
or table. It is possible that the contents will be wider or narrower than
351
this value (if you use a tabbing environment or "\hbox") and the wrapped
352
text will use the greater of the specified and actual widths. \LaTeX\ will
353
wrap surrounding text around the figure, leaving a gap of "\intextsep" at
354
the top and bottom, and "\columsep" at the side, by producing a series of
355
shortened text lines beside the figure. The indentation (shortening) of
356
the text is the figure width plus "\columnsep" minus overhang (if any; see
359
\LaTeX\ calculates the number of short lines needed based on the height
360
of the figure and the length "\intextsep". You can override this guess
361
by giving the first optional argument (parameter "#1") specifying the
362
number of shortened lines (counting each displayed equation as 3 lines).
363
This is particularly useful when the surrounding text contains extra
364
vertical spacing that is not accounted for automatically.
366
The second optional parameter ("#3") tells how much the figure should
367
hang out into the margin. The default overhang is given by the length
368
"\wrapoverhang", which is "0pt" normally but can be changed using
369
"\setlength". For example, to have all wrapfigures use the space
370
reserved for marginal notes,
372
\setlength{\wrapoverhang}{\marginparwidth}
373
\addtolength{\wrapoverhang}{\marginparsep}
375
When you do specify the overhang explicitly for a particular figure, you
376
can use a special unit called "\width" meaning the width of the figure.
377
For example, "[0.5\width]" makes the center of the figure sit on the
378
edge of the text, and "[\width]" puts the figure entirely in the margin
379
(and the adjacent text is indented by just "\columnsep").
382
\section{Some Random Implementation Notes}
384
Unfortunately, \LaTeX's system of setting "\everypar" and "\par" is
385
unable to coexist peacefully with a wrapping environment, so I was
386
forced to subvert the "\@setpar" mechanism and "\everypar". ("\everypar"
387
is already subverted once by NFSS.)
389
When checking the room left on the page, remember that if there is less
390
than "\baselineskip" the new paragraph will begin on the next page, even
391
if there is no page stretch. If non-floating, I force a bad page break
392
rather than have the figure hang into the bottom margin.
394
Here are notes on various variables and some macros; what info they
395
store and how they are used.
397
\WF@wli - number-of-wrapped-lines parameter, saved for start of wrapping.
398
Set globally by "\WF@wr" (set empty if no optional parameter given).
399
The floating mechanism ignores this and uses the real size.
401
\WF@ovh - margin overhang set globally by "\WF@rapt", saved until placing
402
figure (but not reset). Actually, the setting is very tricky so that
403
the expected values are used when a figure floats. First, the expression
404
is saved without evaluation by "\WF@rapt" ("\begin{wrapfigure}") because
405
"\width" is still unknown. Soon after that, "\endwrapfigure" executes
406
"\WF@ovh" to evaluate the overhang and save the result (so that changes
407
to "\wrapoverhang" while this figure is floating won't affect this
408
figure). Finally, it is used by "\WF@putfigmaybe" when printing the fig.
410
\WF@place - a macro that is used as a number, giving the placement code.
411
It might start out as "`I" and later be converted to "114" (r).
413
\WF@box - tested for void at "\begin{wrapfigure}", to avoid collisions,
414
by "\everypar" to do floating, and by "\WF@finale" before resetting
415
"\everypar". Voided globally when used by "\WF@putfigmaybe" (or by
416
"\WF@wr" if an old figure must be dumped prematurely).
418
\par - test if it is "\@@par" by "\begin{wrapfigure}" and "\WF@floathand"
419
to float past special environments. It is set to "\@par" ("\WF@mypar")
420
by "\WF@startwrapping", and restored by an end-group (bad!) or by
421
"\WF@finale" (good). It is protected from change by redefining
424
\parshape - let to "\WF@fudgeparshape" by "\WF@startwrapping", so lists
425
will continue wrapping; "\@@parshape" preserves the real "\parshape"
426
command, and it is restored by "\WF@finale" or "\@parboxrestore".
427
"\WF@floathand" and "\WF@wr" test if old wrapping is still in progress
428
with "\ifx\parshape\WF@fudgeparshape". The value of "\@@parshape" is
429
also tested to float past lists and other wrapping environments.
431
\hangindent - tested to float past section titles etc.
433
\c@WF@wrappedlines - the number of shortened lines + 1; set globally by
434
"\WF@startwrapping" and decremented by "\par" ("\WF@mypar"). It is > 1
435
only when wrapping is incomplete. "\WF@wraphand", "\WF@fudgeparshape",
436
and "\WF@mypar" test the number for calling "\WF@finale". It may get
437
stuck at some high value if "\par" is restored by an end-group, (and
438
wrapping is terminated prematurely) so it is unwise to use this as a
439
test for wrapping-complete.
441
\pagetotal - one of many parameters used to compute floating. When
442
putting a wrapfigure in a parbox, I assign "\let\pagetotal\maxdimen"
443
(locally!) to signal not-top-of-page and no floating.
445
\WF@pspars - the "\parshape" parameters as LaTeX sets them for lists
446
("\WF@fudgeparshape"); when wrapping I test it and use it to modify my
447
own real params for the paragraph. They are also used when "\parshape"
448
is restored after wrapping.
450
\WF@finale - is performed by "\par" when wrapping should end. However,
451
that might happen inside a group (a list especially), so the subverted
452
versions of "\par", "\parshape" etc. will be reinstated when the group
453
ends. Thus, they must themselves test "\c@WF@wrappedlines" < 2 to see
454
when wrapping is over, and if so, they should just do "\WF@finale" again.
456
These are the tests to see if a floating wrapfigure will fit at a particular
457
spot. These tests are performed at the beginning of every paragraph after
458
the figure, except in lists etc. ("\pagegoal" - "\pagetotal" is the room
462
room_left := \pagegoal - \pagetotal
463
if room_left < 0 then page overfull already: put figure (on next page)
465
if figure_size > room_left then does not fit
466
if max(min_stretch, \pagestretch) + extra > room_left
467
then page can stretch until full: put figure (at top of next page)
469
else figure fits: put figure
473
Even if a wrapfigure is not floating, it will go through the same logic
474
to generate a "\pagebreak", and maybe an underfull page, when the current
475
page can stretch until full. The "min_stretch" depends on whether it is
476
floating or not: ".5\baselineskip" (floating) "2\baselineskip" (not). The
477
"extra" is ".5\baselineskip" in either case. These values can be adjusted.
479
There are some other `magic numbers' for floating that aren't really so
480
special, but you must change them together if you change them at all.
481
To make floating wrapfigures float less and fit on pages more frequently,
482
but not change the number of wrapped lines, decrease the "1.5" in
483
"\global\advance\WF@size1.5\baselineskip" and increase the "1.1" in
484
"\advance\WF@size1.1\baselineskip" by the same amount (and vice versa).
485
To make more (or fewer) wrapped lines for the same size figure, without
486
changing the floating, change "1.1" in "\advance\WF@size1.1\baselineskip"
489
%%%%% ----- End Instructions ----- %%%%%
491
Test file integrity: ASCII 32-54, 55-126: !"#$%&'()*+,-./0123456789
492
:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~