summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/draftwatermark/draftwatermark.dtx
blob: ab5cb3b948eeb104f2970e294a5656fe20794dfc (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
% \iffalse meta-comment
%
% Copyright 2006-2020
% Sergio Callegari <sergio.callegari@gmail.com>
%
% ---------------------------------------------
% This file is part of the draftwatermark package,
% a contribution to the LaTeX2e system.
% ---------------------------------------------
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License, version 1.3. This license is at
% https://www.latex-project.org/lppl/ and is part
% of all distributions of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "author-maintained".
%
% This program consists of the files listed in the README file
% included in the package.
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{newtxtext}
\usepackage{hologo}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{url}
\EnableCrossrefs

\setcounter{IndexColumns}{2}
\setlength{\IndexMin}{3cm}

\DoNotIndex{\begin, \end, \begingroup, \endgroup}
\DoNotIndex{\csname, \endcsname}
\DoNotIndex{\if, \else, \fi}
\DoNotIndex{\ifx, \fi}
\DoNotIndex{\ifodd, \fi}
\DoNotIndex{\@car, \@empty, \@ifnextchar, \@nil, \@onelevel@sanitize,
  \@tempboxa, \@tempdima, \@tempdimb, \@tfor}
\DoNotIndex{\AddEverypageHook, \AddThispageHook, \addtolength, \author}
\DoNotIndex{\bfseries, \box}
\DoNotIndex{\color}
\DoNotIndex{\DeclareBoolOption, \DeclareComplementaryOption,
  \DeclareStringOption, \def, \define@key, \dimexpr, \DisableKeyvalOption,
  \do, \documentclass, \dp}
\DoNotIndex{\edef, \expandafter}
\DoNotIndex{\fbox, \fontsize}
\DoNotIndex{\hbox, \hspace, \hss, \ht}
\DoNotIndex{\let, \lipsum}
\DoNotIndex{\maketitle, \MyWM}
\DoNotIndex{\newcommand, \newsavebox, \NeedsTeXFormat}
\DoNotIndex{\PackageError, \paperheight, \paperwidth,
  \ProcessKeyvalOptions, \ProvidesPackage}
\DoNotIndex{\relax, \RequirePackage, \rotatebox}
\DoNotIndex{\savebox, \sbox, \scalebox, \section, \selectfont, \setkeys,
  \setlength, \sffamily}
\DoNotIndex{\thepage, \title}
\DoNotIndex{\usebox, \usepackage}
\DoNotIndex{\vbox, \vspace, \vss}
\DoNotIndex{\wd, \wmbox}
  
\CodelineIndex
%\RecordChanges
\begin{document}
  \DocInput{draftwatermark.dtx}
\end{document}
%</driver>
%
% \fi
%
% \CheckSum{288}
%
% \def\filename{draftwatermark.dtx}
% \def\fileversion{2.1}
% \def\filedate{2020/05/11}
% \def\docdate{2020/05/11}
%
% \newcommand*{\Lpack}[1]{\textsf {#1}}           ^^A typeset a package
% \newcommand*{\Lopt}[1]{\texttt {#1}}            ^^A typeset an option
% \newcommand*{\file}[1]{\texttt {#1}}            ^^A typeset a file
% \newcommand*{\Lcount}[1]{\textsl {\small#1}}    ^^A typeset a counter
% \newcommand*{\pstyle}[1]{\textsl {#1}}          ^^A typeset a pagestyle
% \newcommand*{\Lenv}[1]{\texttt {#1}}            ^^A typeset an environment
%
% \title{The \Lpack{draftwatermark} package\thanks{This file
%     (\texttt{\filename}) has version number \fileversion, last
%     revised \filedate.}}
%
% \author{%
%   Sergio Callegari\thanks{Sergio Callegari can be reached at
%       \texttt{sergio.callegari at gmail dot com}}} 
%
% \date{\docdate}
%
% \maketitle
%
% \begin{abstract}
%   The \Lpack{draftwatermark} package extends \LaTeX\ providing a means
%   to add a watermark (typically textual and light gray, but possibly
%   more sophisticated) on the pages of a document (either on every page,
%   on the first page, or on selected pages). Typical usage may consist in
%   writing words such as ``DRAFT'' or ``CONFIDENTIAL'' across the
%   pages. The package may remind in some sense \Lpack{draftcopy} by
%   Dr.~Juergen Vollmer, but its implementation is lighter (as the reduced
%   code footprint shows) and does not rely on postscript specials, making
%   the package fully compatible with \hologo{pdfLaTeX}, \hologo{XeLaTeX}
%   and \hologo{LuaLaTeX}. The \Lpack{draftwatermark} package depends on
%   \Lpack{everypage} by the same author. Its feature set is somehow
%   restricted because the emphasis is on the simplicity of the
%   interface. For complex layouts, \Lpack{scrlayer.sty} from the
%   \emph{KOMA script} bundle may represent a valuable alternative.
% \end{abstract}
% 
% \section{Introduction}
% 
% This package extends \LaTeX\ providing a means to add a textual, usually
% light gray (but possibly colored) watermark on every page, the first
% page, or selected pages of a document. Typical usage may consist in
% writing words such as ``DRAFT'' or ``CONFIDENTIAL'' across document
% pages.
%
% The package may remind in some sense \Lpack{draftcopy} by Dr.~Juergen
% Vollmer, but its implementation differs in a few points:
% \begin{enumerate}
% \item Code footprint is smaller, although this does not mean much on
%   modern computers.
% \item There is no use of Postscript specials or other driver-dependent
%   tricks, so the package should be compatible with any output driver and
%   with \hologo{pdfLaTeX}, \hologo{XeLaTeX}, and \hologo{LuaLaTeX}\ in
%   particular. Recently, compatibility with PDF generating engines is
%   quite important and its lack in \Lpack{draftcopy} and was the first
%   motivation for writing \Lpack{draftwatermark}.
% \item Reduced code footprint comes at some price. Many features of
%   \Lpack{draftcopy} are not supported here, most notably
%   \Lpack{draftwatermark} has:
%   \begin{itemize}
%   \item No automatic selection of the watermark text to use, based on
%     the language, relying on pre-stored translations of the word
%     ``DRAFT''. This may be no big loss, since one is free to choose
%     whatever text he/she happens to prefer.
%   \item No automatic possibility to watermark only the first \emph{two}
%     pages of a document. However, it is possible to automatically
%     watermark only the first or to \emph{manually} stop the watermarking
%     after the second page.
%   \end{itemize}
%   Note that some features available as options in \Lpack{draftcopy}
%   (e.g., the possibility of time-stamping the watermark text) can be
%   obtained working on the watermark text itself.
% \end{enumerate}
%
% The emphasis of \Lpack{draftwatermark} is on the simplicity of its
% interface. In many cases the package will do the right thing by just
% loading it, without any further configuration. Such simplicity pairs
% with some limitations. As an example, \Lpack{draftwatermark} is capable
% of putting some text or an image on the background of the page, below
% the document text, but not of \emph{stamping}, that is putting it in the
% foreground, above the document text. Similarly, it is not well suited
% for complex watermark layouts. As a further note, there may be
% unexpected behaviors when \Lpack{draftwatermark} is used together with
% other packages affecting the page building, e.g., with respect to the
% stacking order of the objects on the page. For any complex layout, or
% when the Z-ordering is important, the recommendation is to look at the
% \Lpack{scrlayer} package in the \emph{KOMA script} bundle.
%
% For the actual page building \Lpack{draftwatermark} depends on the
% \Lpack{everypage} package by the same author.
% 
% \section{User interface}
% When loaded, \Lpack{draftwatermark} sets up a number of defaults
% (detailed later and modifiable by specific commands) and immediately
% becomes active.
%
% \subsection{Package options and configuration}
% If loaded as
% \begin{quote}
%   |\usepackage{draftwatermark}|
% \end{quote}
% the package sets up itself to put the a large, grayed background text
% saying ``DRAFT'' at the center every page.
%
% This behavior can be modified by passing options to the package as in
% \begin{quote}
%   |\usepackage[<options>]{draftwatermark}|
% \end{quote}
%
% \DescribeMacro{\DraftwatermarkOptions} 
% The package behavior can also be dynamically changed \emph{after the
% package is loaded} by the command |\DraftwatermarkOptions{<options>}|
% that can be used anywhere in the document source. In either case,
% |<options>| is a comma separated list of options, each given as a
% |<key>| or as a |<key>=<value>| pair. Unless otherwise indicated, all
% options that can be used at the loading of the package can also be given
% via the |\DraftwatermarkOptions{<options>}| mechanism. In fact, the two
% things are almost equivalent, with some cautionary
% details. Specifically, the parsing of options by the |\usepackage|
% command is a bit quirky, hence when specifying options with the
% |<key>=<value>| syntax, some values that are accepted by
% |\DraftwatermarkOptions| may cause issues when specifying options with
% |\usepackage|.
% \medskip
%
% The available options are:
% \begin{itemize}
% \item \verb!firstpageonly=true|false!\\
%   If no value is provided, |true| is assumed. Default is |false|.\\
%   Instructs \Lpack{draftwatermark} to only apply the watermark on the
%   first page.\\
%   This option cannot be used with |\DraftwatermarkOptions{<options>}|.
% \item |firstpage| legacy alias of |firstpageonly|.
% \item \verb!stamp=true|false!\\
%   If no value is provided, |true| is assumed. Default is |true|.\\
%   Instructs \Lpack{draftwatermark} to print the watermark.
% \item |nostamp| legacy alias of |stamp=false|.
% \item |final| legacy alias of |stamp=false|.
% \item |angle=<real>|\\
%   Default is |45|.\\
%   Defines the angle, in degrees, at which the watermark text should be
%   printed.
% \item |scale=<real>|\\
%   Default is |1|.\\
%   Defines the scale at which the watermark text should be printed.
% \item |fontsize=<length>|\\
%   Default is |0.25\paperwidth|.\\
%   Defines the font size for the watermark text. See
%   Section~\ref{ssec:fontsize}.
% \item |text=<token list>|\\
%   Default is |DRAFT|.
%   Defines the watermark text. See notes in Section~\ref{ssec:text}.
% \item |hpos=<length>|\\
%   Default is |0.5\paperwidth|.\\
%   Defines the horizontal position of the watermark, based on its anchor
%   point. See also the |pos| option.
% \item |vpos=<length>|\\
%   Default is |0.5\paperheight|.\\
%   Defines the horizontal position of the watermark, based on its anchor
%   point. See also the |pos| option.
% \item |pos={<length>, <length>}|\\
%   Defines at once the horizontal (first length) and vertical (second
%   length) position of the watermark, based on its anchor point. See also
%   the options |hpos| and |vpos|.
% \item \verb!hanchor=l|c|r!\\
%   Default is |c|.\\
%   Defines the horizontal position of the watermark anchor
%   point. Specifically, |l| stands for \emph{left}, |c| for \emph{center}
%   and |r| for \emph{right}. See also the |anchor| option.
% \item \verb!vanchor=t|m|b!\\
%   Default is |m|.\\
%   Defines the vertical position of the watermark anchor
%   point. Specifically, |t| stands for \emph{top}, |m| for \emph{middle}
%   and |b| for \emph{bottom}. See also the |anchor| option.
% \item |anchor=<string>|\\
%   Defines the anchor point for the watermark. The anchor point is placed
%   at the page coordinates specified by the |hpos| and |vpos|
%   options. The string used for the specification is made of the |l|,
%   |c|, |r|, |t|, |m|, and |b| characters, whose meaning is as described
%   for the |hanchor| and |vanchor| options. For instance, if the anchor
%   specification is |lt|, then the top left corner of the watermark is
%   placed at the position specified by the |hpos| and |vpos| options.
% \item |colormodel=<color model>|\\
%   Defaults to |gray|.\\
%   Defines the color model for the specification of the color of the
%   watermark text. Can be any color model supported by the \Lpack{color}
%   package. Leaving it empty allows named colors to be specified. See
%   also the |color| option.
% \item |colorspec=<color specification>|\\
%   Defaults to |0.8|.\\
%   Defines a color specification for the watermark text. The
%   specification is interpreted according to the color model. For
%   instance if the model is |rgb|, then the specification can be a color
%   triple. See also the |color| option.
% \item |color=<full color specification>|\\
%   Defines the color of the watermark text. The color specification may
%   include an optional color model and must include a color
%   specification, following the standard set by the \Lpack{color}
%   package. For instance, some examples of valid specifications are
%   |red|, or |{[rgb]{1, 0, 1}}|, or |{[gray]{0.5}}|.
% \item |markcmd=<macro>|\\
%   Default is |\DraftwatermarkStdMark|.\\
%   Defines the command used to draw the actual watermark. See also
%   Sect.~\ref{sec:markcmd}.
% \end{itemize}
%
% \section{Some notes on the configuration options}
%
% \subsection{The \Lopt{stamp} option and its companion configuration
% directives}
%
% The |stamp|, |nostamp| and |final| options let one control whether the
% watermark should actually be printed or not. They can be handy for
% quickly removing the ``draft'' status from a document, without having to
% alter the actual watermark setup. Option \Lopt{final} is a synonym for
% \Lopt{nostamp}. Note that \Lopt{final} is a commonly used global
% (class-level) option that can be simultaneously obeyed by multiple
% packages. The option \Lopt{stamp} can be used to override a global
% \Lopt{final} option and to recover the watermarking.
%
% Via the |\DraftwatermarkOptions| command, the |stamp| option can be
% given at arbitrary points in the document to suspend or resume
% watermarking. In this way one can \emph{manually} apply the watermark
% only on selected pages of a document.
%
% \subsection{The watermark text size}
% \label{ssec:fontsize}
%
% The package lets one control the watermark size in two different
% ways. One is by picking a font size via the |fontsize| option; the other
% one is by scaling the watermark with the |scale| option.
%
% In principle, using a large font to produce a large textual watermark is
% the preferred choice. In fact, a font with a large point size is not
% just a scaled up version of the same font with a small point
% size. Conversely, it is a font where all the traits are adjusted to
% actually look well at a large size. This is why \Lpack{draftwatermark}
% defaults to setting |scale=1| and to a large point size for the font (in
% fact, sufficiently large to get the word ``DRAFT'', rotated at
% 45\textdegree, take about half of the page width.
%
% Nonetheless asking for a large font may not work, if \LaTeX\ has not
% been set up to use \emph{scalable} fonts. For this reason, the
% recommendation is to always use \Lpack{draftwatermark} on \LaTeX\ or
% \hologo{pdfLaTeX} together with other packages that trigger the usage of
% fully scalable fonts (practical examples may be \Lpack{newtx} or
% \Lpack{mathptmx}, etc.). An even better alternative is to use modern
% \TeX\ engines, as in \hologo{XeLaTeX} or \hologo{LuaLaTeX} with
% \Lpack{fontspec}. In any case, recall that even with scalable fonts,
% \LaTeX\ may be unable to deal with extremely large font sizes (e.g.,
% $\gg 5\,\mathrm{cm}$). If really large letters are required for the
% watermark, a suitable combination of |fontsize| and |scale| may be
% needed.
%
% One can immediately realize when \LaTeX\ is unable to deal with the
% package default or selected |fontsize| from two things: (i) a font
% warning from \LaTeX; and (ii) a tiny watermark text whose size seems not
% to react to the |fontsize| option. In this case, either switch to
% scalable fonts or select a small |fontsize| (e.g., |12pt|) together with
% a |scale| significantly larger than |1|.
%
% \subsection{The watermark text}
% \label{ssec:text}
%
% As previously noticed, the watermark text can be specified with the
% |text| option. The value passed to the |text| option does not need to be
% a plain string. Conversely, it may include formatting marks, such as
% line break commands (e.g., |\\|) or commands to select font variants
% (e.g., |\bfseries|). In fact, it is also possible to use a command to
% include an image as the text. A word of caution is necessary, though. To
% use \LaTeX\ commands in the watermark text, please configure the latter
% via the |\DraftwatermarkOptions| command. Trying to do so by passing
% options to the \Lpack{draftwatermark} package via the |\usepackage|
% mechanism may fail due to how \LaTeX\ parses option strings. Also note
% that the text may need to be surrounded by braces.
%
% If the watermark text includes line breaks, \Lpack{draftwatermark} will
% use a default interline space set to 1.2 times the font size. Consider
% that multiline texts with alignment, taking up their natural width can
% easily be obtained with the |\shortstack| command.
%
% Also, note that the watermark typesetting happens inside a \TeX\
% group, to make sure that any parameter setting you do in the |text|
% token list can stay local.
%
% As a further remark, observe that in order to only put a watermark on
% selected pages, an alternative to changing the |stamp| configuration
% option at arbitrary points in the document can be to use conditional
% expressions in the watermark text itself.
%
% \subsection{The watermark positioning}
%
% The watermark position is controlled via the |hpos| and |vpos|
% configuration options (or alternatively, by the |pos| configuration
% option). It is worth underlining that these configuration parameters
% require \emph{lengths} not plain numbers.
%
% The coordinates used by the positioning parameters are measured from the
% top left corner of the page. They work in conjunction with the anchoring
% parameters (|hanchor|, |vanchor| and |anchor|). Specifically, the anchor
% point of the watermark is placed at the specified position. The default
% is to center the watermark on the page, but it is possible to choose
% other positions by a smart choice of the positioning and anchoring
% parameters. For instance, one may want to put the watermark on the top
% right corner of each page, which can be easily done with an |rt|
% anchoring.
%
% As a final remark, observe that the specification of the positioning
% coordinates can be based on \LaTeX\ macros. For instance, one can set
% the horizontal position |hpos| at |0.5\paperwidth| (for centering) or at
% |\dimexpr\paperwidth-5mm| (for placement at the right hand side of the
% page). Interestingly, \Lpack{draftwatermark} re-evaluates the
% positioning coordinates on every page. This means that positions based
% on |\paperwidth| or |\paperheight| should dynamically adjust if the page
% size is changed through the document. To some extent, this is also true
% of the anchor options. For instance, one may set the horizontal anchor
% |hanchor| at |\ifodd\thepage r\else l\fi| causing the anchor point to
% alternate between |r| and |l| at every new page. In conjunction with a
% similarly conditional |hpos| setting, for instance
% |\ifodd\thepage \dimexpr\paperwidth-5mm\else 5mm\fi|, this enables
% interesting effects, like placing the watermark close to the right side
% of the page on odd pages and to the left side of the page on even
% pages. Obviously, this kind of setting is only applicable with the
% |hanchor| and |vanchor| options, and not with the |anchor| shorthand.
%
% \section{The standard watermark and ways of overriding it}
% \label{sec:markcmd}
%
% In order to simplify its usage as much as possible, the
% \Lpack{draftwatermark} package is designed under the assumption that,
% most of the time, the user will need just a \emph{textual} watermark and
% some basic way to tune its visual aspect, such as choosing the text
% font, font size, scale, rotation angle, etc.
%
% \DescribeMacro{\DraftwatermarkStdMark} To this aim, the macro
% |\DraftwatermarkStdMark| is provided to do the standard job of
% typesetting the watermark text according to the configuration-defined
% font, color, angle, and so on. Then the package arranges so that this
% macro is called whenever watermarking is needed.
%
% In case the facilities provided by |\DraftwatermarkStdMark| are
% insufficient, the package user can set up his/her own macro to do the
% watermark typesetting and instruct \Lpack{draftwatermark} to use it in
% place of |\DraftwatermarkStdMark|. This is done by using the
% configuration option |markcmd| that takes the new macro name as its
% parameter. The advice is to always write a new macro rather than
% redefining or patching |\DraftwatermarkStdMark|. Note that because
% |markcmd| can be reconfigured multiple times, one may actually define
% multiple macros to get different watermark layouts on different
% pages of the same document.
%
% \begin{sloppy} It is worth recalling that when |markcmd| is set to
%   something different from |\DraftwatermarkStdMark|, the angle, font,
%   scale, color parameters will be ignored. In this case, the package
%   user is on his/her own if he/she wants to parametrize the watermark
%   appearance.
%
% \end{sloppy}
%
% \section{Compatibility notes for package version 2.0 and later}
%
% Version 2.0 of the package is an almost complete rewrite with respect to
% version 1.2. Specifically, all the configuration mechanism has been
% overhauled, by moving to an interface based on configuration keys and
% associated values.
%
% In principle, compatibility to the 1.x series should be fully preserved
% by the provision of a \emph{legacy interface}. In case old documents are
% broken by the 2.x series, please report it as a bug.
%
% The advice is not to use the legacy interface in new documents, unless
% you know that they will need to be processed in environments lacking a
% recent \Lpack{draftwatermark}.
%
% \subsection{The legacy interface}
%
% The legacy interface is composed by the following commands:
% \begin{itemize}
% \item \DescribeMacro{\SetWatermarkAngle}^^A
%   Command |\SetWatermarkAngle{<real>}| is the same as\\
%   |\DraftwatermarkOptions{angle=<real>}|.
% \item \DescribeMacro{\SetWatermarkColor}^^A
%   Command |\SetWatermarkColor[<model>]{<color spec>}| is the same as\\
%   |\DraftwatermarkOptions{color={[<model>]{<color spec>}}}|.
% \item \DescribeMacro{\SetWatermarkLightness}^^A
%   Command |\SetWatermarkLightness{<real>}| is the same as\\
%   |\DraftwatermarkOptions{color={[<gray>]{<real>}}}|. The real value
%   should be in between 0 (for black) and 1 (for white).
% \item \DescribeMacro{\SetWatermarkFontSize}^^A
%   Command |\SetWatermarkFontSize{<length>}| is the same as\\
%   |\DraftwatermarkOptions{fontsize=<length>}|.
% \item \DescribeMacro{\SetWatermarkScale}^^A
%   Command |\SetWatermarkScale{<real>}| is the same as\\
%   |\DraftwatermarkOptions{scale=<real>}|.
% \item \DescribeMacro{\SetWatermarkHorCenter}^^A
%   Command |\SetWatermarkHorCenter{<length>}| is the same as\\
%   |\DraftwatermarkOptions{hpos=<length>, hanchor=c}|.
% \item \DescribeMacro{\SetWatermarkVerCenter}^^A
%   Command |\SetWatermarkVerCenter{<length>}| is the same as\\
%   |\DraftwatermarkOptions{vpos=<length>, vanchor=m}|.
% \item \DescribeMacro{\SetWatermarkText}^^A
%   Command |\SetWatermarkText{<text>}| is the same as\\
%   |\DraftwatermarkOptions{text=<text>}|.
% \end{itemize}
%
% Note that the legacy interface supports only a subset of what is
% possible with the modern interface.
%
% \section{Examples}
% \label{sec:examples}
%
% \subsection{Plain case, using  the modern package interface}
%
% As an example, consider this first document source:
%
% \iffalse
%<*samplecode-modern> 
% \fi
%    \begin{macrocode}
\documentclass{article}
\usepackage[named]{xcolor}
\usepackage[T1]{fontenc}
\usepackage[firstpageonly, color={[gray]{0.5}}]{draftwatermark}
\usepackage{mathptmx}
\usepackage{lipsum}
\title{Sample document for the draftwatermark package}
\author{}

\begin{document}
\maketitle

\section{One}
\lipsum[1-3]

\section{Two}
\lipsum[4-6]

\end{document}
%    \end{macrocode}
% \iffalse
%</samplecode-modern>
% \fi
% \makeatletter \c@CodelineNo\z@ \makeatother
%
% This produces a two pages document with placeholder content, putting a
% page-centered watermark on the first page only. The watermark is made a
% slightly darker toneof gray than the default.
%
% \subsection{Plain case, using the legacy interface}
%
% Using the legacy interface, the source for the same sample document as
% above would look like:
% \iffalse
%<*samplecode-legacy> 
% \fi
%    \begin{macrocode}
\documentclass{article}
\usepackage[named]{xcolor}
\usepackage[T1]{fontenc}
\usepackage[firstpage]{draftwatermark}
\usepackage{mathptmx}
\usepackage{lipsum}

\SetWatermarkLightness{0.5}

\title{Sample document for the draftwatermark package}
\author{}

\begin{document}
\maketitle

\section{One}
\lipsum[1-3]

\section{Two}
\lipsum[4-6]

\end{document}
%    \end{macrocode}
% \iffalse
%</samplecode-legacy>
% \fi
% \makeatletter \c@CodelineNo\z@ \makeatother
%
% \subsection{Things that cannot be done with the legacy interface}
%
% A major limitation of the legacy interface that has been removed with
% the modern interface is the inability to control the watermark
% anchoring. For instance, one may want the watermark to appear in the
% right top angle of every page. With the modern interface, such a result
% can be easily obtained as in the following sample code:
% \iffalse
%<*samplecode-anchor> 
% \fi
%    \begin{macrocode}
\documentclass{article}
\usepackage[named]{xcolor}
\usepackage[T1]{fontenc}
\usepackage[firstpage, anchor=tr, color=red,
  pos={\dimexpr\paperwidth-5mm, 5mm},
  angle=-45, fontsize=32pt]{draftwatermark}
\usepackage{mathptmx}
\usepackage{lipsum}
\title{Sample document for the draftwatermark package}
\author{}

\DraftwatermarkOptions{text=\bfseries DRAFT}
\begin{document}
\maketitle

\section{One}
\lipsum[1-3]

\section{Two}
\lipsum[4-6]

\end{document}
%    \end{macrocode}
% \iffalse
%</samplecode-anchor>
% \fi
% \makeatletter \c@CodelineNo\z@ \makeatother
%
% Another interesting possibility of the modern interface derives from the
% fact that the positioning and anchoring options are re-evaluated for
% every page. This allows the positioning to dynamically change following
% other parameters (for instance the |\paperwidth|) or to take advantage
% of conditionals to achieve effects such as differentiating the watermark
% for odd and even pages, as shown in the following code:
% \iffalse
%<*samplecode-dynamic> 
% \fi
%    \begin{macrocode}
\documentclass{article}
\usepackage[named]{xcolor}
\usepackage[T1]{fontenc}
\usepackage[color=red, fontsize=32pt]{draftwatermark}
\usepackage{mathptmx}
\usepackage{lipsum}
\title{Sample document for the draftwatermark package}
\author{}

\DraftwatermarkOptions{%
  angle=0,
  text={\ifodd\thepage ODD\else EVEN\fi},
  hpos={\ifodd\thepage \dimexpr\paperwidth-5mm\else 5mm\fi},
  vpos=5mm,
  vanchor=t,
  hanchor={\ifodd\thepage r\else l\fi}
}

\begin{document}
\maketitle

\section{One}
\lipsum[1-3]

\section{Two}
\lipsum[4-6]

\end{document}
%    \end{macrocode}
% \iffalse
%</samplecode-dynamic>
% \fi
% \makeatletter \c@CodelineNo\z@ \makeatother
%
% \subsection{Maximum freedom with a custom watermarking command}
%
% Maximum freedom can be obtained with a custom watermarking command. The
% following example shows how it can be used, once again differentiating
% the watermark for odd and even pages.
%\iffalse
%<*samplecode-custom> 
% \fi
%    \begin{macrocode}
\documentclass{article}
\usepackage[named]{xcolor}
\usepackage[T1]{fontenc}
\usepackage{draftwatermark}
\usepackage{mathptmx}
\usepackage[scaled]{helvet}
\usepackage{lipsum}
\title{Sample document for the draftwatermark package}
\author{}

\newsavebox\wmbox
\savebox\wmbox{%
  {\color[rgb]{1,0.8,0.8}\sffamily \fbox{DRAFT}}}
\newcommand\MyWM{%
  \ifodd\thepage
    \hspace*{\dimexpr \paperwidth -\wd\wmbox-10mm}%
    \usebox{\wmbox}%
  \else
    \usebox{\wmbox}%
  \fi}

\DraftwatermarkOptions{anchor=lt, pos={5mm, 5mm}, markcmd=\MyWM}
\begin{document}
\maketitle

\section{One}
\lipsum[1-3]

\section{Two}
\lipsum[4-6]

\end{document}
%    \end{macrocode}
% \iffalse
%</samplecode-custom>
% \fi
% \makeatletter \c@CodelineNo\z@ \makeatother
%
% 
% \section{Development and support}
%
% The package is developed on \emph{github}:
% \begin{quote}
%   \url{https://github.com/callegar/LaTeX-draftwatermark}
% \end{quote}
% Please, refer to that site for any bug report or development
% information.  Be so kind to refrain from opening bugs for enhancement
% requests that, from the present document, would evidently require moving
% against some fundamental design decisions.
%
% \section{Changelog}
%
% \begin{description}
% \item[1.0 - 2006/06/30] Initial version.
% \item[1.1 - 2012/01/06] Add support for colored watermarks; options to
%   disable watermarking; many small fixes in the documentation.
% \item[1.2 - 2015/02/19] Add support for watermark position.
% \item[2.0 - 2020/03/08] Configuration management overhaul; introduction
%   of a legacy interface for compatibility.
% \item[2.1 - 2020/05/11] Allow anchor information to be re-evaluated on
%   every page
% \end{description}
%
% \StopEventually {}
% 
% \section{Implementation}
%
% Announce the name and version of the package, which requires
% \LaTeXe.
% \iffalse
%<*draftwatermark>
% \fi
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{draftwatermark}%
  [2020/05/11 2.1 Put a gray textual watermark on document pages]

%    \end{macrocode}
%
% Require the needed packages.
%    \begin{macrocode}
\RequirePackage{kvoptions}
\RequirePackage{everypage}[2007/06/20]
\RequirePackage{graphicx}
\RequirePackage{color}

%    \end{macrocode}
%
% Define the configuration options and default values.
%    \begin{macrocode}
\DeclareBoolOption[false]{firstpageonly}
\define@key{draftwatermark}{firstpage}[true]{%
  \csname draftwatermark@firstpageonly#1\endcsname}
\DeclareBoolOption[true]{stamp}
\DeclareComplementaryOption{nostamp}{stamp}
\DeclareComplementaryOption{final}{stamp}
\DeclareStringOption[45]{angle}
\DeclareStringOption[1]{scale}
\DeclareStringOption[DRAFT]{text}
\DeclareStringOption[0.5\paperwidth]{hpos}
\DeclareStringOption[0.5\paperheight]{vpos}
\DeclareStringOption[0.25\paperwidth]{fontsize}
\DeclareStringOption[gray]{colormodel}
\DeclareStringOption[0.8]{colorspec}
\DeclareStringOption[c]{hanchor}
\DeclareStringOption[m]{vanchor}
\DeclareStringOption[\DraftwatermarkStdMark]{markcmd}
\define@key{draftwatermark}{pos}{%
  \draftwatermark@processpos #1\@nil}
\define@key{draftwatermark}{anchor}{%
  \draftwatermark@processanchor{#1}}
\define@key{draftwatermark}{color}{%
  \draftwatermark@processcolor #1\@nil}

%    \end{macrocode}
%
% Set up some helper marcros to process the options. These should be
% rather self-explanatory.
%    \begin{macrocode}
\def\draftwatermark@processpos#1,#2\@nil{%
  \def\draftwatermark@hpos{#1}%
  \def\draftwatermark@vpos{#2}}

\def\draftwatermark@processanchor#1{%
  \def\draftwatermark@tempa{#1}
  \@onelevel@sanitize \draftwatermark@tempa
  \expandafter \@tfor \expandafter \draftwatermark@tempb
    \expandafter :\expandafter =\draftwatermark@tempa
    \do
    {%
      \if \draftwatermark@tempb l%
        \def \draftwatermark@hanchor{l}%
      \else \if \draftwatermark@tempb c%
        \def \draftwatermark@hanchor{c}%
      \else \if \draftwatermark@tempb r%
        \def \draftwatermark@hanchor{r}%
      \else \if \draftwatermark@tempb t%
        \def \draftwatermark@vanchor{t}%
      \else \if \draftwatermark@tempb m%
        \def \draftwatermark@vanchor{m}%
      \else \if \draftwatermark@tempb b%
        \def \draftwatermark@vanchor{b}
      \else
        \PackageError{draftwatermark}{%
          Illegal anchor directive `\draftwatermark@tempb'}%
          {Directive has been ingnored.}%
      \fi\fi\fi\fi\fi\fi
    }}

\def\draftwatermark@processcolor{%
  \@ifnextchar[
    \draftwatermark@processcolor@ii\draftwatermark@processcolor@i}

\def\draftwatermark@processcolor@i#1\@nil{%
  \def\draftwatermark@colormodel{}%
  \def\draftwatermark@colorspec{#1}}

\def\draftwatermark@processcolor@ii[#1]#2\@nil{%
  \def\draftwatermark@colormodel{#1}%
  \def\draftwatermark@colorspec{#2}}

%    \end{macrocode}
%
% Process the package options\dots
%    \begin{macrocode}
\ProcessKeyvalOptions*
%    \end{macrocode}
% 
% \dots\, and then disable the |firstpageonly| and |firstpage| options,
% that can only be used at the package loading time.
%    \begin{macrocode}
\DisableKeyvalOption{draftwatermark}{firstpageonly}
\DisableKeyvalOption{draftwatermark}{firstpage}

%    \end{macrocode}
%
% \begin{macro}{\DraftwatermarkOptions}
%   Set up a command to modify the configuration as needed.
%    \begin{macrocode}
\newcommand\DraftwatermarkOptions[1]{\setkeys{draftwatermark}{#1}}

%    \end{macrocode}
% \end{macro}
%
% Introduce the legacy interface.
% \begin{macro}{\SetWatermarkAngle}
%   Legacy command to set the rotation angle for the standard watermark
%    \begin{macrocode}
\newcommand\SetWatermarkAngle[1]{\DraftwatermarkOptions{angle=#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetWatermarkFontSize}
%   Legacy command to set the font size for the standard watermark
%    \begin{macrocode}
\newcommand\SetWatermarkFontSize[1]{\DraftwatermarkOptions{fontsize=#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetWatermarkScale}
%   Legacy command to set the scale for the standard watermark
%    \begin{macrocode}
\newcommand\SetWatermarkScale[1]{\DraftwatermarkOptions{scale=#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetWatermarkHorCenter}
% \begin{macro}{\SetWatermarkVerCenter}
% Legacy commands to set the horizontal and vertical position for the
% standard watermark, also forcing it to be centered there.
%    \begin{macrocode}
\newcommand\SetWatermarkHorCenter[1]{%
  \DraftwatermarkOptions{hpos=#1, hanchor=c}}
\newcommand\SetWatermarkVerCenter[1]{%
  \DraftwatermarkOptions{vpos=#1, vanchor=m}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \begin{macro}{\SetWatermarkText}
%   Legacy command to set the text of the standard watermark
%    \begin{macrocode}
\newcommand\SetWatermarkText[1]{\DraftwatermarkOptions{text=#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetWatermarColor}
% \begin{macro}{\SetWatermarkLightness}
% Legacy commands to set the color for the
% standard watermark.
%    \begin{macrocode}
\newcommand\SetWatermarkColor[2][]{%
  \DraftwatermarkOptions{colormodel=#1, colorspec=#2}}
\newcommand\SetWatermarkLightness[1]{%
  \DraftwatermarkOptions{colormodel=gray, colorspec=#1}}

%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\DraftwatermarkStdMark}
% The command to generate the standard watermark
%    \begin{macrocode}
\newcommand\DraftwatermarkStdMark{%
  \rotatebox{\draftwatermark@angle}{%
    \scalebox{\draftwatermark@scale}{%
      \begingroup
      \ifx\draftwatermark@colormodel\@empty
        \color{\draftwatermark@colorspec}%
      \else
        \color[\draftwatermark@colormodel]{\draftwatermark@colorspec}%
      \fi
      \setlength{\@tempdima}{\draftwatermark@fontsize}%
      \fontsize{\@tempdima}{1.2\@tempdima}\selectfont
      \draftwatermark@text
      \endgroup}}}

%    \end{macrocode}
% \end{macro}
%
% The code to actually print the watermark
%    \begin{macrocode}
\newcommand\draftwatermark@printwm[1]{%
  \sbox\@tempboxa{#1}%
  \setlength{\@tempdima}{\draftwatermark@hpos}%
  \setlength{\@tempdimb}{\draftwatermark@vpos}%
  \edef \draftwatermark@tempa{\draftwatermark@hanchor}%
  \@onelevel@sanitize \draftwatermark@tempa
  \expandafter\def\expandafter\draftwatermark@tempb
    \expandafter{\expandafter\@car \draftwatermark@tempa\@nil}%
  \if \draftwatermark@tempb r%
    \addtolength\@tempdima{-\wd\@tempboxa}%
  \else\if \draftwatermark@tempb c%
    \addtolength\@tempdima{-0.5\wd\@tempboxa}%
  \else\if \draftwatermark@tempb l%
    \relax
  \else
    \PackageError{draftwatermark}{%
      Illegal anchor directive `\draftwatermark@tempb'}%
        {Anchoring to left side.}%
  \fi\fi\fi  
  \edef \draftwatermark@tempa{\draftwatermark@vanchor}%
  \@onelevel@sanitize \draftwatermark@tempa
  \expandafter\def\expandafter\draftwatermark@tempb
    \expandafter{\expandafter\@car \draftwatermark@tempa\@nil}%
  \if \draftwatermark@tempb b%
    \addtolength\@tempdimb{-\ht\@tempboxa}%
    \addtolength\@tempdimb{-\dp\@tempboxa}%
  \else\if \draftwatermark@tempb m%
    \addtolength\@tempdimb{-0.5\ht\@tempboxa}%
    \addtolength\@tempdimb{-0.5\dp\@tempboxa}%
  \else\if \draftwatermark@tempb t%
    \relax
  \else
    \PackageError{draftwatermark}{%
      Illegal anchor directive `\draftwatermark@tempb'}%
        {Anchoring to top side.}%
  \fi\fi\fi 
  \vbox to 0pt {%
      \vspace*{-1in}%
      \vspace*{\@tempdimb}%
      \hbox to 0pt {%
        \hspace*{-1in}%
        \hspace*{\@tempdima}%
        \usebox\@tempboxa
        \hss}%
      \vss}}

%    \end{macrocode}
%
% A wrapper to make the watermark printing conditional
%    \begin{macrocode}
\newcommand\draftwatermark@print[1]{%
  \ifdraftwatermark@stamp
    \draftwatermark@printwm{#1}%
  \fi}
%    \end{macrocode}
%
% \dots\, and finally the code to set up the \Lpack{everypage} hooks to
% assure that the watermark printing commands are called when needed
%    \begin{macrocode}
\ifdraftwatermark@firstpageonly
  \AddThispageHook{\draftwatermark@print{\draftwatermark@markcmd}}
\else
  \AddEverypageHook{\draftwatermark@print{\draftwatermark@markcmd}}
\fi
%    \end{macrocode}


% \iffalse
%</draftwatermark>
% \fi
%
% \Finale
% \PrintIndex
%
%% \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         \~}
\endinput

%%% Local Variables: 
%%% mode: doctex
%%% TeX-master: t
%%% End: