summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/wrapfig2/wrapfig2.sty
blob: 94ac12506de69369fce72bca27072bf93e185150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
%%
%% This is file `wrapfig2.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% wrapfig2.dtx  (with options: `package')
%% 
%% Copyright (C) 1991-2003 by Donald Arseneau  (asnd@triumf.ca)
%% Copyright (C) 2021 by Claudio Beccari (claudio.beccari@gmail.com)
%% License information appended
%% 
\NeedsTeXFormat{LaTeX2e}[2019/01/01]
\ProvidesPackage{wrapfig2}%
        [2021-12-21 v.4.0 Wrapfig extended]

\NeedsTeXFormat{LaTeX2e}[2020-01-01]
\ProvidesPackage{wrapfig2}%
 [2021-11-30 v.4.0 Wrap text around figures, tables, framed text blocks]
\@ifpackageloaded{etoolbox}{}{\RequirePackage{etoolbox}}
\ifcsdef{c@WF@wrappedlines}{\endinput}{}
\ifcsdef{fpeval}{}{\RequirePackage{xfp}}
\RequirePackage{xparse}

\newdimen\wrapoverhang \wrapoverhang\z@
\newdimen\WF@size
\newcounter{WF@wrappedlines}
\newbox\WF@box
\newbox\NWF@box
\newtoks\WF@everypar
\newif\ifWF@float
\newcount\WF@correctlines@switch
\newdimen\insertwidth
\let\@@parshape\parshape
\let\WF@@everypar\everypar

\NewDocumentEnvironment{wrapfigure}{o m o G{0pt}}%
  {\wrapfloat{figure}[#1]{#2}[#3]{#4}}%
  {\endwrapfloat}

\NewDocumentEnvironment{wraptable}{o m o G{0pt}}%
  {\wrapfloat{table}[#1]{#2}[#3]{#4}}%
  {\endwrapfloat}

\NewDocumentEnvironment{wraptext}%
      {O{l}    D||{0.5\columnwidth} D<>{0}   D(){figure}}%
{%                                                        Open environment
  \insertwidth=#2\relax
  \def\textplacement{#1}%
  \def\textcorrection{#3}%
  \def\WF@caption@label{#4}%
  \begin{lrbox}\NWF@box%            This box is to contain the framed text
    \minipage{\insertwidth}%
   \tcolorbox
}{%                                                      Close environment
  \endtcolorbox\endminipage\end{lrbox}%
  \edef\NWF@wli%                        \NWF@wli is a macro, not a counter
    {%
      \fpeval{%
        round((\ht\NWF@box+\dp\NWF@box)/\baselineskip,0)+2+\textcorrection
             }%
    }%
  \ifhmode\unskip\else\leavevmode\noindent\fi
  \bgroup\edef\x{\egroup\noexpand\wrapfloat{\WF@caption@label}[\NWF@wli]%
      {\textplacement}{\the\insertwidth}}\x
    \box\NWF@box %                       Output framed box containing text
  \endwrapfloat
  \ignorespaces
}


\NewDocumentEnvironment{wrapfloat}{m o m o G{\z@}}%
  {\def\@captype{#1}\WF@wr[#2]{#3}[#4]{#5}}%
  {\ifdim\hsize>\z@
    \par\hrule\@width\hsize\@height\z@ %   force width with invisible rule
  \else
    \unskip \egroup \box\z@  %                               or close hbox
  \fi
 \egroup %                      close the vtop box; its width now is known
  \WF@floatstyhook %                                 support for float.sty
  \def\width{\wd\WF@box}%
  \setlength\wrapoverhang{\WF@ovh}%
  \xdef\WF@ovh{\the\wrapoverhang}%                     save until wrapping
  \ifdim\ht\WF@box>\topskip \ht\WF@box\z@ \fi%          too high, set flag
  \ifdim\ht\WF@box<.5\p@ %                              too short, move up
    \global\setbox\WF@box\vtop{\vskip-1.4ex\unvbox\WF@box}%
  \fi
   \global\WF@size=%                  compute total box hight with \fpeval
     \fpeval{\ht\WF@box+\dp\WF@box+1.5\baselineskip+\tw@\intextsep}\p@
   \aftergroup\WF@startfloating %        use even when not really floating
  \unless\ifWF@float
    \ifhmode
      {\unskip \parfillskip\z@skip \par \vskip-\parskip}%
      \aftergroup\noindent
    \fi
  \fi
  \global\@ignoretrue
  }

\NewDocumentCommand\WF@wr{o m o m s}{%
  \xdef\WF@wfname{wrap\@captype\space}%
  \unless\ifvoid\WF@box
    \WFclear \WF@collision
  \fi
  \xdef\WF@place{\string`\@car#2r\@nil}%
  \ifnum\lccode\WF@place=\WF@place
      \global\WF@floatfalse
  \else
      \global\WF@floattrue
  \fi
  \ifx\parshape\WF@fudgeparshape
    \unless\ifWF@float\\WF@collision\fi
  \else
    \ifx\par\@@par
      \ifnum\@@parshape>\z@\WF@conflict\fi
    \else
      \WF@conflict
    \fi
   \fi
   \IfValueTF{#1}%                 save optional line number or correction
     {\gdef\WF@wli{#1}}%
     {\gdef\WF@wli{0}}%
  \IfValueTF{#3}%                                   save optional overhang
      {\gdef\WF@ovh{#3}}%
      {\gdef\WF@ovh{\z@}}%
   \global\setbox\WF@box\vtop\bgroup \setlength\hsize{#4}%       set width
   \ifdim\hsize>\z@
     \@parboxrestore
   \else
     \setbox\z@\hbox\bgroup
     \let\wf@@caption\caption
     \let\caption\wf@caption
     \ignorespaces
   \fi
  \IfBooleanTF{#5}%    if the asterisk is present set the numerical switch
              {\global\WF@correctlines@switch=\@ne}%
              {\global\WF@correctlines@switch=\z@}%
  \global\@ignoretrue
}

\def\wf@caption{\relax%        redefine \wf@caption in case \hsize is zero
  \ifdim\hsize>\z@
    \let\caption\wf@@caption
  \else
    \unskip \egroup \hsize\wd\z@ \@parboxrestore \box\z@%      empty \box0
  \fi
  \caption
}

\def\WF@startfloating{%
 \WF@everypar\expandafter{\the\everypar}\let\everypar\WF@everypar
 \WF@@everypar{\ifvoid\WF@box\else\WF@floathand\fi \the\everypar
 \WF@wraphand
}}

\def\WF@floathand{%
  \ifx\parshape\WF@fudgeparshape
    \WF@fltmes
  \else
    \ifx\par\@@par
      \ifnum\@@parshape=\z@
        \ifdim\hangindent=\z@
          \setbox\z@\lastbox \begingroup
          \@@par \WF@@everypar{}\WF@putfigmaybe
          \endgroup %                      after this group start wrapping
          \unless\ifvoid\z@ %                          replace indentation
              \box\z@
          \fi
        \else
          \WF@fltmes
        \fi
      \else
        \WF@fltmes
      \fi
    \else
      \WF@fltmes
    \fi
  \fi}

\def\WF@putfigmaybe{%
\ifinner
  \vskip-\parskip \global\WF@floatfalse
  \let\pagetotal\maxdimen %              kludge flag for "not top of page"
\else %                                                         outer page
  \@tempdima\pagedepth %                                   save page depth
  {\advance\parskip\@tempdima\vskip-\parskip}%        back up to base line
  \penalty\interlinepenalty %                       update page parameters
  \@tempdimb\pagegoal \advance\@tempdimb-\pagetotal %    room left on page
  \ifdim \@tempdimb<\z@ %                                page already full
    \global\WF@floatfalse
    \unless\ifdim-\@tempdimb>\pageshrink
      \pagebreak
    \fi
  \else
    \ifdim\WF@size>\@tempdimb%     box too high does not fit in \@tempdimb
      \ifWF@float
        \dimen@.5\baselineskip
      \else
        \dimen@ 2\baselineskip
      \fi
      \ifdim\pagestretch>\dimen@ \dimen@\pagestretch \fi
      \ifdim\pagefilstretch>\z@ \dimen@\@tempdimb \fi
      \ifdim\pagefillstretch>\z@ \dimen@\@tempdimb \fi
      \advance\dimen@.5\baselineskip
      \ifdim\dimen@>\@tempdimb %                     stretch page contents
        \global\WF@floatfalse \pagebreak
      \fi
    \else %                                         box fits in \@tempdimb
      \global\WF@floatfalse
    \fi
  \fi
  \vskip\@tempdima\relax %                        return erased page depth
\fi
\noindent
\ifWF@float
  \WF@fltmes
\else %                                               place insertion here
  \WF@info{Put \WF@wfname here:}%
  {\ifodd
     \if@twoside\c@page\else\@ne\fi %          assign l/r to i/o placement
     \lccode`i`l\lccode`o`r\else \lccode`i`r\lccode`o`l%
   \fi
   \xdef\WF@place{\the\lccode\lccode\WF@place}%
                 }%                               twice to get only l or r
  \hbox to\z@{%       llap o rlap depending on lor r; calc effective width
    \@tempdima\wd\WF@box \@tempdimb\WF@ovh
    \advance\@tempdima-\@tempdimb \advance\@tempdima\columnsep
    \@tempdimb\hsize \advance\@tempdimb-\@tempdima
    \xdef\WF@adjlw{\the\@tempdima}%
    \ifnum `l=\WF@place %                                   object on left
      \hss
      \def\@tempa{\kern\columnsep}%             take right gap into action
    \else %                                                insert on light
      \@tempdima\z@ %                                  no left indentation
      \kern\@tempdimb \kern\columnsep
      \def\@tempa{\hss}%                object overlaps space to the right
    \fi
    \ifdim\@tempdimb<\hsize
      \xdef\WF@wrapil{\the\@tempdima \the\@tempdimb}%  indent.n and length
      \xdef\WF@adjtlm{\the\@tempdima}%
    \else
      \xdef\WF@wrapil{\z@ \the\hsize}%
      \xdef\WF@adjlw{\z@}\xdef\WF@adjtlm{\z@}%
    \fi
    \ifdim\pagetotal=\z@ %              put object at top of page \thepage
      \global\advance\WF@size-\intextsep
    \else %                               put object in middle of the page
      \setbox\WF@box\hbox{\lower\intextsep\box\WF@box}%
    \fi
    \dp\WF@box\z@
    \box\WF@box
    \@tempa
  }%                                                      end \hbox to 0pt
  \aftergroup\WF@startwrapping
\fi
}

\def\WF@startwrapping{%
  \ifnum\WF@wli=\z@ %                              no number was specified
    \setcounter{WF@wrappedlines}%
      {\fpeval{round(\WF@size/\baselineskip,0)}}%
    \xdef\WF@wli{\value{WF@wrappedlines}}%
  \else
    \ifnum\WF@correctlines@switch>\z@ %             line number correction
      \setcounter{WF@wrappedlines}
        {\fpeval{round((\WF@size)/\baselineskip,0)+\WF@wli}}%
      \xdef\WF@wli{\value{WF@wrappedlines}}%
    \else
      \setcounter{WF@wrappedlines}{\WF@wli}%      absolute number of lines
      \global\advance\c@WF@wrappedlines\@ne
    \fi
  \fi
  \ifnum\c@WF@wrappedlines>\@ne %                              fine tuning
    \let\parshape\WF@fudgeparshape \let\WF@pspars\@empty \let\WF@@par\par
    \def\@setpar##1{\def\WF@@par{##1}}\def\par{\@par}\let\@par\WF@mypar
    \xdef\WF@restoretol{\tolerance\the\tolerance}\tolerance9999
    \advance\linewidth-\WF@adjlw \advance\@totalleftmargin\WF@adjtlm
 \fi}

\def\WF@wraphand{%                    for indenting one or more paragraphs
  \ifnum\c@WF@wrappedlines<\tw@
    \WF@finale
  \else \begingroup %                            create a parshape command
    \@tempcnta\@ne \let\WF@wrapil\relax \gdef\WF@ps{}%
    \@whilenum
      \@tempcnta<\c@WF@wrappedlines\do{%              repeated indentation
      \xdef\WF@ps{\WF@ps\WF@wrapil}\advance\@tempcnta\@ne
      }%
    \endgroup
    \ifx\WF@pspars\@empty
      \@@parshape\c@WF@wrappedlines \WF@ps \WF@noil
    \else %           use external `parshape' values to modify my parshape
      \WF@modps
    \fi
  \fi
}

\def\WF@mypar{\relax
  \WF@@par
  \ifnum\@@parshape=\z@
    \let\WF@pspars\@empty %                                 reset parshape
  \fi
  \global\advance\c@WF@wrappedlines-\prevgraf \prevgraf\z@
  \ifnum\c@WF@wrappedlines<\tw@
    \WF@finale
  \fi
}

\def\WF@modps{\begingroup
  \afterassignment\@tempdimb \@tempdima\WF@pspars % a=indent.num, b= width
  \advance\@tempdima-\WF@adjtlm \advance\@tempdimb\WF@adjlw
  \let\WF@wrapil\WF@pspars
  \edef\@tempb{\@@parshape\c@WF@wrappedlines
                \WF@ps \the\@tempdima \the\@tempdimb}%
  \expandafter\endgroup\@tempb
}

\let\@@setpar\@setpar
\def\WF@noil{\z@ \hsize}
\let\WF@pspars\@empty

\def\WF@fudgeparshape{\relax
  \ifnum\c@WF@wrappedlines<\tw@
    \WF@finale
  \else
    \afterassignment\WF@fudgeparshapee \fam
  \fi
}

\def\WF@fudgeparshapee{%
  \ifnum\fam=\@ne \expandafter
    \WF@parshapeee
  \else
    \WF@conflict \@@parshape\fam
  \fi
}

\def\WF@parshapeee#1#2{%
  \begingroup\delimitershortfall#1%
  \nulldelimiterspace#2%         \advance \nulldelimiterspace by \WF@adjlw
  \edef\@tempa{\def\noexpand\WF@pspars{%
      \the\delimitershortfall \the\nulldelimiterspace}}%
  \expandafter\endgroup\@tempa \WF@wraphand
}

\def\WF@finale{%
  \ifx\parshape\WF@fudgeparshape
    \WF@restoretol \let\@setpar\@@setpar \let\par\WF@@par
    \advance\linewidth\WF@adjlw \advance\@totalleftmargin-\WF@adjtlm
    \WF@info{Finish wrapping text}%
    \ifx\par\@@par
      \def\@par{\let\par\@@par\par}%
    \else
      \let\@par\WF@@par
    \fi
    \let\parshape\@@parshape
    \parshape=\ifx\WF@pspars\@empty
                \z@
              \else
                \@ne \WF@pspars
              \fi
  \fi
  \ifvoid\WF@box
    \ifx\everypar\WF@everypar
      \let\everypar\WF@@everypar \everypar\expandafter{\the\WF@everypar}%
    \fi
  \fi
}

\newcommand{\WFclear}{\par
  \unless\ifvoid\WF@box
    \vskip\bigskipamount \box\WF@box
    \let\everypar\WF@@everypar \everypar\expandafter{\the\WF@everypar}%
  \fi
  \global\c@WF@wrappedlines\z@ \WF@finale
  \global\WF@correctlines@switch\z@
}

\begingroup
 \toks0={\let\everypar\WF@@everypar
         \everypar\expandafter{\the\WF@everypar}%
         \let\parshape\@@parshape
         \let\@setpar\@@setpar
        }
 \toks1=\expandafter{\@arrayparboxrestore}%
 \toks2=\expandafter{\clearpage}%
 \edef\@tempa{%
      \def\noexpand\@arrayparboxrestore{\the\toks0 \the\toks1}%
      \def\noexpand\clearpage
      {\noexpand\protect\noexpand\WFclear \the\toks2}}%
 \expandafter
 \endgroup\@tempa

\@ifundefined{@capwidth}{\let\@capwidth\hsize}{}%

\def\WF@conflict{\WF@warning
  {\WF@wfname used inside a conflicting environment}}%

\def\WF@collision{\WF@warning{Collision between wrapping environments}}%

\def\WF@fltmes{%                                        message for floats
  \ifWF@float
    \WF@info{\WF@wfname floats}%
 \else
   \WF@warning{Stationary \WF@wfname forced to float}%
 \fi
}

\let\WF@warning\@warning
\let\WF@info\@gobble

\let\WF@floatstyhook\relax
\@ifundefined{newfloat}{}{%         \newfloat comes from somewhere besides
  \@ifundefined{restylefloat}{%
    \@ifclassloaded{memoir}{%
      \toks@=\expandafter\expandafter\expandafter
       {\csname\string\newfloat\endcsname [{#1}]{#2}{#3}{#4}%
       \newenvironment{wrap#2}{\wrapfloat{#2}}{\endwrapfloat}%
      }%                             Mmmm; this might be wrong. Not tested
   \edef\@tempa{\def\expandafter\noexpand\csname\string\newfloat\endcsname
        [##1]##2##3##4{\the\toks@}}%
      \@tempa
     }%                                                 end memoir support
     {}%                                  other origins of \newfloat here?
  }{%          float.sty handler. Ops: Two versions for different versions
  \@ifundefined{float@restyle}%
    {%                                                     older float.sty
      \toks@=\expandafter{\restylefloat{#1}%       env. might be undefined
      \@namedef{wrap#1}{%
        \def\@captype{#1}\@nameuse{fst@#1}%
        \def\WF@floatstyhook{\let\@currbox\WF@box \columnwidth\wd\WF@box
          \global\setbox\WF@box\float@makebox}%
      \@ifnextchar[\WF@wr{\WF@wr[]}}%
      \expandafter\let\csname endwrap#1\endcsname \endwrapfigure
      }%
    \edef\@tempa{\def\noexpand\restylefloat##1{\the\toks@}}%
  }{%             newer float.sty: uses \float@restyle, and \float@makebox
      \toks@=\expandafter{\float@restyle{#1}%      env. might be undefined
        \@namedef{wrap#1}{\def\@captype{#1}\@nameuse{fst@#1}%
          \def\WF@floatstyhook{\let\@currbox\WF@box
            \global\setbox\WF@box\float@makebox{\wd\WF@box}}%
        \@ifnextchar[\WF@wr{\WF@wr[]}}%
        \expandafter\let\csname endwrap#1\endcsname \endwrapfigure
      }%
      \edef\@tempa{\def\noexpand\float@restyle##1{\the\toks@}}%
    }%
    \@tempa %                                        perform redefinitions
    }%                                               end float.sty handler
}%                                          end redefinitions of \newfloat

\ifcsname newfloatlist\endcsname%                     support ccaption.sty
  \toks@=\expandafter\expandafter\expandafter
    {\csname\string\newfloatlist\endcsname [{#1}]{#2}{#3}{#4}{#5}%
  \@namedef{wrap#2}{\wrapfloat{#2}}%
  \expandafter\let\csname endwrap#2\endcsname \endwrapfloat
  }%
  \edef\@tempa{%
  \def\expandafter\noexpand\csname\string\newfloatlist\endcsname
   [##1]##2##3##4##5{\the\toks@}}%
  \@tempa
\fi

\@ifundefined{DeclareOption}{\endinput}{%
\def\WF@warning{\PackageWarning{wrapfig2}}%
\DeclareOption{verbose}{\def\WF@info{\PackageInfo{wrapfig2}}}%
\ProcessOptions
\AtEndDocument{\WFclear}}%

%% 
%% Distributable under the LaTeX Project Public License,
%% version 1.3c or higher (your choice). The latest version of
%% this license is at: http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained"
%% 
%% This work  consists of file  wrapfig2.dtx, and the derived  files
%% wrapfig2.sty and wrapfig2.pdf, plus the auxiliary file README.txt.
%% 
%%
%% End of file `wrapfig2.sty'.