summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/swfigure/swfigure.sty
blob: f63332322e5553403ebc53665513d920671cad98 (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
%%
%% This is file `swfigure.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% swfigure.dtx  (with options: `package')
%% 
%%   Copyright (C)  2020 Claudio Beccari  all rights reserved.
%%   License information appended
%% 
\NeedsTeXFormat{LaTeX2e}[2020/01/01]
\ProvidesPackage{swfigure}%
        [2020-12-23 v.0.9.18 Managing large and spread wide figures]
\RequirePackage{etoolbox}
\RequirePackage{xfp}

\ExplSyntaxOn
\ProvideExpandableDocumentCommand\CompStrings{m m}{%
  \str_if_eq_p:nn{#1}{#2}}

  \ProvideExpandableDocumentCommand\fptest{m m m}{%
     \fp_compare:nTF{#1}{#2}{#3}}

  \ProvideExpandableDocumentCommand\SetList{m m}{%
  \seq_clear_new:N #1
  \seq_set_from_clist:Nn \DisplayModeList {#2}}

  \ProvideExpandableDocumentCommand\TestList{m m m m}{%
  \seq_if_in:NnTF #1 {#2}{#3}{#4}}

\ExplSyntaxOff

\RequirePackage{graphicx}
\RequirePackage{afterpage}
\RequirePackage{wrapfig}

\newdimen\internalmargin
    \internalmargin=\dimexpr\oddsidemargin+1in+1bp\relax
\newdimen\externalmargin
    \externalmargin=\dimexpr\evensidemargin+1in
\newdimen\spreadwidth
    \spreadwidth=\dimexpr 2\textwidth+2\internalmargin\relax
\newdimen\DFwidth
\newdimen\DFheight
\newdimen\DFhalfwidth
\newdimen\DFheight
\newdimen\DFhalfheight
\newdimen\FigSpace
\newdimen\TScaptionwidth
\def\SWcaptionShift{1em}%
\def\FScaptionShift{2em}%
\newsavebox\DFtotalimage
\newsavebox\DFimageI
\newsavebox\DFimageII
\newsavebox\RFbox
\newdimen\VS@textwidth
\newcount\VS@lines
\newif\if@SWtmode

\NewDocumentCommand\DFcaption{O{#2} m o}{\refstepcounter{figure}%
  \vtop to 0pt{\hsize=\textheight\parindent=0pt\leavevmode
  Figure \thefigure\quad #2\vss}%
  \addcontentsline{lof}{figure}{\protect\numberline{\thefigure}#1}
  \IfValueT{#3}{\label{#3}}\relax%
}
\NewDocumentCommand\DFcaptionP{O{#2} m o D!!{\color{black}}}%
  {\refstepcounter{figure}%
  \vbox to 0pt{\vss\hsize=\textheight\parindent=0pt\leavevmode
  #4\relax Figure \thefigure\quad #2}%
  \addcontentsline{lof}{figure}{\protect\numberline{\thefigure}#1}
  \IfValueT{#3}{\label{#3}}\relax%
}


\newcommand\set@tmode@newpage{%
\ifvmode
  \@SWtmodefalse
\else
  \unskip\@SWtmodetrue\linebreak
  \vadjust{\vspace{0pt plus1fill}\par}%
\fi}

\newcommand\reset@tmode{\if@SWtmode\expandafter\noindent\ignorespaces\fi}

\newcommand\cleartoeven[1]{%
    \set@tmode@newpage\clearpage
    \ifodd\c@page\afterpage{#1}\else#1\fi%
    }

\newcommand\cleartopage{\set@tmode@newpage\clearpage}

\NewDocumentEnvironment{DFimage}%
  { O{SW} m O{#4} m o D(){0.8} D<>{0} D||{0.25} D!!{} }%
{%
  \SetList{\DisplayModeList}{SW,HS,VS,FS,NF,RF,TH,TW}%
  \TestList{\DisplayModeList}{#1}%
  {\csuse{#1figure}{#2}[#3]{#4}[#5](#6)<#7>|#8|!#9!%
    \fptest{\CompStrings{\@currenv}{DFimage}}{}{\reset@tmode}%
  }%
  {%
    \DFwarning[#1]{#2}[#3]{#4}
  }%
}%
{%
  \aftergroup\reset@tmode
}%

\NewDocumentCommand\DFwarning{ o m o m }{%
\PackageWarning{swfigure}%
    {********************************************\MessageBreak
      Option #1\space is not valid. Nothing done \MessageBreak
                                                 \MessageBreak
      Image #2 was not processed                 \MessageBreak
     ********************************************\MessageBreak
    }%
}

\NewDocumentCommand\SWfigure{m o m o d() d<> d|| d!!}{%
\setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
\DFwidth=\wd\DFtotalimage \DFhalfwidth=0.5\DFwidth
\DFheight=\ht\DFtotalimage
\FigSpace=\dimexpr\textwidth+\internalmargin\relax
\setbox\DFimageI\hbox{\bgroup
\edef\x{\egroup\noexpand\includegraphics*[%
trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
\setbox\DFimageII\hbox{\bgroup
\edef\x{\egroup\noexpand\includegraphics*[%
trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
\fptest{\DFheight/\DFhalfwidth > \textheight/\FigSpace}%
  {\edef\DFscalefactor{\fpeval{\textheight/\DFheight}}}%
  {\edef\DFscalefactor{\fpeval{\FigSpace/\DFhalfwidth}}}%
\setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
\setbox\DFimageII=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
\cleartoeven{%
  \begin{figure}[p]%
  \vbox to\textheight{\vss\hsize=\textwidth%
  \makebox[\hsize][l]{\makebox[\FigSpace][r]{\box\DFimageI}}\vss}%
  \end{figure}\newpage
  \begin{figure}[p]%
  \vbox to\textheight{\vss\hsize=\textwidth
  \makebox[\hsize][r]{\makebox[\FigSpace][l]{\box\DFimageII}%
  \makebox(0,0)[lb]{\hspace*{\SWcaptionShift}\raisebox{0.5\textheight}{%
  \rotatebox[origin=tc]{90}{\DFcaption[#2]{#3}[#4]%
  %
  }}}}\vss}%
  \end{figure}%
}\clearpage\reset@tmode}

\NewDocumentCommand\NFfigure{m o m o d() d<> d|| d!!}{%
  \begin{figure}[p]
    \includegraphics[width=\linewidth, height=#5\textheight,
                     keepaspectratio]{#1}%
    \caption[#2]{#3}%
    \IfValueT{#4}{\label{#4}}\relax
  \end{figure}
}

\NewDocumentCommand\RFfigure{m o m o d() d<> d|| d!!}{%
      \dimen8=\textwidth\dimen10=\textheight
      \figure[p]\setbox\RFbox=\hbox{%
      \rotatebox[origin=cc]{90}{\parbox[b][\dimen8][c]{\dimen10}%
        {\centering\includegraphics[width=\dimen10, height=\dimen8,
                        keepaspectratio]{#1}%
          \caption[#2]{#3}%
          \IfValueT{#4}{\label{#4}}\relax%
        }}}%
        \edef\RFx{\fpeval{\ht\RFbox/\textheight}}%
        \edef\RFy{\fpeval{\wd\RFbox/\textwidth}}%
        \fptest{\RFx > \RFy}%
          {\scalebox{\RFx}{\box\RFbox}}%
          {\scalebox{\RFy}{\box\RFbox}}%
        \endfigure}

\NewDocumentCommand\VSfigure{m o m o d() d<> d|| d!!}{%
   \setbox\DFtotalimage=\hbox{\includegraphics{#1}}
   \DFwidth=\wd\DFtotalimage \DFheight=\ht\DFtotalimage
   \edef\VS@aspectratio{\fpeval{\DFheight/\DFwidth}}
   \fptest{ \VS@aspectratio < 2 }%
     {\begin{center}\ttfamily\relax
      ===========================================\\
      Image #1 is not tall enough.               \\
      Consider using a display mode different    \\
      from VS; may be NF or RF are better suited.\\
      ===========================================\\
      Nothing done!                              \\
      ===========================================
      \end{center}
     }%
     {\edef\VS@factor{\fpeval{#5\textheight/\DFheight}}%
      \fptest{\VS@factor>1 || \VS@factor\DFwidth < #7\textwidth}%
       {%
         \begin{center}\ttfamily\relax
         =================================================\\
         The scaled image #1 is too slim.                 \\
         Maybe directly using the wrapfig package might   \\
         solve this problem.                              \\
         =================================================\\
         Nothing done!\\
         =================================================
         \end{center}
       }%
       {\setbox\DFtotalimage=
        \hbox{\scalebox{\VS@factor}{\box\DFtotalimage}}%
        \edef\VS@width{\fpeval{\VS@factor*\DFwidth}\p@}%
        \setbox\DFtotalimage=\vbox{\hsize=\VS@width%
        \def\@captype{figure}\box\DFtotalimage
        \caption[#2]{#3}%
        \IfValueT{#4}{\label{#4}}\relax}
        \VS@lines=
        \fpeval{round(\ht\DFtotalimage/\baselineskip,0)+#6}%
        \begin{wrapfigure}[\VS@lines]{o}[0pt]{\VS@width}%
        \box\DFtotalimage
        \end{wrapfigure}%
      }%
    }%
  }%

\NewDocumentCommand\HSfigure{m o m o d() d<> d|| d!!}{%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFwidth=\wd\DFtotalimage \DFhalfwidth=0.5\DFwidth
  \FigSpace=0.5\spreadwidth%
  \setbox\DFimageI\hbox{\bgroup
  \edef\x{\egroup\noexpand\includegraphics*[%
          trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
  \setbox\DFimageII\hbox{\bgroup
  \edef\x{\egroup\noexpand\includegraphics*[%
          trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
  \edef\DFscalefactor{\fpeval{\FigSpace/\DFhalfwidth}}%
  \setbox\DFimageI=\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
  \setbox\DFimageII=\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
  \setbox\DFimageII=
    \hbox{\dimen10=\linewidth\dimen8\internalmargin
           \vbox{\hsize\DFhalfwidth\parindent\z@
             \box\DFimageII\par
             \leavevmode\hspace*{\dimen8}%
             \vtop{\hsize\dimen10\parindent\z@
               \textwidth=\hsize
               \DFcaption[#2]{#3}[#4]%
              }\vspace*{2\baselineskip}%
            }%
          }%
  \setbox\DFimageI=\vbox to\ht\DFimageII{\box\DFimageI\vss}%
  \cleartoeven{%
    \hb@xt@\textwidth{%
      \makebox[\DFhalfwidth][l]{\box\DFimageI}\hss}%
     \afterpage{\hb@xt@\textwidth{%
         \hss\makebox[\DFhalfwidth][r]{\box\DFimageII}}%
    }%
  }%
}

\NewDocumentCommand\FSfigure{m o m o  d() d<> d|| d!!}{%
  \fptest{\CompStrings{\@currenv}{DFimage}}{}{\bgroup}%
  \pagestyle{empty}%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFwidth=\wd\DFtotalimage \DFheight=\ht\DFtotalimage
  \ifdim \DFheight < \DFwidth
    \DFhalfwidth=0.5\DFwidth
    \setbox\DFimageI\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim = 0 0 \the\DFhalfwidth\space 0]}\x{#1}}%
    \setbox\DFimageII\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim = \the\DFhalfwidth\space 0 0 0]}\x{#1}}%
    \DFwidth=\DFhalfwidth \DFheight=\ht\DFtotalimage
  \else
    \DFhalfheight=0.5\DFheight
    \setbox\DFimageII\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim = 0 0 0 \the\DFhalfheight\space]}\x{#1}}% bottom half
    \setbox\DFimageI\hbox{\bgroup
    \edef\x{\egroup\noexpand\includegraphics*[%
    trim =  0 \the\DFhalfheight\space 0 0]}\x{#1}}% tophalf
    \setbox\DFimageI=\hbox{\rotatebox[origin=cc]{90}{\box\DFimageI}}
    \setbox\DFimageII=\hbox{\rotatebox[origin=cc]{90}{\box\DFimageII}}
    \DFwidth=\DFhalfheight \DFheight=\wd\DFtotalimage
  \fi
\fptest{\DFheight/\DFwidth > \paperheight/\paperwidth}%
  {\edef\DFscalefactor{\fpeval{\paperheight/\DFheight}}}%
  {\edef\DFscalefactor{\fpeval{\paperwidth/\DFwidth}}}%
  \setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageI}}}%
  \setbox\DFimageII=\hbox{\scalebox{\DFscalefactor}{\usebox{\DFimageII}}}%
\cleartoeven{%
  \begin{figure}[p]%
  \vbox to\textheight{\vss\hsize=\textwidth%      vbox 1
    \hbox to\hsize{\hspace*{-\externalmargin}%    hbox 1
      \vbox to\paperheight{\vss%                  vbox 2
        \hbox to\paperwidth{\hss\box\DFimageI}%   hbox 2 end hbox 2
      \vss}%                                      end vbox 2
    \hss}%                                        end hbox 1
  \vss}%                                          end vbox 1
  \end{figure}\newpage
  \begin{figure}[p]%
    \vbox to\textheight{\vss\hsize=\textwidth%    vbox 1
      \hbox to\hsize{\hspace*{-\internalmargin}%  hbox 1
        \vbox to\paperheight{\vss%                vbox 2
          \hbox to\paperwidth{%                   hbox 2
            \box\DFimageII\makebox(0,0)[lb]{%     mbox 3
              \hspace*{-\FScaptionShift}%
              \raisebox{0.5\textheight}{%         raisebox
                \rotatebox[origin=bc]{90}{%       rotatebox
                  \DFcaptionP[#2]{#3}[#4]!#8!%
                 }%                               end rotatebox
              }%                                  end raisebox
            }%                                    end mbox 3
          }%                                      end hbox 2
        \vss}%                                    end vbox 2
      \hss}%                                      end hbox 1
    \vss}%                                        end vbox 1
  \end{figure}%
}\clearpage
\fptest{\CompStrings{\@currenv}{DFimage}}{}{\egroup}%
\reset@tmode}

\NewDocumentCommand\THfigure{m o m o  d() d<> d|| d!!}{%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFheight=\ht\DFtotalimage \DFwidth=\wd\DFtotalimage
  \edef\DFscalefactor{\fpeval{\paperheight/\DFheight}}
  \setbox\DFimageI\hbox{%
    \scalebox{\DFscalefactor}{\usebox{\DFtotalimage}}}%
  \DFwidth=\wd\DFimageI
  \ifdim\dimexpr\paperwidth-\DFwidth < 2\externalmargin\relax
    \PackageWarning{swfigure}{%
      *******************************************\MessageBreak
      Figure #1 is too wide to be set in a       \MessageBreak
      Total Height display mode.                 \MessageBreak
      There is not enough space for its caption. \MessageBreak
      Expect questionable results.               \MessageBreak
      *******************************************\MessageBreak}%
  \fi
  \FigSpace=\DFwidth
  \dimen10=\dimexpr\topmargin+1in-(\paperheight-\headsep-\headheight
    -\textheight-\footskip)/2\relax
  \TScaptionwidth=\dimexpr\paperwidth-\FigSpace-3\columnsep\relax
  \cleartopage
  \thispagestyle{empty}%
  \begin{figure}
    \ifodd\c@page\relax%                                    odd page
      \begin{minipage}[c][\textheight][t]{\textwidth}%      minibox1
        \vspace*{\dimen10}%
        \makebox[\textwidth][l]{\hspace*{-\internalmargin}%    hbox2
          \vbox to\textheight{\vss%                            vbox3
            \hbox to\paperwidth{%                              hbox4
              \parbox{\FigSpace}{\box\DFimageI}%
              \hspace{\columnsep}%
              \makebox[\TScaptionwidth]{%
                \parbox{#5\TScaptionwidth}{%
                \caption[#2]{#3}\IfValueT{#4}{\label{#4}}\relax}%
              }%
            }%                                             end hbox4
          \vss}%                                           end vbox3
        }%                                                 end hbox2
      \end{minipage}%                                   end minibox1
    \else %                                                even page
      \begin{minipage}[c][\textheight][t]{\textwidth}
        \vspace*{\dimen10}%
        \makebox[\textwidth][l]{\kern-\externalmargin
          \vbox to\textheight{\vss
            \hbox to\paperwidth{\hss
              \hbox to\TScaptionwidth{\hss
                \parbox{#5\TScaptionwidth}{%
                  \caption[#2]{#3}\IfValueT{#4}{\label{#4}}\relax}%
              }\hspace{\columnsep}%
              \parbox{\FigSpace}{\box\DFimageI}%
            }\vss
          }%
        }%
      \end{minipage}%
    \fi
  \end{figure}%
  \clearpage\reset@tmode
}

\NewDocumentCommand\TWfigure{m o m o  d() d<> d|| d!!}{%
  \setbox\DFtotalimage=\hbox{\includegraphics{#1}}%
  \DFheight=\ht\DFtotalimage \DFwidth=\wd\DFtotalimage
  \FigSpace=\dimexpr\internalmargin+0.80\textwidth\relax
  \TScaptionwidth=\dimexpr\paperwidth-\FigSpace-2\columnsep\relax
  \edef\DFscalefactor{\fpeval{\FigSpace/\DFwidth}}
  \setbox\DFimageI=\hbox{\scalebox{\DFscalefactor}{\box\DFtotalimage}}
  \DFwidth=\wd\DFimageI \DFheight\ht\DFimageI
  \cleartopage
  \thispagestyle{empty}%
    \ifodd\value{page}%                         odd numbered page
      \begin{figure}
         \setbox\DFimageII=\hbox{%
           \parbox{\FigSpace}{\box\DFimageI}\hspace{\columnsep}%
           \begin{minipage}{\TScaptionwidth}\centering
             \parbox{#5\hsize}{\caption[#2]{#3}%
               \IfValueT{#4}{\label{#4}}\relax}%
           \end{minipage}
         }%
         \DFheight=\ht\DFimageII \dimen8=\dp\DFimageII
         \advance\DFheight by\dp\DFimageII
         \dp\DFimageII=\z@ \ht\DFimageII=\DFheight
         \dimen10=\fpeval{\topskip+1in+\headheight+\headsep}\p@
         \advance\dimen8 by\baselineskip
         \advance\DFheight by-\dimen10\relax
         \vbox to\DFheight{\vss
         \hbox{\raise \dimen8\hbox to\textwidth{%
            \hspace{-\internalmargin}\box\DFimageII\hss}}%
         }%
      \end{figure}
    \else%                                     even numbered page
      \begin{figure}
         \setbox\DFimageII=\hbox{%
           \begin{minipage}{\TScaptionwidth}\centering
             \parbox{#5\hsize}{\caption[#2]{#3}%
               \IfValueT{#4}{\label{#4}}\relax}%
           \end{minipage}
           \hspace{\columnsep}%
           \parbox{\FigSpace}{\box\DFimageI}
         }%
         \DFheight=\ht\DFimageII \dimen8=\dp\DFimageII
         \advance\DFheight by\dp\DFimageII
         \dp\DFimageII=\z@ \ht\DFimageII=\DFheight
         \dimen10=\fpeval{\topskip+1in+\headheight+\headsep}\p@
         \advance\dimen8 by\baselineskip
         \advance\DFheight by-\dimen10\relax
         \vbox to\DFheight{\vss
         \hbox{\raise \dimen8\hbox to\textwidth{\hss%
            \box\DFimageII\hspace{-\internalmargin}}}%
         }%
       \hbox to\textwidth{\hspace{-\internalmargin}\box\DFimageII\hss}
    \end{figure}%
    \fi
  \reset@tmode
}

%% 
%% 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 files swfigure.dtx and README.txt, and the derived
%% files swfigure.sty and swfigure.pdf
%% 
%%
%% End of file `swfigure.sty'.