summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/phffullpagefigure/phffullpagefigure.dtx
blob: 9a88636dc0f055671ad5e43ce2a2eec40343d859 (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
% \iffalse meta-comment
%
% Copyright (C) 2016 by Philippe Faist <philippe.faist@bluewin.ch>
% -------------------------------------------------------
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.3 or later is part of all distributions of LaTeX 
% version 2005/12/01 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{phffullpagefigure.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01]
%<package>\ProvidesPackage{phffullpagefigure}
%<*package>
    [2016/08/15 v1.0 phffullpagefigure package]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{xcolor}
\usepackage{phffullpagefigure}
\usepackage[preset=xpkgdoc]{phfnote}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{phffullpagefigure.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2016/04/20}{Initial version}
%
% \GetFileInfo{phffullpagefigure.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment,\def,\gdef,\edef,\xdef,\if,\else,\fi,\ifx}
% 
% \title{\phfqitltxPkgTitle{phffullpagefigure}}
% \author{Philippe Faist\quad\email{philippe.faist@bluewin.ch}}
% \date{\pkgfmtdate\filedate}
% \maketitle
%
% \begin{abstract}
%   \pkgname{phffullpagefigure}---Figures which fill up a full page of a document.
% \end{abstract}
%
% \inlinetoc
%
% \section{Introduction}
%
% The package \pkgname{phffullpagefigure} provides an implementation for figures which are
% to be displayed to occupy a full page.
%
% A typical use case: suppose you have a figure in PDF format of the size of the document
% paper, for example, and wants to include it as a figure.
%
% This package takes care to display the caption of the figure on the preceeding page,
% with a caption of the form ``Figure X (on facing page): \meta{caption}.''
%
% For two-sided documents, you may specify on which (odd/even) page side
% you want the figure to appear.  If the document is not two-sided, the
% figure may appear on any page.
%
% A number of options allow you to set the exact figure contents (usually a
% PDF file, but it can be constructed from arbitrary \LaTeX{} commands),
% the figure caption placement (top, own page, bottom), the caption and
% label as usual, and the formatting of the caption if you want to replace
% the default ``(on facing page)'' or ``(on next page).''
%
%
% \section{The Full-Page-Figure Environment}
%
% \DescribeEnv{fullpagefigure} The |\begin{fullpagefigure}| |...|
%   |\end{fullpagefigure}| environment starts a full-page-figure.  Inside
% this environment, only the following commands may be used:
% \begin{itemize}
% \item one of the |\fig***| commands;
% \item the |\caption| command, to provide a figure caption as for regular
%   figures;
% \item the |\label| command to set a label for text references to this
%   figure, also as for regular figures.
% \end{itemize}
%
% A simple example to get started:
%
% \begin{verbatim}
% \begin{fullpagefigure}
%   \figpdf{fig/my-figure} % my PDF file
%   \caption{A colorful figure with letters and words.  The table
%     design may remind you of going to the optician.}
%   \label{fig:test}
% \end{fullpagefigure}
% \end{verbatim}
%
% \begin{fullpagefigure}%
%   \iffalse meta-comment
%     In this documentation code, we cheat and provide LaTeX code for the
%     figure instead of a PDF file.  This is really just so that we don't
%     have to ship an extra PDF file (and for no other reason).
%     
%     See a trick in http://tex.stackexchange.com/a/278101/32188 and
%     http://tex.stackexchange.com/a/40503/32188 .
%   \fi
%   \figcontents{\newpage\thispagestyle{empty}\relax
%     \newgeometry{margin=0.25in}\relax
%     \sffamily\fontsize{36pt}{42pt}\fontseries{bx}\selectfont
%     \hbox to 0.9\textwidth{\fcolorbox{red}{yellow}{\vbox to 0.99\textheight{\vfil\vfil\vfil\centering
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{red!50!black} T H I S }\par\vfil
%         \vfil
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{green!70!red} F I G U R E }\par\vfil
%         \vfil
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{black!40!blue} F I L L S }\par\vfil
%         \vfil
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{green!40!blue} U P }\par\vfil
%         \vfil
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{red!50!gray} T H E }\par\vfil
%         \vfil
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{blue} W H O L E }\par\vfil
%         \vfil
%         \leavevmode\noindent\hbox to 0.8\textwidth{ \color{red!50!yellow} P A G E }\par\vfil
%         \vfil\vfil\vfil}}}%
%       \clearpage\aftergroup\restoregeometry}%
%   \caption{A colorful figure with letters and words.  The table design
%       may remind you of going to the optician.}%
%   \label{fig:test}%
% \end{fullpagefigure}
%
% The |\figpdf| command sets the PDF file to be displayed in full page (see
% more details below).  You may contemplate the result of this code in
% \autoref{fig:test}.
%
% Some wizard once told me that old wise men had determined that the
% |fullpagefigure| environment should be placed at the beginning of a
% paragraph, or on its own paragraph.  (TODO: I'm not too sure why this is
% the case or if this is still relevant.)
%
%
% The contents and appearance of the figure can be adjusted by the
% following commands, which must be issued within the |fullpagefigure|
% environment.
%
% \DescribeMacro{\figcontents} Specify the contents of the figure by calling
% |\figcontents|\marg{\LaTeX{} commands}.  The contents can be any \LaTeX{}
% commands which will generate the figure content.  These commands will be
% called within an |afterpage| block.\footnote{See documentation for the
% \pkgname{afterpage} package}
%
% If you want a figure to occupy several pages, you may use for example
% |\figcontents{\includepdf{pdf-1}\includepdf{pdf-2}}|, and request that
% the figure start on an even-side page with |\figpageside{even}|.
%
% If the \meta{\LaTeX{} commands} are not |\includepdf| instructions
% and want to change the page geometry for the figure and you're using
% the \pkgname{geometry} package, have a look at these \texttt{TeX.SX}
% answers\footnote{\url{http://tex.stackexchange.com/a/278101/32188}
% and \url{http://tex.stackexchange.com/a/40503/32188}} for getting
% the |\newgeometry| and
% |\restoregeometry| commands right.  You might use, for example:\\[1ex]
% \noindent|\figcontents{\newpage\thispagestyle{empty}%|\\
% \hspace*{2em}|\newgeometry{margin=0.25in}% or 0in for no margins|\\
% \hspace*{2em}\meta{\ldots{} \LaTeX{} commands to draw figure contents ...}\\
% \hspace*{2em}|\clearpage\aftergroup\restoregeometry}|.
%
%
% 
% \DescribeMacro{\figpdf} As a shorthand, you may use
% |\figpdf|\oarg{options}\marg{pdf-file} as a shorthand for
% |\figcontents{\includepdf|\oarg{options}\marg{pdf-file}|}|.
% 
% You should explore the options provided by |\includepdf| (from the
% \pkgname{pdfpages} package\footnote{See documentation at
% \url{https://www.ctan.org/pkg/pdfpages}}).  For example, the figure can
% be resized to fill the page, pages may be selected individually from a
% mult-page PDF, the image may be rotated, etc.
%
%
% \DescribeMacro{\caption} \DescribeMacro{\label} The |\caption|\oarg{short
% caption}\marg{caption} and |\label|\marg{identifier} macros may be used
% as for a normal figure.  Be warned, though, that some dark manipulations
% occur here, so it may be for example that the code passed as argument to
% these commands is expanded only later.
%
% \DescribeMacro{\figpageside} Use the commands |\figpageside{odd}|,
% |\figpageside{}|, or |\figpageside{even}| to specify on which side the
% figure should appear on if the document is two-sided.  This command has
% no effect if the document is not two-sided (|twoside| class option for
% the |article| or |book| classes, for example).  Calling |\figpageside{}|,
% i.e.\@ with an empty argument, instructs |fullpagefigure| to use either
% side, whichever is more convenient.
%
% \DescribeMacro{\figplacement} Specify the figure caption placement with
% |\figplacement||{b|$\mid$|t|$\mid$|p}|.  Each of |b| (bottom), |t| (top)
% and |p| (own page) work as for usual LaTeX floats.  If you specify |p|,
% do NOT combine it with any other option.  You may leave the argument
% empty (|\figplacement{}|) to use defaults.
%
% The figure placement can also be specified as an optional argument to the
% environment (e.g., |\begin{fullpagefigure}[p]| \ldots{} |\end{fullpagefigure}|).
%
% \DescribeMacro{\figcapmaxheight} Specify the maximum estimated height of
% the caption with |\figcapmaxheight|\marg{length}.  This is used to see
% whether the figure caption still fits on the current page.
%
% TODO: This is ugly, the height of the caption should be calculated
% automatically\ldots{} for next time.
%
% By default (if no |\figcapmaxheight| is present), the figure will never
% be assumed to fit in the remainder of the page.
%
% \DescribeMacro{\fullpagefigurecaptionfmt}
% The figure label in the caption may be changed (e.g. ``Figure X (on facing page): \ldots'') by
% redefining the command |\fullpagefigurecaptionfmt|.  See the default implementation for
% more info (\autoref{sec:impl}).
%
% \DescribeMacro{\FlushAllFullPageFigures} If you need to make sure that
% all full-page-figures have been placed up to a certain point, you may
% issue the command |\FlushAllFullPageFigures|.  (You may wish to do so
% before starting a new chapter.)  An optional argument
% |\FlushAllFullPageFigures[\clearpage]| or
% |\FlushAllFullPageFigures[\cleardoublepage]| specifies whether to
% continue on any page or on an odd-side page only.
% 
%
%
% \section{Package Options}
%
% Only a single package option is recognized:
%
% \begin{verbatim} \usepackage[nopdfpages]{phffullpagefigure} \end{verbatim}
%
% If this package option is given, then the \pkgname{pdfpages} package
% is not loaded, and the command |\figpdf| is not made available.  You
% may use this package option if the \pkgname{pdfpages} package
% conflicts with your setup.
%
%
%
%
%
%
%
%
% \StopEventually{\PrintChangesAndIndex}
%
% \section{Implementation}
% \label{sec:impl}
%
% Include some general useful packages first.
%    \begin{macrocode}
\RequirePackage{etoolbox}
\RequirePackage{ifoddpage}
\RequirePackage{afterpage}
%    \end{macrocode}
% 
% The \pkgname{placeins} package provies the |\FloatBarrier| command,
% which we use to ensure that no other float gets in the way.
%    \begin{macrocode}
\RequirePackage{placeins}
%    \end{macrocode}
% 
% \begin{macro}{phffpf@internal@pending}
%   Counter which stores how many full-page-figures still haven't been
%   placed.  Used for |\FlushAllFullPageFigures| as well as making sure
%   that the full-page-figures don't interfere with one another.
%    \begin{macrocode}
\newcounter{phffpf@internal@pending}
\setcounter{phffpf@internal@pending}{0}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\phffpfFloatBarrier}
%   Redefine this if you don't want to use a |\FloatBarrier|.  Be warned of
%   the following points:
%
%   \begin{itemize}
%   \item |\FloatBarrier| introduces automatically a new paragraph.
%     Nothing you can do about that a priori.
%
%   \item If you remove |\FloatBarrier|, you need to either be sure that
%     there are no floats which can mess up placement of the
%     fullpagefigure.  Alternatively, you need to provide your own
%     mechanism that ensures that.
%   \end{itemize}
%
%    \begin{macrocode}
\def\phffpfFloatBarrier{\FloatBarrier}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsection{The Main Environment Definition}
%
% \begin{environment}{fullpagefigure}
%   The main |fullpagefigure| environment.
%    \begin{macrocode}
\newenvironment{fullpagefigure}[1][b]{% 
%    \end{macrocode}
% 
% Remember that we have a float pending to be placed:
%    \begin{macrocode}
  \addtocounter{phffpf@internal@pending}{1}%
%    \end{macrocode}
% 
% Don't allow any other floats to meddle with our calculations.
%    \begin{macrocode}
  \phffpfFloatBarrier%
  %[YYY]% -- debugging [where is a space being inserted?]
%    \end{macrocode}
% 
% The following variables will store the relevant values of options
% collected in the definition of the figure with e.g. |\figplacement|,
% |\figcontents|, etc.
% 
% NOTE TO SELF: If you add a |\phffpf@val@...| storage variable, don't
% forget to fix that value in |\phffpf@takecareofplacingfigure|.
% 
%    \begin{macrocode}
  \xdef\phffpf@val@pageside{\phffpf@side@}%
  \gdef\phffpf@val@captionopt{}%
  \gdef\phffpf@val@caption{}%
  \gdef\phffpf@val@label{}%
  \gdef\phffpf@val@placement{#1}%
  \gdef\phffpf@val@capmaxheight{\paperheight}%
  \gdef\phffpf@val@figcontents{}%
%    \end{macrocode}
%
% If this document is two-sided (facing odd/even pages), then by default
% place the float on an odd page.  Otherwise, we don't care.
% 
%    \begin{macrocode}
  \if@twoside%
    \xdef\phffpf@val@pageside{\phffpf@side@odd}%
  \else\fi%
%    \end{macrocode}
%
% Provide a set of commands within this figure block which allow to specify
% the figure contents and appearance:
%    \begin{macrocode}
  \begingroup%
  \let\figcontents\phffpf@impl@figcontents%
  \let\figpageside\phffpf@impl@figpageside%
  \let\caption\phffpf@impl@caption%
  \let\label\phffpf@impl@label%
  \let\figplacement\phffpf@impl@placement%
  \let\figcapmaxheight\phffpf@impl@capmaxheight%
%    \end{macrocode}
% 
% Provide |\figpdf| as a shorthand, but only if applicable (i.e., the
% |nopdfpages| package option was not specified and the
% \pkgname{pdfpages} package was loaded):
%    \begin{macrocode}
  \phffpf@provide@figpdf%
%    \end{macrocode}
%
% Finally, ignore any spaces following this command, as well as after the
% |\endenvironment| command.
%    \begin{macrocode}
  \ignorespacesafterend%
  \ignorespaces%
}
%    \end{macrocode}
% 
% Now, the definitions for the end of the environment:
%    \begin{macrocode}
{%
%    \end{macrocode}
%
% Remove any spaces which might have been inserted.
%    \begin{macrocode}
  \ifhmode\unskip\fi%
%    \end{macrocode}
%
% Restore |\caption|, |\label|, etc.\@ to their original meaning:
%    \begin{macrocode}
  \endgroup%
%    \end{macrocode}
% 
% Finally we should actually take care of placing the figure.
%    \begin{macrocode}
  \phffpf@takecareofplacingfigure%
%    \end{macrocode}
% 
% Finally finally, ignore any spaces following this command.  Note that
% because the expansion of |\endfullpagefigure| is inside the definition of
% \LaTeX' ``|\end|'' and has internal commands after that, we can't just
% simply issue a |\ignorespaces|.
%
%    \begin{macrocode}
  \phfpf@useignorespacesandallpars%
}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\phffpf@useignorespacesandallpars}
%   Utility to ignore spaces and paragraphs after the
%   |\end{fullpagefigure}| command.\footnote{This solution was adapted from
% \url{http://tex.stackexchange.com/a/179034/32188} and
% \url{http://tex.stackexchange.com/a/23101/32188}.}
%    \begin{macrocode}
\def\phfpf@useignorespacesandallpars#1\ignorespaces\fi{%
  #1\fi\phffpf@ignorespacesandallpars}
\def\phffpf@ignorespacesandallpars{%
  \begingroup%
  \catcode`\^^M=10\relax%
  \catcode`\^^J=10\relax%
  \@ifnextchar\par%
    {\endgroup\expandafter\phffpf@ignorespacesandallpars\@gobble}%
    {\endgroup}%
}
%    \end{macrocode}
% \end{macro}
% 
%
%
% \begin{macro}{\fullpagefigurecaptionfmt}
% \begin{macro}{\fullpagefigurecaptionfmt@paren@O}
% \begin{macro}{\fullpagefigurecaptionfmt@paren@E}
% \begin{macro}{\fullpagefigurecaptionfmt@paren@x}
%   The macro |\fullpagefigurecaptionfmt| is called to generate the text
%   which is prepended to the figure caption.  It should essentially say
%   ``Figure X (on next page): ''.
%
%   The argument to |\fullpagefigurecaptionfmt| is |#1| = |O|, |E| or |x|
%   for if the figure is on an odd page, an even page, or an unspecified
%   page.
%    \begin{macrocode}
\def\fullpagefigurecaptionfmt#1{%
  \figurename\nobreakspace\thefigure\nobreakspace%
  (\csname fullpagefigurecaptionfmt@paren@#1\endcsname)%
}
\def\fullpagefigurecaptionfmt@paren@O{on facing page} % for odd page figures
\def\fullpagefigurecaptionfmt@paren@E{on next page}   % for even page figures
\def\fullpagefigurecaptionfmt@paren@x{on next page}   % for next-page figures
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
%
%
% \subsection{Implementation of \phfverb{\fig}\phfverb{****} Commands}
%
% These macros really just store their values for later use.
%
% \begin{macro}{\figcontents}
%   This macro will become |\figcontents| inside the |fullpagefigure|
%   environment.
%    \begin{macrocode}
\newtoks\phffpf@tmp@toks
\long\def\phffpf@impl@figcontents#1{%
  \phffpf@tmp@toks={#1}%
  \xdef\phffpf@val@figcontents{\the\phffpf@tmp@toks}%
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\phffpf@side@odd}
% \begin{macro}{\phffpf@side@even}
% \begin{macro}{\phffpf@side@}
%   These hold one-character codes to signify ``odd side,'' ``even side,''
%   or ``no specification.''
%
%    \begin{macrocode}
\def\phffpf@side@odd{O}
\def\phffpf@side@even{E}
\def\phffpf@side@{x}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% \begin{macro}{\figpageside}
%   This will become |\figpageside| inside the |fullpagefigure|
%   environment.
%    \begin{macrocode}
\def\phffpf@impl@figpageside#1{%
  \ifcsname phffpf@side@#1\endcsname%
    \xdef\phffpf@val@pageside{\csname phffpf@side@#1\endcsname}%
  \else%
    \PacakgeError{phffullpagefigure}{Unknown page side designation:
      '#1'. Please use 'odd', 'even', or '' for no preference.}%
  \fi%
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\caption}
%   This will become |\caption| inside the |fullpagefigure| environment.
%    \begin{macrocode}
\def\phffpf@NOARG{}
\def\phffpf@test@NOARG{\phffpf@NOARG}
\newcommand\phffpf@impl@caption[2][\phffpf@NOARG]{%
  \gdef\phffpf@val@captionopt{#1}%
  \gdef\phffpf@val@caption{#2}%
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\label}
%   This will become |\label| inside the |fullpagefigure| environment.
%    \begin{macrocode}
\def\phffpf@impl@label#1{%
  \gdef\phffpf@val@label{#1}%
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\figplacement}
%   This will become |\figplacement| inside the |fullpagefigure| environment.
%    \begin{macrocode}
\def\phffpf@impl@placement#1{%
  \gdef\phffpf@val@placement{#1}%
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\figcapmaxheight}
%   This will become |\figcapmaxheight| inside the |fullpagefigure|
%   environment.
%    \begin{macrocode}
\def\phffpf@impl@capmaxheight#1{%
  \gdef\phffpf@val@capmaxheight{#1}%
  \ignorespaces%
}
%    \end{macrocode}
% \end{macro}
% 
%
%
% \subsection{Placing the figures}
%
% Here's the gory details of how the figures are placed.
%
%
% \begin{macro}{\phffpf@place@pending@figs@code}
%   This macro will store code to be executed after the next figure has
%   been placed.  This can be used to queue other figures to be placed
%   later.
%    \begin{macrocode}
\def\phffpf@place@pending@figs@code{\phffpf@place@pending@figs@code@start}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\phffpf@place@pending@figs@code@start}
%   When another figure is placed, and the
%   |\phffpf@place@pending@figs@code| is updated, then the macro
%   |\phffpf@place@pending@figs@code@start| contains the code which
%   reinitializes |\phffpf@place@pending@figs@code|.
%
%   This reinitialization code consists in precisely making sure that a
%   future execution of |\phffpf@place@pending@igs@code@start| will start
%   by reinitializing that macro.
%    \begin{macrocode}
\def\phffpf@place@pending@figs@code@start{%
  \gdef\phffpf@place@pending@figs@code{\phffpf@place@pending@figs@code@start}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\phffpf@impl@figcode}
%   The code to be inserted to generate the figure.
%
%   The argument |#1| is the prefix for macro names where to look up the
%   contents of the figure and values of the figure settings.  The macro
%   names are determined as |\csname #1@|\meta{field-name}|\endcsname|.
%    \begin{macrocode}
\gdef\phffpf@impl@figcode#1{%
%    \end{macrocode}
% 
% Do we have a figure placement position request (|p|, |t|, |b|)?  If yes,
% then define a macro which we will expand in front of the |\begin{figure}|
%   command for the caption.  If no, then that macro should be left blank
%   (first case below):
%    \begin{macrocode}
  \expandafter\ifblank\expandafter{\csname #1@placement\endcsname}{%
    \edef\phffpf@tmp@figplacementarg{}%
  }{%
    \edef\phffpf@tmp@figplacementarg{[\csname #1@placement\endcsname]}%
  }
%    \end{macrocode}
% 
% Invoke the |figure| environment, which we use to typeset the caption.
% Use specified placement if applicable.  Set up some basic stuff in the
% figure: the contents, caption and label.
%    \begin{macrocode}
  \expandafter\figure\phffpf@tmp@figplacementarg%
  \centering%
  \begingroup%
  \def\fnum@figure{\fullpagefigurecaptionfmt{\csname #1@pageside\endcsname}}%
  \expandafter\afterpage\expandafter{\csname #1@figcontents\endcsname}%
  \expandafter\ifx\csname #1@captionopt\endcsname\phffpf@test@NOARG%
    \expandafter\caption\expandafter{\csname #1@caption\endcsname}%
  \else%
    \def\phffpf@tmp@captioncmdopt{%
      \expandafter\caption\expandafter[\csname #1@captionopt\endcsname]}%
    \expandafter\phffpf@tmp@captioncmdopt\expandafter{\csname #1@caption\endcsname}%
  \fi%
  \expandafter\notblank\expandafter{\csname #1@label\endcsname}{%
    \expandafter\label\expandafter{\csname #1@label\endcsname}%
  }{%
  }
  \endgroup%
  \endfigure%
%    \end{macrocode}
%
% Now we have placed the |figure|, so decrease our ``pending-to-be-placed''
% counter.
%    \begin{macrocode}
  \addtocounter{phffpf@internal@pending}{-1}%
%    \end{macrocode}
%
% \ldots{} and execute the code to place any other pending figures.  (We
% set |\ifphffpf@flag@forcenextmaybequeuetoplacefigure| to TRUE to force
% the next figure in queue to be placed now.)
%    \begin{macrocode}
  \afterpage{%
    \phffpf@flag@forcenextmaybequeuetoplacefiguretrue%
    \phffpf@place@pending@figs@code%
  }%
}
%    \end{macrocode}
% \end{macro}
%
%
% Now, all options have been set etc., the fullpagefigure environment has finished, so
% calculate the commands to place the figure appropriately.
%
%
% \begin{macro}{\phffpf@takecareofplacingfigure}
%   First, fix the values of the contents and settings (in case another
%   full-page-figure comes along and messes up the |\phffpf@val@...|
%   commands).
%
%   After the values have been fixed (in fact they are stored in the form
%   of ``restore code''), then we delegate to
%   |\phffpf@maybequeuefigurecode|, which checks whether we can place a
%   figure or if we should queue.
%    \begin{macrocode}
\def\phffpf@takecareofplacingfigure{%
%    \end{macrocode}
%
% A tricky part: make sure we save the values of |phffpf@val@|\meta{field}
% in a fixed way so that several figures won't overwrite each other's
% values.
%
% We build a bunch of tokens which are in fact restore code for the given
% variables, i.e., which is a list of commands of the form
% |\gdef\phffpf@val@|\meta{field}|{|\meta{first-level-expanded-value-of-this-field}|}|.
% This set of tokens have the values of these variables expanded to the
% first level, so that it is OK if the variables |\phffpf@val@|\meta{field}
% are overwritten.
%    \begin{macrocode}
  \edef\phffpf@tmp@fixallfieldvalues{%
    \noexpand\gdef\noexpand\phffpf@val@pageside{\expandonce\phffpf@val@pageside}%
    \noexpand\gdef\noexpand\phffpf@val@captionopt{\expandonce\phffpf@val@captionopt}%
    \noexpand\gdef\noexpand\phffpf@val@caption{\expandonce\phffpf@val@caption}%
    \noexpand\gdef\noexpand\phffpf@val@label{\expandonce\phffpf@val@label}%
    \noexpand\gdef\noexpand\phffpf@val@placement{\expandonce\phffpf@val@placement}%
    \noexpand\gdef\noexpand\phffpf@val@capmaxheight{\expandonce\phffpf@val@capmaxheight}%
    \noexpand\gdef\noexpand\phffpf@val@figcontents{\expandonce\phffpf@val@figcontents}%
  }%
%    \end{macrocode}
% 
% Finally, relay the call to
% |\phffpf@maybequeuefigurecode|\marg{restore-code-for-figure-settings}\hspace{0pt}\marg{full-figure-code}.
%    \begin{macrocode}
  \edef\phffpf@tmp@figcodetwoargs{%
    {\expandonce\phffpf@tmp@fixallfieldvalues}%
    {\noexpand\phffpf@impl@figcode{phffpf@val}}%
  }%
  \expandafter\phffpf@maybequeuefigurecode\phffpf@tmp@figcodetwoargs%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\phffpf@maybequeuefigurecode}
%   USAGE: |\phffpf@maybequeuefigurecode|\marg{restore-code-for-figure-settings}\hspace{0pt}\marg{full-figure-code}.
%
%   Checks if we can place the figure; if yes then place it on the right
%   page, if no, then add it to the queue.
%
%   The arguments are: |#1| = code to restore correct |\phffpf@val@XYZ|
%   values; |#2| = figure code.  Make sure it's expanded.
%    \begin{macrocode}
\long\def\phffpf@maybequeuefigurecode#1#2{%
%    \end{macrocode}
% 
% Possibly we have been told to place the next figure now via the flag
% |\ifphffpf@flag@forcenextmaybequeuetoplacefigure|.  In this case, reset
% the flag and place the figure now (relay to |\phffpf@doplacefigure|).
%    \begin{macrocode}
  \ifphffpf@flag@forcenextmaybequeuetoplacefigure%
    \phffpf@flag@forcenextmaybequeuetoplacefigurefalse
    \phffpf@doplacefigure{#1}{#2}%
    %
  \else
%    \end{macrocode}
%
% See if there are other figures waiting to be placed first.  If so, add
% ours to the queue.
%    \begin{macrocode}
    \ifnum\value{phffpf@internal@pending}>1\relax%
      \xdef\phffpf@place@pending@figs@code{%
          \expandonce\phffpf@place@pending@figs@code%
          \unexpanded{\phffpf@maybequeuefigurecode{#1}{#2}}%
        }%
        %\show\phffpf@place@pending@figs@code
        %[figure queued:  \texttt{\detokenize{#1}}]%  -- DEBUGGING
    \else%
%    \end{macrocode}
%
% If not, deal with placing the figure now:
%    \begin{macrocode}
      \phffpf@doplacefigure{#1}{#2}%
      %[figure placed: \texttt{\detokenize{#1}}]  -- DEBUGGING
    \fi%
  \fi%
}
%    \end{macrocode}
% 
% Define also the flag which will force a next call to
% |\phffpf@maybequeuefigurecode| to place the next figure in the queue.
%    \begin{macrocode}
\newif\ifphffpf@flag@forcenextmaybequeuetoplacefigure
\phffpf@flag@forcenextmaybequeuetoplacefigurefalse
%    \end{macrocode}
% \end{macro}
% 
%
% \begin{macro}{\phffpf@doplacefigure}
%   Place the figure now.  Determine the correct number of |\afterpage|'s
%   to use so that the figure caption ends up on the correct page side.
%
%   The arguments to this macro are: |#1| = code to restore correct
%   |phffpf@val@XYZ| values, |#2| = the figure code. Make sure it's
%   expanded.
%    \begin{macrocode}
\long\def\phffpf@doplacefigure#1#2{%
%    \end{macrocode}
%
% Make sure the correct values of |phffpf@val@XYZ| are restored, because we
% need e.g. |\phffpf@val@pageside|.  They may be wrong because this might
% be called after a figure has been queued.
%    \begin{macrocode}
  #1%
%    \end{macrocode}
%
% Now, determine where exactly to place the figure code.  There are no other pending
% figures.  If there is no side preference, just place the figure pretty much now.
%    \begin{macrocode}
  \ifx\phffpf@val@pageside\phffpf@side@%
    \let\phffpf@tmp@doplace\@firstofone%
  \else%
%    \end{macrocode}
%
% If, however, we have a side preference, then check everything more
% carefully.  Use the helper macros
% |\phffpf@placecode@on|\meta{\phfverb{same}$\mid$\phfverb{other}}|parity|.
% (The latter essentially expand to the correct number of |\afterpage|'s.)
%    \begin{macrocode}
    \ifx\phffpf@val@pageside\phffpf@side@odd%
      %[CHECK DONE HERE/WANT ODD] % -- for debugging
      \checkoddpage\ifoddpage%
      %[IS ODD] % -- for debugging
        \let\phffpf@tmp@doplace\phffpf@placecode@onotherparity%
      \else%
        %[IS NOT ODD] % -- for debugging
        \let\phffpf@tmp@doplace\phffpf@placecode@onsameparity%
      \fi%
    \else%
      %[CHECK DONE HERE/WANT EVEN] % -- for debugging
      \checkoddpage\ifoddpage%
        %[IS ODD] % -- for debugging
        \let\phffpf@tmp@doplace\phffpf@placecode@onsameparity%
      \else%
        %[IS NOT ODD] % -- for debugging
        \let\phffpf@tmp@doplace\phffpf@placecode@onotherparity%
      \fi%
    \fi%
  \fi%
%    \end{macrocode}
% 
% I think an |\hbox{}| might help to place the anchor which determines
% which page side we are currently on.  Note that this starts a new
% paragraph and enters horizontal mode.
%    \begin{macrocode}
  \leavevmode\hbox{}%
%    \end{macrocode}
%
% Now, do place the figure somewhere.
%    \begin{macrocode}
  \phffpf@tmp@doplace{#1#2}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\phffpf@placecode@onsameparity}
%   Place the figure code on the same parity (page side) as we are
%   currently.  If enough space remains on the current page, place the
%   figure immediately.  Otherwise, use two |\afterpage|'s so as the figure
%   caption to appear in two pages.
%
%    \begin{macrocode}
\newdimen\phffpf@tmp@spaceleft
\newdimen\phffpf@tmp@compareto
\long\def\phffpf@placecode@onsameparity#1{%
%    \end{macrocode}
%
% First, see if the caption itself requires to be on its own page (and thus
% no height calculations are necessary, and an additional |\clearpage| is
% required)
% 
%    \begin{macrocode}
  \def\@tmpa{p}%
  \ifx\phffpf@val@placement\@tmpa%
    \afterpage{\vspace*{0pt}\afterpage{#1\clearpage}}%
  \else%
%    \end{macrocode}
%
% Otherwise, the figure caption is there along with some text on the page.
% 
% See if there is enough place left on this page to place the figure
% caption; otherwise use two |\afterpage|'s.
%    \begin{macrocode}
    %[PLACING FIG CODE ON SAME PARITY]% -- debugging
    \phffpf@tmp@spaceleft=\textheight\relax%
    \phffpf@tmp@compareto=\phffpf@val@capmaxheight\relax%
    \advance\phffpf@tmp@spaceleft by -\pagetotal%
    %[DIM LEFT: \the\phffpf@tmp@spaceleft]%
    \ifdim\phffpf@tmp@spaceleft>\phffpf@tmp@compareto%
      %[ENOUGH DIM LEFT.] % -- debugging
      #1%\phffpf@tmp@figcode%
    \else%
      %[*NOT ENOUGH* DIM LEFT.] % -- debugging
      \afterpage{\vspace*{0pt}\afterpage{#1}}%
    \fi%
  \fi%
}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\phffpf@placecode@onotherparity}
%   Place the figure caption on the opposite parity as the current page.
%   This just requires one |\afterpage| so as the figure code to appear on
%   the following page.
%
%    \begin{macrocode}
\def\phffpf@placecode@onotherparity#1{%
  %[PLACING FIG CODE ON OTHER PARITY]% -- debugging
%    \end{macrocode}
% 
% First, see if the caption requires to be on its own page (and thus no
% height calculations are necessary, and an additional |\clearpage| is
% required).
%    \begin{macrocode}
  \def\@tmpa{p}%
  \ifx\phffpf@val@placement\@tmpa%
    \afterpage{#1\clearpage}%
  \else%
%    \end{macrocode}
%
% The figure caption is not on its own page.  Just use a simple |\afterpage|.
%    \begin{macrocode}
    \afterpage{#1}%
  \fi%
}
%    \end{macrocode}
% \end{macro}
% 
%
% \subsection{Commands to Flush All Full-Page-Figures}
%
%
% Here are a set of commands which can be used to ensure that all full-page
% figure floats have been placed.
%
% \begin{macro}{\FlushAllFullPageFigures}
%   The name is pretty self-explanatory.  The command is documented in the user doc above.
%    \begin{macrocode}
\newcommand\FlushAllFullPageFigures[1][\phffpf@clearpage]{%
%    \end{macrocode}
% 
% As long as there are full-page-figures pending, clear pages until those
% figures have been placed.
%    \begin{macrocode}
  \ifnumcomp{\value{phffpf@internal@pending}}{>}{0}{%
    \clearpage%
    %[page cleared.]% DEBUG
    \FlushAllFullPageFigures[#1]% recurse again.
  }{%
%    \end{macrocode}
%
% All figures placed, all fine.  We still need to flush one last time
% because at this point the figure code (ie. caption) has been placed only,
% and we want the text that follows to come after the figure itself.  Here
% finally we use the clear command in |#1| to continue on any page or on an
% odd-side page only.
%    \begin{macrocode}
    #1%
  }%
}
\def\phffpf@clearpage{\if@twoside\cleardoublepage\else\clearpage\fi}
%    \end{macrocode}
% \end{macro}
% 
%
%
% \subsection{Package Option Parsing}
%
% Note the singular form of the word ``option.''
% 
%    \begin{macrocode}
\def\phffpf@provide@figpdf{}
\newcommand\phffpf@impl@figpdf[2][]{%
  \figcontents{\includepdf[#1]{#2}}%
}
\def\phffpf@do@pdfpages{%
  \RequirePackage{pdfpages}%
  \def\phffpf@provide@figpdf{\let\figpdf\phffpf@impl@figpdf}%
}
%
\DeclareOption{nopdfpages}{\def\phffpf@do@pdfpages{}}
\DeclareOption*{%
  \@unknownoptionerror%
}
\ProcessOptions\relax
%
\phffpf@do@pdfpages
%    \end{macrocode}
%
%\Finale
\endinput