summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wrapfig2/wrapfig2.dtx
blob: 55147dcd015a44a23d9cc45534e75151cc3cddf1 (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
% \iffalse
% !TEX encoding = UTF-8 Unicode
% Remember to use \StopEventually{}, otherwise no Check Sum
%<*internal>
\begingroup
\input docstrip.tex
\keepsilent
\preamble

Copyright (C) 1991-2003 by Donald Arseneau  (asnd@triumf.ca)
Copyright (C) 2021-2022 by Claudio Beccari (claudio.beccari@gmail.com)
License information appended
  
\endpreamble

\postamble

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 this file README.txt.

\endpostamble
\askforoverwritefalse

\generate{\file{wrapfig2.sty}{\from{wrapfig2.dtx}{package}}}

\def\tmpa{plain}
\ifx\tmpa\fmtname\endgroup\expandafter\bye\fi
\endgroup
%</internal>
% \fi
%
% \iffalse
%<*package>
%<package>\NeedsTeXFormat{LaTeX2e}[2019/01/01]
%</package>
%<*driver>
\ProvidesFile{wrapfig2.dtx}%
%</driver>
%<+package>\ProvidesPackage{wrapfig2}%
%^^A<+readme>File README.txt for package wrapfig2
%<*package|readme>
 [2022-01-26 v.5.0.7 Wrap text around figures, tables, framed text blocks]
%</package|readme>
%<*driver>
%
\documentclass{ltxdoc}\errorcontextlines=100
\hfuzz 10pt
\usepackage[greek.ancient, english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{mflogo}
\usepackage{multicol,amsmath,fancyvrb,verbatim,microtype}
\usepackage{wrapfig2,kantlipsum,xspace}
%
\GetFileInfo{wrapfig2.dtx}
\title{The \textsf{wrapfig2} package}
\author{Claudio Beccari\thanks{E-mail: \texttt{claudio dot beccari at gmail dot com}}}
\date{Version \fileversion~--~Last revised \filedate.}
%
\providecommand*\diff{\mathop{}\!\mathrm{d}}
\renewcommand\meta[1]{{\normalfont\textlangle\textit{#1}\textrangle}}
\renewcommand\marg[1]{\texttt{\{\meta{#1}\}}}
\providecommand\opz{}
\renewcommand\opz[1]{{\normalfont\texttt{\itshape#1}}}
\providecommand\Marg{}
\renewcommand\Marg[1]{\texttt{\{#1\}}}
\providecommand\oarg{}
\renewcommand\oarg[1]{\texttt{[\meta{#1}]}}
\providecommand\Oarg{}
\renewcommand\Oarg[1]{\texttt{[#1]}}
\providecommand\aarg{}
\renewcommand*\aarg[1]{\texttt{<\meta{#1}>}}
\providecommand\Aarg{}
\renewcommand\Aarg[1]{\texttt{<#1>}}
\providecommand\barg{}
\renewcommand\barg[1]{\texttt{\string|\meta{#1}\string|}}
\providecommand\Barg{}
\renewcommand\Barg[1]{\texttt{\string|#1\string|}}
\providecommand\parg{}
\renewcommand\parg[1]{\texttt{(\meta{#1})}}
\providecommand\Parg{}
\renewcommand\Parg[1]{\texttt{(#1)}}
\providecommand\eTeX{}
\renewcommand\eTeX{\lower0.5ex\hbox{$\varepsilon\!$}\TeX}
\providecommand\pack{}
\renewcommand\pack[1]{{\normalfont\texttt{#1}}}
\providecommand\class{}
\renewcommand\class[1]{{\normalfont\texttt{#1}}}
\providecommand\env{}
\renewcommand\env[1]{\meta{\textsf{\slshape#1}}}\let\amb\env
\providecommand\Env{}
\renewcommand\Env[1]{\textsf{\slshape#1}}\let\Amb\Env
\providecommand\opt{}
\renewcommand\opt[1]{\textsl{\meta{#1}}}
\providecommand*\Bambiente[1]{\texttt{\char92begin\{#1\}}}
\providecommand*\Eambiente[1]{\texttt{\char92end\{#1\}}}
\let\Bamb\Bambiente \let\Benv\Bamb
\let\Eamb\Eambiente \let\Eenv\Eamb

\ProvideDocumentCommand\goodpagebreak{O{4}}{%
\dimen10=#1\baselineskip
\dimen8=\dimexpr\pagegoal-\pagetotal\relax
\ifdim\dimen8 < \dimen10 \newpage\fi}



\makeatletter

\AfterEndPreamble{%
\apptocmd{\TeX}{{}\xspace}{}{}
\apptocmd{\LaTeX}{{}\xspace}{}{}}

\DeclareRobustCommand\LaTeXe{%
  \LaTeX\@killglue\kern0.15em2$_{\textstyle \varepsilon}${}\xspace}
\DeclareRobustCommand\LaTeXccix{%
  \LaTeX\@killglue\nobreakspace2.09{}\xspace}
\DeclareRobustCommand\LaTeXiii{\LaTeX\@killglue\kern0.15em3{}\xspace}
\DeclareRobustCommand*\pdfLaTeX{pdf\/\LaTeX}
\DeclareRobustCommand*\LuaLaTeX{Lua\LaTeX}
\DeclareRobustCommand*\XeLaTeX{%
X\ifdim\fontdimen1\font=0pt\kern-0.15em\fi
\lower.5ex\hbox{\rotatebox[origin=c]{180}{E}}%
\ifdim\fontdimen1\font=0pt\kern-0.15em\else\kern-0.275em\fi
\LaTeX}

\providecommand\setfontsize{}
\RenewDocumentCommand\setfontsize{O{1.2} m}{%
  \fontsize{#2}{\fpeval{#1*#2}}\selectfont}

\providecommand\hz{\hskip0pt}

\newenvironment{ttsintassi}{\begin{lrbox}{0}
  \minipage{\dimexpr\linewidth-2\fboxrule-2\fboxsep}\ttfamily\obeylines}%
{\endminipage\end{lrbox}\center\fbox{\box0}\endcenter}
    

%
\begin{document}\errorcontextlines=100
\maketitle
\columnseprule=0.4pt
\begin{multicols}{2}
 \tableofcontents
 \end{multicols}
 \DocInput{wrapfig2.dtx}
 
\end{document}
%</driver>
% \fi
%
% \begin{abstract}
% This new package \pack{wrapfig2} is a fork that extends Donald
% Arseneau's package \pack{wrapfig} (version 2.6, dated 2003) by
% adding some \LaTeXiii definitions that accept a final optional star;
% its presence changes the meaning of the first optional
% argument so that it becomes a correction to the number of lines
% that must be indented in order to receive the wrapped object.
% A new environment is added to the original \Env{wrapfigure}
% and \Env{wraptable}, namely \Env{wraptext}; it may be used
% to wrap a small framed text block on a coloured background; the
% philosophy of this new environment is similar to that of the
% other two environments, but the syntax was different with
% version~4.0 of this package, and is very similar with this
% version 5.0. A fall back option is available for backwards
% compatibility.
% \end{abstract}
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% {\color{red}\paragraph{Caution} This package requires a fairly recent
% \LaTeX kernel, otherwise it won't work; any \LaTeX kernel dated
% at least 2020 is~OK.
%
% Read carefully this document, because the are several pieces of
% information concerning other packages that may be incompatible
% with this \pack{wrapfig2} version. Special warnings are typeset
% in red as this one.}
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Introduction}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The purpose of this package is manyfold. On one side it tries
% to modernise the original software by Donald Arseneau by
% upgrading it to the \LaTeXiii modern language. On another it
% creates a new environment, with the same philosophy
% of the original Arseneau's ones, such that a document author
% can emphasise short blocks of text by framing them while
% typesetting the text on a coloured background by means of
% the \pack{curve2e} functionalities, and wrapping this
% inserted text with the surrounding main text.
%
% The original software had some idiosyncrasies; Donald Arseneau
% described them in the documentation of his package; we are sorry
% to admit that such idiosyncrasies might have been slightly reduced;
% but in any case in order to avoid such peculiar anomalies, it is
% sufficient to wrap the inserted object with a reasonable
% number of lines, i.e. by reasonably long paragraphs.
%
% The above implies that no wrapped object code should be specified
% in the source file close the end of a paragraph, unless it is
% followed by other paragraphs; again no object code should be inserted
% within any list; not even close to the end or to the beginning of a
% section. Arseneau's code is capable of specifying the wrapping
% number of lines such that two or more paragraphs can be indented
% so as to wrap a longish insertion, but it is wise to avoid such
% risky situations. Moreover, if the inserted object has a numbered
% caption, the number might not result in the correct sequence with
% the normal corresponding floating objects.
%
% Therefore the usefulness of the wrapping procedure depends very
% much on the document authors' ability to move around their code
% until a suitable position is found. Certainly a good place is
% within a longish  paragraph especially at the beginning of a
% section; or at the beginning of a chapter that starts with plain
% text, in particular just at the beginning of the first paragraph.
%
% The code of this package does very little if anything to correct
% such idiosyncrasies. They are caused by the limitations of the
% \cs{ShipOut} \LaTeXe kernel macro, and very little we were able
% to do in addition to what Arseneau already did.
%
% Another purpose of this package is to add an optional argument
% so that the \meta{number of indented lines} argument does not mean
% the total number, but the correction number to add-to or subtract-from 
% the value computed by the default mechanism devised by Arseneau.
% We assume that most users first use the software to insert an object
% to be wrapped by the surrounding text without specifying any value
% with the specific optional argument; then they evaluate the result,
% and if the space below the wrapped object is too large, or if such
% space is too small they count the necessary number of lines and
% specify it to be processed during another document compilation.
% When the object to be wrapped is tall, it is very easy to miscount
% the necessary number of lines, while is is very easy to evaluate the
% necessary small correction to the computed value.
%
% A further purpose of this package is to define a new environment,
% \Env{wraptext}, to wrap a framed text block typeset on a coloured
% background. On \texttt{texstackexchange} a solution was suggested
% to a user who was asking for such an arrangement; the solution
% resorted to a specific use of the \Env{wrapfigure} environment and
% used the \Env{tcolorbox} environment.
% We thought that an \emph{ad hoc} solution would be a better one,
% since the parameters to be used for a figure have nothing or
% little to do with a text, therefore most of them would be useless
% with a wrapped text. Nevertheless the \meta{location} of the wrapped
% text and the optional correction of the indented lines number would
% still be necessary. We added also the possibility of optionally
% specifying the measure of the wrapped text, even if this measure
% should not be too different from a half the wrapping text measure.
% In facts, with a value too different from \texttt{0.5\cs{linewidth}}
% either the wrapped text has problems with inter word spaces and
% hyphenation because of the small measure, or, on the opposite, the
% indented lines of the wrapping text would have similar problems. 
%
% Notice that the first implementation of this package, version~4.0,
% achieved the desired result but there were two drawbacks: $(a)$
% the syntax was different from that of the other environments,
% and $(b)$ any possible caption was typeset within the same
% framing environment. In version~5.0 both drawbacks were
% eliminated, but since the environment syntax is different,
% in oder to assure backwards compatibility a package option
% was defined in order to fall back to the previous version~4.0
% behaviour.
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Environment syntax}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The new syntax for \Env{wrapfigure} and \Env{wraptable} is backwards
% compatible with the original one: just a final optional star is
% added to the original list of arguments. 
%
% The optional star is available only for the standard \Env{wrapfigure}
% and \Env{wraptable} environments because the backwards compatibility
% requires the first four optional and mandatory arguments to be
% maintained identical. When the optional star is specified, the
% \meta{indented lines number} is interpreted as the correction to the
% computed number.
%
% Notice the different syntax in version 4.0 and version 5.0 of the
% \Env{wraptext} syntax.
%
% \begin{ttsintassi}\setfontsize{8.00}
%\Bambiente{wrapfigure}\oarg{indented lines number}\marg{location}\oarg{overhang}\oarg{width}\meta{$\star$}
%\qquad\meta{figure}
%\Eambiente{wrapfigure}
%~
%\Bambiente{wraptable}\oarg{indented lines number}\marg{location}\oarg{overhang}\oarg{width}\meta{$\star$}
%\qquad\meta{table}
%\Eambiente{wraptable}
%~
%\textnormal{\color{red}Package option \opt{WFold} required for backwards compatibility with version 4.0.*}.
%~
%\Bambiente{wraptext}\oarg{location}\barg{width}\aarg{indented line number correction}\parg{caption label}
%\qquad\meta{text to frame}
%\Eambiente{wraptext}
%~
%\textnormal{\color{red}No package option required for version 5.0.*}.
%~
%\Bambiente{wraptext}\oarg{indented lines number correction} \marg{location} \oarg{overhang} \oarg{width}
%\qquad \meta{optional colour settings}
%\qquad\cs{includeframedtext}\marg{text to frame}\Oarg{\meta{frame thickness},\meta{frame separation}}
%\Eambiente{wraptext}
% \end{ttsintassi}
%^^A
% Please notice that both syntaxes, thanks to  differently
% delimited optional arguments with peculiar default values,
% become very similar when such optional arguments are reduced
% to a minimum; only the \meta{location} argument is delimited
% by brackets with the old version and with braces with the newer
% one.
%
% It may be useful to compare the \cs{includeframedtext} macro,
% used to insert a framed test into a \Env{wraptext} environment,
% with \cs{includegrphics}, used to insert an external image into
% a \Env{figure} environment. Their functions are similar even if
% they refer to different objects to include. Their codes are
% obviously very different and the latter is much more complex
% than the former. The solution for a framed text used by
% version~4.0 was inspired by the information found on
% \texttt{texstackexchange} that used the very elaborate \Env{tcolorbox}
% environment; version~5.0 uses instead a much simpler command
% \cs{framedbox} based on the \pack{curve2e} package macro
% \cs{Curve}. As it can be seen, the logic behind these different
% macros are very similar.
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\section{Examples}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We display some examples by using fake objects and suitably
% long paragraphs; some fake-language long-paragraphs are
% obtained by means of the \pack{kantlipsum} package functionalities;
% they are emphasised with an italic font.
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{A wrapped figure}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The code used to type figure~\ref{fig:figure} is the following:
%
%\begin{flushleft}\obeylines%
%\noindent\Bambiente{wrapfigure}\Marg{r}\Marg{50mm}
%\cs{centering}\cs{unitlength}=1mm
%\Bambiente{picture}\texttt{(40,30)}
%\cs{polygon}\texttt{(0,0)(40,0)(40,30)(0,30)}
%\cs{Line}\texttt{(0,0)(40,30)}\cs{Line}\texttt{(0,30)(40,0)}
%\Eambiente{picture}
%\cs{caption}\Marg{A rectangle with its diagonals}\cs{label}\Marg{fig:figure}
%\Eambiente{wrapfigure}
%\Marg{\cs{itshape} \cs{kant}\Oarg{1}}
%\end{flushleft}
%
% No asterisk was used because the package succeeded to correctly
% compute the necessary number of indented lines.
%
%\begin{wrapfigure}{r}{50mm}
%\centering\unitlength=1mm
%\begin{picture}(40,30)
%\polygon(0,0)(40,0)(40,30)(0,30)
%\Line(0,0)(40,30)\Line(0,30)(40,0)
%\end{picture}
%\caption{A rectangle with its diagonals}\label{fig:figure}
%\end{wrapfigure}
%{\itshape \kant[1]}
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{A wrapped table}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Wrapping a small table is a little more difficult than wrapping
% a figure, because the width of the inserted object is not known
% exactly in advance and is difficult to estimate; therefore it is
% possible that several trial compilations are necessary. In any
% case a \cs{centering} command might help to center the table
% within the indention of the wrapping text. Nevertheless the
% software can compute the object width if a zero value is specified
% or if the \meta{width} parameter is completely omitted together
% with its braces; this second possibility is a feature of this
% package, that uses a \LaTeXiii property by which even a braced
% argument can be treated as an optional argument with a predefined
% default value; see below more details about this feature.
%
%\begin{wraptable}{l}
%\centering 
%  \begin{tabular}{cc}
%  \hline
%  First & Second\\
%  Third & Fourth\\
%  \hline
%\end{tabular}
%\caption{A small table}
%\end{wraptable}
%{\itshape \kant[2]}
%
% The above wrapped small table has been typeset by means of the
% following code.
%\begin{flushleft}\ttfamily\obeylines
%\Bambiente{wraptable}\Marg{l} 
%\quad\cs{centering} 
%\quad\Bambiente{tabular}\Marg{cc}
%\qquad\cs{hline}
%\qquad First \& Second\string \\
%\qquad Third \& Fourth\string \\
%\qquad\cs{hline}
%\quad\Eambiente{tabular}
%\quad\cs{caption}\Marg{A small table}
%\Eambiente{wraptable}
%\Marg{\cs{\itshape} \cs{kant}\Oarg{2}}
%\end{flushleft}
%
% You notice the absence of the braced width value; as said above,
% this braced value is optional, and the software autonomously
% computes the width of the wrapped object. This feature may be
% useful in many instances, although a smart use of
% this width parameter might yield better looking results.
% 
% On the opposite if the user estimates that the table with its
% caption might use 5~lines, and specified such a value as the
% first (optional) argument to the environment, the result would
% be the following poor one, with the last caption line overlapping the
% wrapping text.
%
%\begin{wraptable}[5]{l}
%\centering 
%  \begin{tabular}{cc}
%  \hline
%  First & Second\\
%  Third & Fourth\\
%  \hline
%\end{tabular}
%\caption{A small table}
%\end{wraptable}
%{\itshape \kant[2]}
%
%^^A\goodpagebreak
% \newpage
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \subsection{A wrapped text}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \begin{wraptext}{l}
% \includeframedtext{Text, text, text, text, text, text, text, text, text, text, text.}
% \end{wraptext}
% {\itshape \kant[3]}
% 
% The above example was typeset with this simple code:
%\begin{flushleft}\obeylines
% \Bamb{wraptext}\Marg{l}
% \cs{includeframedtext}\Marg{\ttfamily Text, text, text, text, text, text, text, text, text, text, text.}
% \Eamb{wraptext}
% \Marg{\itshape \cs{kant}[3]}
%\end{flushleft}
%
% The result is the same as that obtainable with version~4.0 of this 
% package where the  \meta{location} argument specification is braced
% instead of bracketed.
%
% If a caption is specified, version~4.0 would print it within the
% framed box, while version 5.0 prints it outside the framed box.
% 
% \begin{wraptext}{r}
% \includeframedtext{Text, text, text, text, text, text, text, text, text, text, text.}
%\caption{A wrapped text}%
% \end{wraptext}
% {\itshape \kant[3]}
%
% The further feature introduced by version~5.0.* is the
% possibility of choosing the colours for all three elements of the
% framed text; if within the environment \Env{wraptext} and before using
% \cs{includeframedtext} the following colours are set, it is
% possible to set different colours from the default light grey for
% the background, the black text, and the almost black frame:
% \begin{ttsintassi}
%\cs{SetWFfrm}\marg{frame colour}
%\cs{SetWFbgd}\marg{background colour}
%\cs{SetWFtxt}\marg{text colour}
% \end{ttsintassi}
%See figure~\ref{fig:framed text}
%
%\begin{figure}
%\includeframedtext[0.45\linewidth]{Text}\hspace{9mm}%
%\includeframedtext[0.45\linewidth]{Text}[3pt,1ex]\\[2ex]
%{\SetWFtxt{red}\includeframedtext[0.45\linewidth]{Text}[2pt,0.6em]}\hspace{9mm}%
%{\Large\includeframedtext[0.45\linewidth]{Text}[1pt,1ex]}\\[2ex]
%{\SetWFbgd{yellow}\includeframedtext[0.45\linewidth]{Text}}\hspace{9mm}%
%{\SetWFfrm{blue}\SetWFbgd{yellow}\includeframedtext[0.45\linewidth]{Text}[3pt,1ex]}%
%\caption{Some framed text boxes with different dimension parameters,
% different font size, and different colours}\label{fig:framed text}
%\end{figure}
%
% As it was previously shown, the syntax of environment \Env{wraptext}
% for version~5.0 is practically identical to the one used for the
% other two environments; actually, as it can be seen below in 
% the code description of this version~5.0, it has a fairly
% longer definition; it is required by the necessity of avoiding 
% the \cs{caption} redefinition by the adjustments foreseen in
% version~4.0, but in effects the new definition of the \Env{wraptex}
% environment uses the same \cs{wrapfloat} and \cs{endwrapfloat}
% commands. 
%
% In all cases the \meta{width} parameter is a braced optional
% argument; for the \Env{wraptext} environment its preset value is
% half the column width, that in one column typesetting mode
% coincides with the text width. The wrapped text is typeset
% in justified mode within a \cs{parbox} argument; the
% measure of this text box should not be too small (unless
% the text is less than one line long) otherwise the inter
% word spacing might be too large; at the same time the measure
% of the mini paragraph cannot be too large, otherwise the indented
% wrapping lines, generally justified, might get a bad word
% spacing. Therefore it is suggested to avoid specifying the
% optional \meta{widh} outside the range of 40\% to 60\% the
% column width. Actually specifying \texttt{0.2\cs{textwidth}}
% or \texttt{0.4\cs{columnwidth}} when typesetting in two column
% mode produces approximately the same result, because
% \cs{columnwidth} is a little less than half the \cs{textwidth}.
% In any case version~5.0 of \Env{wrapfig2} resets any specified
% width outside the above range at the nearest range bound.
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Remarks}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% The syntax of the original environments \amb{wrapfigure} and
% \amb{wraptable} has not been changed, except for a last optional
% star. The fact that the last braced argument is optional does not
% change the backward compatibility with the original environments.
%
% Therefore the optional \meta{line number} argument maintains its
% meaning, unless the optional star is specified; in such a case that
% number assumes the meaning of a correction to the computed number
% of the indented lines.
%
% The mandatory \meta{location} maintains its meaning and the legal
% values are \texttt{l} (left), \texttt{r} (right), \texttt{L}
% (floating left), \texttt{R} (floating right), \texttt{i}
% (inner margin), \texttt{o} (outer margin), \texttt{I}
% (floating inner margin), \texttt{O} (floating outer margin).
% 
% We tested all of them, but as a (possibly questionable) personal
% choice we prefer to place the wrapped object at the left of the text,
% without floating it and irrespective of the page number parity.
%
% As in the previous examples, we prefer to specify the wrapping
% environment just before a sufficiently long paragraph. Should the
% paragraph be too short to completely wrap the object, all
% the environments are capable of counting the number of used indented
% lines and to apply the remaining number and the \cs{overhang} amount 
% to the following paragraph(s); in these circumstances
% it might be necessary to recourse to the optional star in order
% to correct the indention, since the mechanism does not consider
% the inter paragraph spacing that \LaTeX introduces only at ship~out
% time.
%
%\begin{wraptext}{l}
%\includegraphics[width=\hsize]{commodilla}
%\caption{A IX century text written in a language half way between
% Latin and Italian. It is written on the frame of a fresco in the
% Comodilla catacombs, Rome; an apparently free picture is available
% from the internet site of the Bibliotheca Augustana in Germany.
% The text is: «Non dicere ille secrita a bboce».}\label{fig:commodilla}
%\end{wraptext}
%{\textit{\kant[4]}
%
% We avoid also to enter the wrapping environment before paragraphs
% that are close to a page break; this action would tickle the
% idiosyncrasies of the software, and requires moving the wrapping
% environment some paragraphs before or after the preferred one; but
% this can be done only while reviewing the document, because any
% change in the previous source text might change the situation
% if this adjustment is done while still editing the document. 
% 
% Juan Luis Varona Malumbres, whom we thank very much, noticed  that
% if the space left at the bottom of a page is scarse, it may be
% that a section title falls alone at the bottom of the page and
% the wrapping environment with its wrapping text gets typeset on the next
% page; this of course is not acceptable. We found the place to
% correct and this version~5.0 more often than not does not
% exhibit any more this “feature”. Unfortunately in some rare cases
% this “feature” pops up agains; a \cs{newpage} command before the
% section title solves the problem. This rare feature could be avoided
% if the sectioning commands are redefined; but this would imply
% modifications to a large number of redefinitions due to the large
% varieties of classes and packages that redefine such sectioning
% commands.
%
% With the standard environments the optional parameter \meta{overhang} 
% does exactly what its name implies: the wrapped object protrudes
% into the adjacent margin exactly by the specified amount. This
% parameter is not available for the \Env{wraptext} environment,
% or better, it is still available in version~5.0, but we recommend
% to abide from using it; we believe that a wrapped text logically
% pairs the wrapping text; of course this personal opinion might be wrong.
%
%
% The \meta{width} parameter has been already sufficiently described;
% we just remember that for \Env{wraptext} this parameter is optional
% and its default value amounts to half the current measure; this
% insertion width can be specified but it should not be too different
% from its default value $y_0$, set to 50\% of the current measure.
% For the standard environments this parameter value appears to be
% mandatory; actually it is a braced optional argument only for the
% redefined environments \env{wrapfigure} and \env{wraptable}.
%
% Matter of facts, for the \Env{wraptext} environment we defined a
% command in order to specify a factor $x$ so as to avoid getting the
% object width outside the range  $xy_0 \leq y \leq y_0/x$, where $y_0$
% is the preset default width; if the authors specified
% a value outside this range, the above environment automatically
% resets the insertion width $y$ to the nearest bound. Of course authors
% have the possibility to change the preset $x$ value, if they redefine
% the \cs{WFscalefactor} macro, but such resetting is strongly
% discouraged; the default value is~$0.8$.
%
% If optional parameters are not used and the mandatory ones are
% reduced to a minimum (remember the \meta{width}, in spite of being
% braced is optional) the three environments produce the same results;
% the difference, in spite of the nature of the wrapped object differs
% only with the environment name. Text~\ref{fig:commodilla} displays
% an image that contains some text; it is reasonable  to insert it with
% the \Env{wrapfigure} environment, but it is not absurd to insert
% it with the \Env{wraptext} one.
%
%\begin{wraptext}{l}
%\includeframedtext{%
% \foreignlanguage{greek}{Κα᾽γὼ σὲ πατάξας διαλύσω τὸ κρανὶον}}\caption{A sample text in Greek}\label{txt:greek}
%\end{wraptext}
%{\textit{\kant[7]}
%
% The wrapped text may be written also in a foreign language, even
% if it uses a differente alphabet. Evidently this language should
% be specified in the preamble of the author's document, either
% when using \pack{babel} or \pack{polyglossia}. The example
% text~\ref{txt:greek} was typeset with the following code:
%\begin{flushleft}\ttfamily\obeylines
%\Bamb{wraptext}\Marg{l}
%\cs{includeframedtext}\Marg{\%
%\qquad\cs{foreignlanguage}\Marg{greek}\Marg{\foreignlanguage{greek}{Κα᾽γὼ σὲ πατάξας διαλύσω τὸ κρανὶον}}}
%\cs{caption}\Marg{A sample text in Greek}\cs{label}\Marg{txt:greek}
%\Eamb{wraptext}
%\end{flushleft}
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{Other floating objects}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pictures and textual arrays may be floated by means of the standard
% \amb{figure} and \amb{table} environments. But other floating
% objects may be defined by means of other packages, such as
% \pack{float}, or classes, such as \class{memoir}. Besides floating,
% the main difference is the name of the caption “label”: Figure, Table,
% Algorithm, Example, and so on and the lists of such objects.
%
% If floating is not necessary, this package (as well as the original
% one) allows to use the underlying  environment \Env{wrapfloat}
% that uses the same syntax as \Env{wrapfigure} plus the mandatory
% name of the new object: even a figure  might be introduced without
% using \amb{wrapfigure}, by using instead:
%\begin{flushleft}\setfontsize{9.275}\ttfamily\obeylines
%\Benv{wrapfloat}\Marg{figure}\oarg{line number}\marg{placement}\oarg{overhang}\marg{width}\meta{$\star$}
%\quad\qquad\meta{image}
%\Eenv{wrapfloat}
%\end{flushleft}
% Another \meta{object} might be wrapped by using:
%\begin{flushleft}\ttfamily\obeylines
%\Benv{wrapfloat}\marg{object name}\oarg{line number}\marg{location}\%
%\quad \oarg{overhang}\marg{width}\meta{$\star$}
%\qquad\qquad\meta{object}
%\Eenv{wrapfloat}
%\end{flushleft}
%
% By reading the documentation of the original \pack{wrapfig} package,
% it may be assumed that if the floating \meta{location} codes have to
% be used, another floating object with the desired \meta{object name}
% has to be previously  defined by means of the functionalities of other
% packages or classes. But, if the non floating \meta{location} codes
% are used, the presence of another \meta{floating object} environment
% appears to be unnecessary.
%
% This is actually possible by “cheating” a little bit; it can be actually
% wraped any \meta{object} by using the \Env{wrapfigure} environment,
% while assigning a different name to the caption label; something
% similar to typeset a small figure within a non floating environment.
% The obvious draw back is that the caption is numbered as a figure.
%
% In order to avoid such drawbacks and to have a real floating
% \Env{text} environment it is necessary to procede by defining
% a new real floating environment with that name. To do this task,
% \pack{wrapfig2} version~5.0 uses the \pack{float} package.
%
% As it is possible to verify by reading the section where the
% code is documented, the operation is not that simple because
% \pack{float} redefines several internal macros that are incompatible
% with both \pack{wrapfig} and \pack{wrapfig2}. This is why, even 
% with \pack{wrapfig2} in version~5.0, that loads the \pack{float}
% package, the code for this environment  redefines the \cs{caption}
% command so that Arseneau had to define some adjusting macros in order
% to deal with something different from what it was with the \LaTeXe
% kernel. We did not modify what Arseneau defined in although it did not
% work correctly with the new \Env{text} floating environment. Therefore
% we reinstated the \LaTeXe kernel relevant definitions.
%
% It is possible that such resetting of the original definition is
% necessary also with floating objects defined by other means, for
% example by using the functionalities of \class{memoir}. We admit
% we did not test this package functionality with class
% \class{memoir}; feedback on this compatibility issue is very
% welcome 
% 
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section*{Acknowledgements}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% We gratefully thank  Donald Arseneau  who gave the \TeX community
% the original \Env{wrapfig} package. 
%
% Thanks to Heinrich Fleck who submitted to our attention the
% \texttt{texstackexchange} message where the problem of wrapping text was
% presented possibly for the first time. The solution presented in
% \texttt{texstackexchange} appears to be oversimple, almost trivial;
% especially it does not solve the problem of a caption if one is desired 
% under the wrapped text.
% Moreover the solution of \texttt{texstackexchange} uses an almost
% trivial usage of the \Env{tcolorbox} environment, that behind the
% scenes uses a very heavy set of multifunctional macros the offer
% functionalities are not required for this problem.
%
% Warm thanks also to Juan Luis Varona Malumbres for his precious
% feedback and his suggestions. 
%
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \CheckSum{1345}
% \StopEventually{}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%\iffalse
%<*package>
%\fi
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \section{The code}
%^^A%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Here we describe and comment the code of this package; essentially 
% only the initial parts need some comments; because the final ones
% are almost identical to Arseneau's original code.
%
% The usual specification of the format name and date, and the
% identification of this specific package have been already specified
% by the \texttt{.dtx} file.
%
% First of all we check if certain packages have already been
% loaded; some of these packages, such as \pack{wrapfig}, that
% might have been previously directly loaded, or might have been
% loaded by other packages, are incompatible with this new version 
% (and also with the previous versions, but the problem was less
% serious). That package might have been loaded by other packages,
% such as \pack{caption} or \pack{subcaption}, that redefine some
% internals that we did not want to replace so as to avoid other
% possible incompatibilities. We first check if a specific macro
% with the \texttt{WF} prefix has already been defined; if so,
% this package loading is aborted with a very evident error
% message. In contrast the job is not aborted, because the presence
% of the original \pack{wrapfig} package might still be sufficient;
% evidently there will be many errors if some new user commands
% or environments are used.
%
%{\color{red}\paragraph{Caution} besides the evident error message,
% that might be neglected by the user, the job may continue but it
% may produce several errors difficult to interpret. Please,
% in these cases read the \texttt{.log} file and look for error
% messages; there you are going to discover what has gone wrong
% with your way of using this package.}
%
%    \begin{macrocode}
\ifcsname c@WF@wrappedlines\endcsname
\PackageError{wrapfig2}{                              \MessageBreak
  ****************************************************\MessageBreak
  Package `wrapfig' has already been loaded perhaps   \MessageBreak
  by other packages, for example caption or subcaption.\MessageBreak
  Such packages are incompatible with wrapfig2        \MessageBreak
  Loading `wrapfig2' aborted                          \MessageBreak
  ****************************************************\MessageBreak
}{You might type X <return> and might get along without\MessageBreak
  this package if you don't use the new environment   \MessageBreak
 `wraptext'; otherwise you get errors about such      \MessageBreak
  environment not being defined; you must kill your job!}
\expandafter\endinput\fi

%    \end{macrocode}
% We keep the original definition of the \cs{WF@warning} and the
% original definition of the \opt{verbose} option; but we add the
% new \opt{WFold} option in order to fall back to the functionalities
% of the previous version~4.0, at least for what concerns the
% \Env{swraptext} environment.
%
% 
%    \begin{macrocode}
\def\WF@warning{\PackageWarning{wrapfig2}}
\DeclareOption{verbose}{\def\WF@info{\PackageInfo{wrapfig2}}}
\newif\ifWFnew \let\ifWFnew\iftrue
\DeclareOption{WFold}{\let\ifWFnew\iffalse}
\ProcessOptions

%    \end{macrocode}
%
% We load the \pack{etoolbox} package, in order to have available
% its powerful macros. 
%
% If is was not previously loaded, we load the \pack{xfp} package,
% that allows us to perform precise calculations. Loading the
% \pack{xparse} package is necessary in order to use one of its
% rare features that did not migrate to the \LaTeX kernel. From the
% \LaTeX News Letter dated October 2020:
%\begin{quote}
% Most, but not all, of the argument types defined by \pack{xparse}
% are now supported at the kernel level. In particular, the types
% \texttt{g/G}, \texttt{l} and \texttt{u}, are not provided by the
% kernel code; these are \emph{deprecated} but still available by
% explicitly loading \pack{xparse}. All other argument types are
% now available directly within the \LaTeXe kernel.
%\end{quote}
% Their availability eases the treatment of the backwards
% compatibility of this software with the original \pack{wrapfig}
% functionality. It deals with the mandatory \meta{width} argument
% of the \Env{wrapfigure}, \Env{wraptable}, the new \Env{wraptext},
% and \Env{wrapfloat} environments, where it was possible to specify
% a zero value. Now it is possible to omit it completely because it
% is a \emph{braced optional argument} with a \texttt{0pt} default value. 
%
%
%    \begin{macrocode}
\RequirePackage{xparse}
\@ifpackageloaded{xfp}{}{\RequirePackage{xfp}}
\@ifpackageloaded{etoolbox}{}{\RequirePackage{etoolbox}}
\@ifpackageloaded{float}{}{\RequirePackage{float}}
\@ifpackageloaded{color}{}{%
   \@ifpackageloaded{xcolor}{}{\RequirePackage{xcolor}}%
 }
\@ifpackageloaded{curve2e}{}{\RequirePackage{curve2e}}

%    \end{macrocode}
% Notice that we loaded the \pack{xcolor} package without any option
% in order to avoid option clash errors, and users who want to use
% \pack{xcolor} with options should load it \emph{before} this package
% \pack{wrapfig2}, version~5.0.*. {\color{red}Users are warned
% to pay attention to this \pack{color} package loading: they should load
% it neither before nor after loading \pack{wrapfig2}; if they do, they
% receive various warning or error messages because \pack{xcolor}
% redefines some \pack{color} internal commands; everything is
% explained in the \pack{xcolor} documentation.}
%
% In order to define the new floating object \texttt{text} we have
% to load the package \pack{float}, but only if version~5.0.* 
% is used; in other words only if the \cs{ifWFnew} switch
% is \texttt{true}.
%
% If the \cs{chapter} command is or is not defined we have to load
% the \pack{float} package with different options; for example, if
% we are using the \class{article} class, the \cs{chapter} command
% is undefined, and the last option might become \opt{section} (although
% in the standard \class{article} class, no floating object counter
% belongs to any other counter reset list) so as to have the floating
% \Env{text} environment correctly reset the right counter with the
% right label before the object number.
%    \begin{macrocode}
\ifWFnew
  \floatstyle{plain}
  \ifcsname chapter\endcsname
    \newfloat{text}{tbp}{lotx}[chapter]
  \else 
    \newfloat{text}{tbp}{lotx}
  \fi
  \floatname{text}{Text}
  \let\WF@text@caption\float@caption
\fi

%    \end{macrocode}
%
% If the users wanted to add the \texttt{text} counter to some
% sectioning command counter reset list they might use the
% \cs{counterwithin} command now available with the recent updates
% of the \LaTeX kernel; see the \LaTeX newsletter~28 for details
% (terminal command \texttt{texdoc ltnews28}). Its syntax is the following:
%\begin{ttsintassi}
%\cs{counterwithin}\marg{counter}\marg{main counter reset list}
%\end{ttsintassi}
%
% Next we define some dimensions, boxes, token registers, \TeX counters,
% and alias names, plus some color and macro definitions.
% The \cs{WF@correctlines@switch} \TeX numeric register
% (not a \LaTeX counter) is going to be used as a boolean switch: if its
% value is zero, il means “false”, otherwise it is “true”; in the other
% definitions below,  it will be set only to 0 or 1, depending on the
% presence of the optional star.
%    \begin{macrocode}
\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
\let\@@parshape\parshape
\let\WF@@everypar\everypar

\newdimen\insertwidth 
\newdimen\WFinsertwidthL
\newdimen\WFinsertwidthH

\definecolor{WFbackground}{rgb}{0.95,0.95,0.95}
\definecolor{WFframe}{rgb}{0.1,0.1,0.1}
\colorlet{WFtext}{black}
\def\SetWFbgd#1{\colorlet{WFbackground}{#1}}
\def\SetWFfrm#1{\colorlet{WFframe}{#1}}
\def\SetWFtxt#1{\colorlet{WFtext}{#1}}
\def\WFsplitdimens#1,#2!{\fboxrule=#1\relax\fboxsep=#2\relax}

\def\WFscalefactor{0.8}%
\newcommand*\WFscalewidth{%
  \WFinsertwidthL=\fpeval{\WFscalefactor*0.5\columnwidth}\p@
  \WFinsertwidthH=\fpeval{0.5\columnwidth/\WFscalefactor}\p@
  \ifdim\insertwidth<\WFinsertwidthL
    \insertwidth=\WFinsertwidthL
  \else
    \ifdim\insertwidth>\WFinsertwidthH
      \insertwidth=\WFinsertwidthH
    \fi
  \fi
}%

%    \end{macrocode}
%
%
% {\color{red}Should the format file be not so up to date, a multitude
% of errors would be produced, and the user should take care to load the
% \pack{xparse} and \pack{xfp} packages before loading \pack{wrapfig2}.
% Notice that most of the \pack{xparse} package functionalities
% at the date required for the format file are already included. The
% \pack{xparse} package has been available since about 2018; should the
% users have available a definitely older \TeX system installation, they
% should upgrade it, or must avoid using this \pack{wrapfig2} package
% and use the original \pack{wrapfig} one; if they need to wrap text,
% they should resort to some ingenious, not so trivial, tricks to do it.}
%
% Originally version~4.0 used the \pack{tcolrbox} package to frame
% the wrapped text; we thought that loading that package was too
% heavy on memory, even if the modern computers have large working
% memories. But in order to maintain and track possible errors
% the traced \texttt{.log} file would become too large to be of any
% help; therefore in oder to draw a framed box with rounded corners
% we thought it would be much simpler to load the \pack{curve2e}
% package, just a second level extension of the original \Env{picture}
% environment defined in the \LaTeXe kernel; just some 30 lines of code
% are sufficient to replace the extremely powerful \pack{tcolorbox}
% functionalities needed by this \pack{wrapfig2} package. 
%
%
% The definitions of the \Env{wrapfigure} and \Env{wraptable}
% environments are very simple by means of the underlaying
% \Env{wrapfloat} environments.
%    \begin{macrocode}
\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}
  
%    \end{macrocode}
% 
% Notice that the argument descriptor \texttt{s} for the optional
% star is not present in these definitions; if a star is being used,
% it will be read by successive macros or environments.
%
% In order to include the text to be wrapped the floating object
% \Env{text} has already been defined, but we need a suitable command
% to insert it with its frame into the \Env{wraptext} environment body.
% Here is the code of some extra macros and of the \Env{wraptext}
% environment.
%    \begin{macrocode}
\NewDocumentCommand\includeframedtext{O{\insertwidth} m O{1pt,1ex} o}%
{\bgroup \WFsplitdimens #3!% 
\insertwidth=#1\relax
\IfNoValueTF{#4}%
  {\framedbox{#2}{\fboxrule}{\fboxsep}}%
  {\framedbox{#2}{\fboxrule}{\fboxsep}[#4]}%
\egroup}

%    \end{macrocode}
%
%
% Its simple syntax is the following
%\begin{ttsintassi}
%\cs{includeframedtext}\oarg{text width}\marg{text}\oarg{settings}\oarg{radius}
%\end{ttsintassi}
% 
% The optional \meta{text width} is the (possibly scaled) width computed
% by the \Env{wraptext} environment; but if the authors use this command
% outside the \Env{wraptext} environment, they should specify a width;
% in any case the default value is half the current measure
% \cs{linewidth}. The \meta{text} is the unformatted text to be wrapped;
% it will be boxed and framed by the service macro \cs{framedbox}; the
% \meta{settings} are passed on to \cs{framedbox} command; they are a
% comma separated list of dimensions, namely the thickness of the frame
% and the necessary distance of the frame from the formatted text; the
% last optional \meta{radius} is the curvature radius of the rounded frame
% corners; no default value is specified, because it is going to be
% received by \cs{framedbox} that by default sets it equal to the frame
% separation width; this value is certainly the best one, but the user
% can specify a different value, of course not too different from the
% default one. See some examples in figure~\ref{fig:framed text}.
%
% The definition of the \cs{framedbox} command appears to be
% complicated; it is just an apparent complication due to the
% fact that it uses the powerful \cs{Curve} command that draws an
% arbitrary curved line or fills the area delimited by the
% curved line; it suffices to specify the nodes and the tangents
% to each node; the nodes are the points the line should pass
% through, their tangents may be specified with arbitrary vector
% components along the horizontal and vertical axes. For a
% rectangle such vector componente are just 0 or $\pm 1$. The
% node coordinates, on the opposite, must be determined with
% accuracy; we used the \cs{fpeval} function of package \pack{xfp},
% that performs precise operation on operands in fractional
% decimal numbers; if the operands are dimensions, the operands
% are their fractional values in printer points, the results of
% such operations are  pure fractional decimal numbers without units;
% if the numerical result is to be interpreted again as the measure of a
% dimensional entity, \texttt{pt} must be appended to the
% assignments to a dimension register; within the \Env{picture}
% environment, any coordinate is expressed in multiples of 
% \cs{unitlength}.
%
% For a rectangle with curved corners of a given radius \cs{R}
% we have four quarter circles joined by straight lines; therefore
% we need eight nodes.
%
% The shaded background and the coloured frame have the same contour;
% but the former is filled, while the latter is stroked; we have to
% draw the same curve two times; first the shaded background, then
% the frame.
%
% This shaded framed rectangle is at the center of the coordinate
% system of a \Env{piture} environment, and has the correct dimensions
% to receive the boxed text; it is trivial to center the text within a 
% zero dimensioned box, typical of the \Env{picture} environment.
% 
% The code of this long but simple code is the following.
%    \begin{macrocode}
\NewDocumentCommand\framedbox{ m m m O{#3}}{\bgroup
\fboxrule=#2\fboxsep=#3\relax
\setbox0\hbox{\fboxrule=0pt\fboxsep=#3\relax
\framebox{\parbox{%
  \fpeval{\insertwidth-2\fboxrule-2\fboxsep}pt}{\textcolor{WFtext}{#1}}}}%
%
\unitlength=\fpeval{\wd0/100}pt 
\edef\x{100}\edef\y{\fpeval{(\ht0 +\dp0)/\unitlength}}%
\edef\xc{50}\edef\yc{\fpeval{\y/2}}\edef\R{\fpeval{#4/\unitlength}}% 
%
\edef\WFXds{\fpeval{-\xc+\R}}\edef\WFXsd{-\WFXds}% 
\edef\WFYuo{\fpeval{\yc-\R}}\edef\WFYou{-\WFYuo}%
%
\edef\PSEl{\WFXsd,-\yc}\edef\PSEu{\xc,\WFYou}\edef\PNEd{\xc,\WFYuo}%
\edef\PNEl{\WFXsd,\yc}\edef\PNWr{\WFXds,\yc}\edef\PNWd{-\xc,\WFYuo}%
\edef\PSWu{-\xc,\WFYou}\edef\PSWr{\WFXds,-\yc}%
%
\def\WFrectangle{%
  (\WFXsd,-\yc)<1,0>(\xc,\WFYou)<0,1>(\xc,\WFYuo)<0,1>%
  (\WFXsd,\yc)<-1,0>(\WFXds,\yc)<-1,0>(-\xc,\WFYuo)<0,-1>%
  (-\xc,\WFYou)<0,-1>(\WFXds,-\yc)<1,0>(\WFXsd,-\yc)<1,0>}%
\def\CurveStar{\Curve*}%
%
\begin{picture}(\x,\y)(-\xc,-\yc)
{\color{WFbackground}\expandafter\CurveStar\WFrectangle}%
{\color{WFframe}\linethickness{#2}\expandafter\Curve\WFrectangle}%
\put(0,0){\makebox(0,0)[cc]{\box0}}%
\end{picture}
\egroup}

%    \end{macrocode}
% Its syntax is the following.
%\begin{ttsintassi}\setfontsize{8.5}\obeylines
%\cs{framedbox}\marg{text to be wrapped}\marg{frame thickness}\marg{frame separation}\oarg{corner radius}
%\end{ttsintassi}
%
% The default value of the \meta{corner radius} is assigned to
% equal argument number~3, that is the \meta{frame separation}; both
% have a default value of \texttt{1ex} therefore they vary with the
% current font size. See figure~\ref{fig:framed text}. The frame
% thickness is given a default value of \texttt{1pt} if the command is
% used within the body of the \cs{includeframedtext}; but if this
% command received a different value the frame may be thicker,
% or even vanish; we discourage values higher than \texttt{3pt}
% (about \texttt{1mm}) and lower than \texttt{1pt}.
%
% The definition of the \Env{wraptext} environment is more detailed,
% because most of the computations must be done on the actual text
% to be wrapped, that does not have a specific width; moreover the
% inserted text must not be too wide, nor too short in order to avoid
% problems with its justification or the justification of the wrapping
% lines. The framed box width is preset to 50\% of the normal text
% measure, but it can be optionally specified to a different value (not
% too different from 50\%); as with the other wrapping environments, the
% inserted material width is a \emph{braced optional parameter}.
%
% For what concerns \Env{wraptext}, the first statement argument
% description list does not contain any descriptor for an optional
% star. There is no need because the computation of the insertion
% block height is pretty precise and at most the user might desire
% one line more or less depending on the  measure of the whole text,
% and that of the inserted block and/or the measure of the indented
% wrapping lines; sometimes it might be necessary to get rid of the
% space below the inserted block when it gets typeset at the bottom
% of a page.
%
% It is true that some of the input parameters specified to the opening
% command of any environment with \LaTeXiii are available also to the
% closing commands; see the last paragraph of section~2 in the
% \pack{xparse} documentation. 
% But the following definition, besides using special delimiters for
% optional parameters, uses the separate opening and closing
% macros  of the \Env{wrapfloat} environment; such procedure
% breaks this second availability of the input parameters,
% therefore it is necessary to save them into local macros or count
% registers (assignments to \TeX count registers are \emph{local},
% while assignment to \LaTeX\ named counters, through the
% \cs{setcounter} macro and its siblings, are \emph{global}) so 
% that we can use their values within the closing commands.
%
% The \cs{NWF@box} box register has been allocated at the beginning;
% remember that \LaTeXiii registers of any kind are not limited in number
% as they were some years ago with \LaTeXe.
% 
% The last opening commands are conceived to box the object to be
% wrapped, typeset within a coloured box with the default of
% specified width; compared to version 4.0.*, this new version~5.0.* 
% does not use anymore the functionalities provided by package
% \pack{tcolorbox}; instead it uses the \pack{curve2e} package
% functionalities to draw similar framed and shaded boxes; this
% implies much less memory used by the almost unlimited,
% but unused, functionalities of the \pack{tikz} package on which
% \pack{tcolorbox} relies.
%
% Notice the \Env{wraptext} has two definitions depending on the
% logical switch \texttt{WFnew}; if this switch is \texttt{true}
% the definition applies to version~5.0.* of this package; otherwise
% the second definition is a fall back to the functionality and the
% syntax of the previous version~4.0.* implementation of this package;
% in this latter case, in spite of the fact the the previous version
% used package \pack{tcolorbox}, the new code relies on the
% \pack{curve2e} functionalities in oder to get the same results
% without using the memory heavy \pack{tcolorbox} package.
%
% With this new solution the frame that surrounds the wrapped text
% has the desired rounded corners; it is possible to easily
% specify the background and the frame colours, that by default
% are maintained to light grey and black respectively; this new
% flexibility allows to set also the frame thickness and the
% separation width of the frame from its contents; by default
% they are~1pt for the frame thickness, and~1ex for the separation
% width, that changes with the size of the surrounding fonts.
% The number of indented lines is computed by  means of the
% \cs{fpeval} \LaTeXiii function; among the operands of this
% function there is the number \texttt{2} used to take into
% account the vertical space above and below the framed box.
% It is possible that a value of \texttt{3} might reduce the
% probability of using the \meta{line number correction}. But
% it is not always true and we found that the chosen value is
% a better choice.
%
% Eventually the opening \Env{wrapfloat} statement is created by
% expanding the whole line complete of arguments, by means of the
% usual trick of defining a dummy macro within a group that contains
% among its expansion the group closing command, so that while
% it is being executed, it deletes itself from memory.
%
% Notice that the syntax of the new \Env{wraptext} environment is
% absolutely identical to that of the other two wrapping environments
% in terms of \meta{line number correction}, mandatory \meta{location},
% optional \meta{overhang} and braced optional \meta{width}. See below
% for the very different syntax of the fall back version~4.0.* opening
% \Env{wraptext} statement; they become almost identical when no
% optional parameters are specified, the only little difference being
% that the \meta{location} argument is mandatory for version~5.0.*,
% while it is optional for version~4.0.*.
%    \begin{macrocode}
\ifWFnew  
\NewDocumentEnvironment{wraptext}{O{0} m O{0pt} G{0.5\columnwidth}}{%
  \insertwidth=#4\WFscalewidth
  \def\textplacement{#2}%
  \def\textcorrection{#1}%
  \def\textoverhang{#3}%
  \bgroup\edef\x{\egroup\noexpand\wrapfloat{text}%
    [\textcorrection]{\textplacement}[\textoverhang]{\insertwidth}*}\x%
  \def\caption{\unskip 
    \refstepcounter\@captype
    \let\@tempf\@caption
    \unless\ifcsname @float@c@\@captype\endcsname
      \expandafter\expandafter\let
        \expandafter\@tempf\csname @float@c@\@captype\endcsname
    \fi
    \@dblarg{\@caption\@captype}%
    }%
}{\endwrapfloat\ignorespaces}%
%    \end{macrocode}
%
% For the fallback definition of this \Env{wraptext} environment 
% we have to start with the old list of specifically delimited
% optional arguments. We remember that this opening statement
% receives in order a bracket delimited \meta{location} parameter,
% a vertical bar delimited optional \meta{width}, an angle bracket
% delimited \meta{line number correction}, a round parenthesis
% delimited \meta{caption label}; the different delimiters allow
% to specify any optional argument without regard with the other
% ones, provided they are in the same logical order when more than one
% optional argument is specified.
%
% Notice that the third optional argument contains the
% \meta{line number correction}, therefore the star used with the
% other environments is useless; if an asterisk is indadvertedly
% specified, it is typeset as the first token of the wrapped test.
%
% The text to be wrapped, that forms the body of the environment,
% must be first boxed into a correct width vertical box; this is
% easily obtained with a \Env{minipage} environment, of which the
% internal commands are used; this insures that the text is
% typeset with the correct measure; with the closing commands 
% this boxed text shall be fed to the \cs{framedbox} command, in
% oder to be framed and assigned a default background color.
% There is no possibility of specifying the colours unless the
% whole \Env{wraptext}  environment, preceded by explicit color
% settings, is confined within a group delimited, for example
%, by the  \cs{begingroup} and \cs{endgroup} commands.
% \begin{macrocode}
\else % 
% 
  \NewDocumentEnvironment{wraptext}%
        {O{l}    D||{0.5\columnwidth} D<>{0}   D(){text}    }%
  {% 
    \insertwidth=#2
    \def\textplacement{#1}%
    \def\textcorrection{#3}%
    \def\WF@caption@label{#4}%
    \setbox0\hbox\bgroup
      \minipage{\dimexpr\insertwidth-2pt-6ex}%
}{\endminipage\egroup
    \begin{lrbox}{\NWF@box}%
      \framedbox{\box0}{1pt}{1ex}%
    \end{lrbox}
    \edef\NWF@wli
      {%
        \fpeval{%
                round((\ht\NWF@box+\dp\NWF@box)/\baselineskip,0)+2+
                \textcorrection
               }%
      }%  
    \unles\ifhmode
      \leavevmode\noindent
    \fi
    \bgroup\edef\x{\egroup\noexpand\wrapfloat{\WF@caption@label}[\NWF@wli]%
        {\textplacement}{\the\insertwidth}}\x
      \box\NWF@box 
    \endwrapfloat
    \ignorespaces
  }
\fi

%    \end{macrocode}
%
% The opening command of the \Env{wrapfloat} environment receives
% the mandatory and optional arguments plus the name of the particular
% object to be wrapped. It is used to define the prefix label of the
% caption number in case that the object is described with a caption. The
% optional star is not explicit, because it is going to be read by
% the \cs{WF@wr} macro.
%
% The closing command of \Env{wrapfloat} performs most of
% the work necessary to wrap the box that contains the object
% to be wrapped, but certain tasks are demanded to other service
% macros.
%
% It is possible to  set the width of the box if the \meta{width}
% parameter is specified; otherwise it closes the \cs{hbox} that was used;
% then it closes the main vertical box \cs{WF@box}. After executing
% \cs{WF@floatstyhook}, necessary when package \pack{float.sty}
% has been used, it saves the \meta{overhang} value to be used when
% wrapping is actually performed; then it verifies if the box height
% is too high to fit, or is too short; possibly re-boxes this box
% in the same box register with a negative initial vertical skip
% that raises the box contents.
%
% Now comes the definition of the fundamental environment \Env{wrapfloat};
% compared to the original Arseneau's definition it is much longer,
% but it contains the code that Arseneau, who used the \LaTeXe
% language, had to split in several macros in order to handle the
% multitude of interspersed mandatory and optional arguments.
%
% The main function of this environment is to handle the box that
% contains the figure, or the table, or the framed text, or what
% else, so that the inserted box is preceded and followed by 
% suitable vertical spaces, and it is possible to compute the
% number of lines to be indented; often this computed number is correct;
% but in certain cases, when the code is used too close or
% within prohibited wrapping text, such number might need to be
% corrected. As it can be seen the optional star is not among
% the argument descriptors of the opening commands; it will be
% the following macro \cs{WR@wr} responsible of taking care of
% the list of arguments and see if a star has been specified and
% not yet read by the preceding commands.
%
% In order to handle any kind of wrapped object, this environment
% first argument is the \meta{caption label}. It may remain blank;
% but for wrapping figures or tables their respective definitions
% specify the name of the floating object they belong to; it is 
% necessary that there exists a floating environment with the same
% name of the wrapped object, even  when its wrapping environment
% is not specified with a floating \meta{location} argument.
%
%    \begin{macrocode}

\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
  }

%    \end{macrocode}
%
% The working macro \cs{WF@wr} is defined with the \LaTeXiii
% language; it grabs all the optional and mandatory arguments
% in a single step, contrary to \LaTeXe that requires to split
% the various steps in separate macros. In the definition code
% we use also some commands, such as \cs{unless}, originally
% defined by the \eTeX typesetting program extensions, that have
% been included in the \pdfLaTeX, \XeLaTeX and \LuaLaTeX kernels
% several years ago.
%
% Notice that the optional first (optional) argument, that represents
% the number of indented lines or their correction number, is saved
% into the macro \cs{WF@wli}, but if this argument is not specified,
% \cs{WF@wli} is assigned the value zero. The same happens for the
% \meta{overhang} optional argument.
%
% After these adjustments, the software computes the box total height
% plus some fixed amounts needed mostly to set the wrapped material
% below the wrapping text first line. Here is where the \LaTeXiii
% \cs{fpeval} computing function comes into play so as to assign such
% height to \cs{WF@size}. Afterwards some unusual macros are executed;
% they were devised by Arseneau to deal with possibly floating wrapped
% objects. The optional star is not accepted by this macro; if the user
% specified it, it is still in the input flux; notice that the
% \Env{wraptext} environment in version~4.0 does not accept the
% optional star; if the user specifies it for this environment, an
% asterisk appears at the beginning of the wrapped text; with
% version~5.0 the asterisk appears at the left of the framed text.
% In both cases, in facts, the first optional parameter is always
% interpreted as the \meta{line number correction}.
%
% The braced \meta{width} fourth parameter (actually a
% \textit{braced optional parameter}) may be specified
% to be \texttt{0pt}; in any case \texttt{0pt} is the default parameter
% value; if so, the object is treated at its natural width, by
% boxing it into an hbox and using this box width as the working width.
%    \begin{macrocode}
\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
}

%    \end{macrocode}
%
% At this point the main box \cs{WF@box} is opened in order to
% store the object to be wrapped; with this box height the software
% is going to compute the number of lines to be indented, unless
% such a number has been specified and no star was added to the
% input parameters.
%
% Also the \amb{wraptext} environment uses a box to collect the
% framed text; the name of this second box must be different from
% \cs{WF@box} otherwise interference of the various tasks produces
% unrecoverable errors. This is why at the beginning of this
% package we defined two different boxes: \cs{WF@box} and \cs{NWF@box}.
%
% The trick of creating an alias for the \cs{caption} macro is
% used by Arseneau to redefine one of the two macros according to
% certain conditions. Here \cs{wf@caption} is actually redefined
% if the \meta{width} parameter has been specified.
%    \begin{macrocode}
\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
}

%    \end{macrocode}
%
% One of these unusual macros was introduced by Arseneau
% to deal with paragraph parameters and possibly to float the
% object to be wrapped.
%    \begin{macrocode}
\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
}}

%    \end{macrocode}
% The following macro is for floating wrapping environments.
%    \begin{macrocode}
\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}

%    \end{macrocode}
%
% On the contrary if there is enough space or if the wrapped object
% cannot float, it gets output here.
%    \begin{macrocode}
\def\WF@putfigmaybe{%
\ifinner
  \vskip-\parskip \global\WF@floatfalse
  \let\pagetotal\maxdimen %              kludge flag for "not top of page"
\else %                                                         outer page
  \unless\ifWFnew \@tempdima\pagedepth \fi%                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 l or r; determine 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
}
%    \end{macrocode}
%
% Here comes the very important macro that counts the wrapping indented
% lines, so that wrapping is correct; of course the limitations
% of the \LaTeX processing (needed to ship out a complete
% page) forbid to take into account the spaces inserted between
% paragraphs and/or those inserted between entries of various
% listings. The idiosyncrasies of this package arise from the
% fact that this macro cannot preview actions that have not
% yet taken place when this macro is executed.
%
% This macro counts the lines to be indented by rounding the division
% of the box height by the current base line skip. Notice that
% \texttt{WF@wrappedlines} is the name of a \LaTeX named counter,
% not of a \TeX numeric register; therefore special \LaTeX commands,
% such as \cs{setcounter} or \cs{value}, have to be used in order
% to  set or access the numerical value stored within the \TeX
% register associated to the \LaTeX counter name.
%    \begin{macrocode}
\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}

%    \end{macrocode}
%
% The next macro is the one that actually indents the wrapping text lines
% and keeps track of the number of such processed lines. It can work
% on more than a single paragraph. It resorts to service macros that
% reiterate as long as the number of indented lines is lower than the
% computed number of lines. Possibly this process could be defined by
% means od the \texttt{dowhile} or \texttt{whiledo} \LaTeXiii functions.
% By now we did not afford this task, because first we would like to
% see if the overall software is reliable.
%    \begin{macrocode}
\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
}

%    \end{macrocode}
%
% This macro resets the paragraph properties and terminates the job.
%    \begin{macrocode}
\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
}

%    \end{macrocode}
%
% These macros modify the paragraph settings.
%    \begin{macrocode}
\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
}

%    \end{macrocode}
%
% The following macro is the one that actually ends the single
% wrapping job.
%    \begin{macrocode}
\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
}

%    \end{macrocode}
%
% At the very end everything is restored, and the used boxes are emptied.
%    \begin{macrocode}
\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@
}

%    \end{macrocode}
%
% The following code is one of those “dirty tricks” by which a macro 
% defined within a group is executed with the help of an 
%\cs{expandafter} command that bypasses an \cs{endgroup}; by so doing,
% after execution nothing local to the group remains in memory.
%    \begin{macrocode}
\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

%    \end{macrocode}
%
% Donald Arseneau classifies the following macro as the one
% that “pampers the RevTeX's stupidity”.
%    \begin{macrocode}
\@ifundefined{@capwidth}{\let\@capwidth\hsize}{}% 

%    \end{macrocode}
%
% This one, instead, issues a warning if a specific name conflicts
% with another.
%    \begin{macrocode}
\def\WF@conflict{\WF@warning
  {\WF@wfname used inside a conflicting environment}}%

%    \end{macrocode}
%
% While this one issues a warning when a wrapping environment is
% too close to another one.
%    \begin{macrocode}
\def\WF@collision{\WF@warning{Collision between wrapping environments}}%

%    \end{macrocode}
%
% And this one is when two wrapping environments are too close to
% one another so that the second one is forced to move.
%    \begin{macrocode}
\def\WF@fltmes{%                                        message for floats
  \ifWF@float 
    \WF@info{\WF@wfname floats}%
 \else 
   \WF@warning{Stationary \WF@wfname forced to float}%
 \fi
}

%    \end{macrocode}
%
% These two aliases are just service macros for this package; in
% particular, the second one is used to insert info of any kind
% within a source file.
%    \begin{macrocode}
\let\WF@warning\@warning
\let\WF@info\@gobble

%    \end{macrocode}
%
% Arseneau says that his \pack{wrapfig} package is already compatible
% with  package \pack{float.sty}, since, after defining a new float
% \meta{foo}, it suffices to define the new environment
% \Env{wrap\meta{foo}}. This fork version of his package should do
% the same: is suffices to mimic the definitions of environments
% \Env{wrapfigure} or \Env{wraptable}.
%
% Here there is some Arseneau's code that renders his \pack{wrapfig}
% code compatible with \cs{newfloat} of class \class{memoir}, and
% with \cs{newfloatlist} of package \pack{ccaption}. We leave his code
% hereafter; but we did not test it with this package.
% \begin{macrocode}
\let\WF@floatstyhook\relax
%
\@ifundefined{newfloat}{}{%         \newfloat comes from somewhere besides
%                                                                float.sty
  \@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
%             Changing \floatstyle or \restylefloat changes \newfloat too.
  \@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
%                                                          takes width arg
      \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

%    \end{macrocode}
%
%\iffalse
%</package>
%\fi
% \Finale
%
%^^A \endinput