summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/pgfopts/pgfopts.dtx
blob: 0a33eb88dcd7261c029ca5916d833d6ca084ea77 (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
% \iffalse meta-comment
% !TeX program = pdfLaTeX
%<*internal>
\iffalse
%</internal>
%<*readme>
pgfopts - LaTeX package options with pgfkeys
============================================

Using key-value options for packages and macros is a good way of
handling large numbers of options with a clean interface. The
`pgfkeys` package provides a very well designed system for
defining and using keys, but does not make this available for
handling LaTeX class and package options. The `pgfopts` package
adds this ability to `pgfkeys`, in the same way that `kvoptions`
extends the `keyval` package.

Installation
------------

The package is supplied in `.dtx` format and as a pre-extracted
zip file, `pgfopts.tds.zip`. The later is most convenient for
most users: simply unzip this in your local `texmf` directory.
If you want to unpack the `.dtx` yourself, running `tex
pgfopts.dtx` will extract the package whereas 'latex pgfopts.dtx
will extract it and also typeset the documentation.

Typesetting the documentation requires a number of packages in
addition to those needed to use the package. This is mainly 
because of the number of demonstration items included in the 
text. To compile the documentation without error, you will 
need the packages:
 - `csquotes`
 - `helvet`
 - `hypdoc`
 - `listings`
 - `lmodern`
 - `mathpazo`
 - `microtype`
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
pgfopts --- LaTeX package options with pgfkeys
E-mail: joseph.wright@morningstar2.co.uk
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2008-2011,2013,2014 by
  Joseph Wright <joseph.wright@morningstar2.co.uk>

It may be distributed and/or modified under the conditions of
the LaTeX Project Public License (LPPL), either version 1.3c of
this license or (at your option) any later version.  The latest
version of this license is in the file:

   http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by
  Joseph Wright.

This work consists of the file  pgfopts.dtx
          and the derived files pgfopts.ins and
                                pgfopts.pdf.
                                
\endpostamble
\usedir{tex/latex/pgfopts}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/pgfopts}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/pgfopts}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage{csquotes,lmodern,pgfopts}
\usepackage[osf]{mathpazo}
\usepackage[scaled=0.95]{helvet}
\usepackage[T1]{fontenc}
\usepackage[final]{listings,microtype}
\usepackage[numbered]{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%\GetFileInfo{\jobname.sty}
%
%\makeatletter
%
%^^A For creating examples with nice highlighting of code, and so
%^^A on; based on the system used in the listings source (lstsample).
%\lst@RequireAspects{writefile}
%\lstnewenvironment{LaTeXdemo}[1][code and example]^^A
%  {^^A
%    \global\let\lst@intname\@empty
%    \expandafter\let\expandafter\LaTeXdemo@end
%      \csname LaTeXdemo@#1@end\endcsname
%    \@nameuse{LaTeXdemo@#1}^^A
%  }
%  {\LaTeXdemo@end}
%\newcommand*\LaTeXdemo@new[3]{^^A
%  \expandafter\newcommand\expandafter*\expandafter
%    {\csname LaTeXdemo@#1\endcsname}{#2}^^A
%  \expandafter\newcommand\expandafter*\expandafter
%    {\csname LaTeXdemo@#1@end\endcsname}{#3}^^A
%}
%\newcommand*\LaTeXdemo@common{^^A
%  \setkeys{lst}
%    {
%      basicstyle   = \small\ttfamily,
%      basewidth    = 0.51em,
%      gobble       = 3,
%      keywordstyle = \color{blue},
%      language     = [LaTeX]{TeX},
%      moretexcs    = 
%        {
%          pgfkeys                  ,
%          ProcessPgfOptions        ,
%          ProcessPgfPackageOptions ,
%        }
%    }^^A 
%}
%\newcommand*\LaTeXdemo@input{^^A
%  \MakePercentComment
%  \catcode`\^^M=10\relax
%  \small
%  \begingroup
%    \setkeys{lst}^^A
%      {^^A
%        SelectCharTable=\lst@ReplaceInput{\^\^I}{\lst@ProcessTabulator}
%      }^^A
%    \leavevmode 
%    \input{\jobname.tmp}^^A
%  \endgroup
%  \MakePercentIgnore
%}
%\LaTeXdemo@new{code only}{\LaTeXdemo@common}{}
%
%\MakeShortVerb\"
%\DeleteShortVerb\|
%
%\providecommand*\file{\texttt}
%\providecommand*\opt{\texttt}
%\providecommand*\pkg{\textsf}
%
%\makeatother
%
%\title{^^A
%  \textsf{pgfopts} --- LaTeX package options with 
%  \pkg{pgfkeys}\thanks{^^A
%    This file describes version \fileversion, last revised 
%    \filedate.^^A
%  }^^A
%}
%\author{^^A
%  Joseph Wright\thanks{E-mail: joseph.wright@morningstar2.co.uk}^^A
%}
%\date{Released \filedate}
%
%\maketitle
%
%\changes{v1.0}{2008/06/26}{First public release}
%\changes{v2.0}{2009/04/30}{Complete internal rewrite}
%\changes{v2.1}{2011/06/02}{Allow for \texttt{.unknown} handler for
%  class options}
%\changes{v2.1a}{2014/07/10}{Fix incorrect \cs{Provides\ldots} line}
%
%\begin{abstract}
%  Using key--value options for packages and macros is a good way of
%  handling large numbers of options with a clean interface. The
%  \pkg{pgfkeys} package provides a very well designed system for
%  defining and using keys, but does not make this available for
%  handling LaTeX class and package options.  The \pkg{pgfopts} package
%  adds this ability to \pkg{pgfkeys}, in the same way that
%  \pkg{kvoptions} extends the \pkg{keyval} package.
%\end{abstract}
%
%\tableofcontents
%
%\section{Introduction}
%
% The key--value method for optional arguments is very popular, as it
% allows the class or package author to define a large number of
% options with a simple interface.  A number of packages can be used
% to provide the key management system, most of which load or extent
% the parent \pkg{keyval} package.  On its own, \pkg{keyval} can only
% be used for parsing the arguments of macros.  However, a number of
% packages have extended the method to processing LaTeX class and
% package options.  This processing is made available as part of two
% general-purpose packages \pkg{xkeyval} and \pkg{kvoptions}; both
% allow the author of a class or package to process key--value
% options given at load-time.
%
% The \pkg{pgfkeys} package provides a somewhat different key--value
% system to \pkg{keyval} and derivatives.  This uses a completely
% different model for defining and using keys, although for the
% end-user the result appears very similar.  The \pkg{pgfopts} package
% allows keys defined with \pkg{pgfkeys} to be used as class or
% package options, in the same way that \pkg{kvoptions} extends
% \pkg{keyval}.
%
% Users of \pkg{pgfopts} should be familiar with the general methods 
% used by \pkg{pgfkeys}.  These are outlined in the manual for the
% Ti\emph{k}z and \pkg{pgf} bundle.
%
%\section{Installation}
%
% The package is supplied in \file{dtx} format and as a pre-extracted
% zip file, \file{\jobname.tds.zip}. The later is most convenient for
% most users: simply unzip this in your local texmf directory and
% run \texttt{texhash} to update the database of file locations. If
% you want to unpack the \file{dtx} yourself, running 
% \texttt{tex \jobname.dtx} will extract the package whereas
% \texttt{latex \jobname.dtx} will extract it and also typeset the
% documentation.
% 
% Typesetting the documentation requires a number of packages in
% addition to those needed to use the package. This is mainly 
% because of the number of demonstration items included in the text. To
% compile the documentation without error, you will need the packages:
% \begin{itemize}
%   \item \pkg{csquotes}
%   \item \pkg{helvet}
%   \item \pkg{hypdoc}
%   \item \pkg{listings}
%   \item \pkg{lmodern}
%   \item \pkg{mathpazo}
%   \item \pkg{microtype}
%\end{itemize}
%
%\section{Using the package}
%
%\subsection{Creating options}
% 
% To create package or class options for use with \pkg{pgfopts}, it is
% only necessary to define the appropriate keys.  Taking as an
% example a package \enquote{\pkg{MyOwnPackage}}, which uses the prefix
% \texttt{MOP} on internal macros, creating keys would take the form:
%\begin{LaTeXdemo}[code only]
%  \pgfkeys{
%    /MOP/.cd,
%    keyone/.code=\wlog{Value '#1' given},
%    keytwo/.store in=\MOP@store
%  }
%\end{LaTeXdemo}
% Here, \opt{keyone} simply writes its argument to the log, while
% \opt{keytwo} stores the value given in the \cs{MOP@store} macro.
%
% An important point to notice is that the key names \emph{do not}
% contain a space. This is because the LaTeX kernel removes spaces
% from options before they are passed to the class or package. Spaces
% can occur in the path to the key, but not in the key name itself.
% This restriction only applies to keys used as options.
% 
%\subsection{Processing options}
%
%\DescribeMacro{\ProcessPgfOptions}
% The \cs{ProcessPgfOptions} macro is used to process package or
% class options using \pkg{pgfkeys}. When used in a package, it
% will also examine the available class options, and use any which
% match declared package options. \cs{ProcessPgfOptions} requires
% the \pkg{pgfkeys} key \meta{path} to search for options. Thus
% for the example of \pkg{MyOwnPackage} given in the previous 
% section, the appropriate call would be
%\begin{LaTeXdemo}[code only]
%  \ProcessPgfOptions{/MOP}
%\end{LaTeXdemo} 
% Alternatively, \cs{ProcessPgfOptions} can be given with a 
% star:
%\begin{LaTeXdemo}[code only]
%  \ProcessPgfOptions*
%\end{LaTeXdemo}
% The macro will then use the current file name as the path. This will
% be the name of the current class or package, as appropriate.
%
%\DescribeMacro{\ProcessPgfPackageOptions}
%\changes{v2.0}{2010/05/01}{New \cs{ProcessPgfPackageOptions}
%  macro}
% As a complement to \cs{ProcessPgfOptions}, the macro 
% \cs{ProcessPgfPackageOptions} is also provided. As the name indicates,
% this is intended for use in packages. It does \emph{not} examine the
% list of global class options, processing only the list of options
% given for the class itself. If used in a class, the behaviour will
% be identical to \cs{ProcessPgfOptions}.
%
%\StopEventually{^^A
%  \PrintChanges
%  \PrintIndex
%}
%
%\section{Implementation}
%
% The code here is based heavily on \pkg{kvoptions}, which has a
% similar aim to \pkg{pgfopts}, but works with \pkg{keyval} and derived
% packages.
% 
%    \begin{macrocode}
%<*package>
\ProvidesPackage{pgfopts}
  [2014/07/10 v2.1a LaTeX package options with pgfkeys]
%    \end{macrocode} 
%    
% The only package requires is \pkg{pgfkeys} itself.
%    \begin{macrocode}
\RequirePackage{pgfkeys}
%    \end{macrocode}
%    
%\begin{macro}{\ifpgfopts@process@class}
% The processing of options can apply to those which are given for 
% a class, or can be limited to those of the package only.
%    \begin{macrocode}
\newif\ifpgfopts@process@class
%    \end{macrocode}
%\end{macro}  
%
%\begin{macro}{\pgfopts@options@clist}
% A comma-separated list of options to process.
%    \begin{macrocode}
\newcommand*\pgfopts@options@clist{}
%    \end{macrocode}
%\end{macro}  
%
%\begin{macro}{\pgfopts@options@execute}
% A storage macro which specifies the final list of options to
% actually execute.
%    \begin{macrocode}
\newcommand*\pgfopts@options@execute{}
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\pgfopts@key@path}
%\begin{macro}{\pgfopts@process@options}
% The main processing macro first clears the list of options to deal
% with, and stores the key path to use. Global and local options are
% then added to the \enquote{to do} list before executing the options.
%    \begin{macrocode}
\newcommand*\pgfopts@key@path{}
\newcommand\pgfopts@process@options[1]{%
  \def\pgfopts@options@clist{}%
  \def\pgfopts@options@execute{}%
  \def\pgfopts@key@path{#1/}%
  \ifx\@currext\@clsextension\else
    \expandafter\pgfopts@check@class@options
  \fi
  \pgfopts@process@local@options
  \pgfopts@options@execute
  \let\CurrentOption\@empty
  \AtEndOfPackage{\let\@unprocessedoptions\relax}%
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%
%\begin{macro}{\pgfopts@current@option}
%\begin{macro}{\pgfopts@check@class@options}
%\begin{macro}{\pgfopts@check@class@options@aux}
% First, a check to see if class options should be processed, and if so
% if there are any. If so, then each option needs to be examined. Any
% \texttt{=\meta{value}} is removed, and a test is made to see if
% the resulting \meta{key} was defined for the current \meta{path}.
% If so, the option is added to the list for processing later, and
% it is removed from the list of unused options. The syntax for the
% later process is rather complex, but LaTeX2e's function to 
% achieve that is somewhat awkward.
%    \begin{macrocode}
\newcommand*\pgfopts@current@option{}
\newcommand*\pgfopts@check@class@options{%
  \ifpgfopts@process@class
    \ifx\@classoptionslist\relax\else
      \expandafter\expandafter\expandafter
        \pgfopts@check@class@options@aux
    \fi
  \fi
}
\newcommand*\pgfopts@check@class@options@aux{%
  \@for\pgfopts@current@option:=\@classoptionslist\do
    {%
      \pgfkeysifdefined
        {%
          \pgfopts@key@path
          \pgfopts@get@key@name\pgfopts@current@option
          /.@cmd%
        }%
        {%
          \pgfopts@list@add\pgfopts@options@clist\pgfopts@current@option
          \@expandtwoargs\@removeelement\pgfopts@current@option
            \@unusedoptionlist\@unusedoptionlist          
        }%
        {}%
    }%
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%
%\begin{macro}{\pgfopts@local@options}
%\begin{macro}{\pgfopts@process@local@options}
%\begin{macro}{\pgfopts@process@local@options@aux@i}
%\begin{macro}{\pgfopts@process@local@options@aux@ii}
%\begin{macro}{\pgfopts@process@local@options@class}
%\begin{macro}{\pgfopts@process@local@options@class@aux}
%\begin{macro}{\pgfopts@process@local@options@package}
% The first step in processing local options is to see if any exist.
% This is done inside a group to avoid polluting the hash table. If 
% options are found, these are transferred into a storage macro and
% the auxiliary function is called. There is then a fork depending on
% whether a package or class is being processed. Once the list is
% finalised, the execution macro is set up appropriately.
%    \begin{macrocode}
\newcommand*\pgfopts@local@options{}
\newcommand*\pgfopts@process@local@options{%
  \begingroup
    \@ifundefined{opt@\@currname.\@currext}%
      {\endgroup}%
      {%
        \toks@\expandafter\expandafter\expandafter
          {\csname opt@\@currname.\@currext\endcsname}%
        \expandafter\endgroup
        \expandafter\def\expandafter\pgfopts@local@options
          \expandafter{\the\toks@}%
        \pgfopts@process@local@options@aux@i
      }%
}
\newcommand*\pgfopts@process@local@options@aux@i{%
  \ifx\@currext\@clsextension
    \expandafter\pgfopts@process@local@options@class
  \else
    \expandafter\pgfopts@process@local@options@package
  \fi
  \ifx\pgfopts@options@clist\@empty\else
    \expandafter\pgfopts@process@local@options@aux@ii
  \fi
}
\newcommand*\pgfopts@process@local@options@aux@ii{%
  \begingroup
    \toks@\expandafter{\pgfopts@options@clist}%
    \edef\pgfopts@options@execute
      {%
        \noexpand\pgfkeys
          {%
            \pgfopts@key@path .cd,%
            \the\toks@
          }%
      }%
  \expandafter\endgroup
  \expandafter\def\expandafter\pgfopts@options@execute
    \expandafter{\pgfopts@options@execute}%
}
%    \end{macrocode}
% Options given for a class may not be applicable to the class itself,
% and so they have to be checked. First, there is a simple test for an
% unknown key handler: if it exists then there is no need to look at
% each option separately.  
%    \begin{macrocode}
\newcommand*\pgfopts@process@local@options@class
  {%
    \pgfkeysifdefined{\pgfopts@key@path .unknown/.@cmd}%
      {\pgfopts@list@add\pgfopts@options@clist\@classoptionslist}
      {\pgfopts@process@local@options@class@aux}%
  }
\newcommand*\pgfopts@process@local@options@class@aux{%
  \@for\pgfopts@current@option:=\pgfopts@local@options\do{%
    \pgfkeysifdefined
      {%
        \pgfopts@key@path
        \pgfopts@get@key@name\pgfopts@current@option
        /.@cmd%
      }%
      {\pgfopts@list@add\pgfopts@options@clist\pgfopts@current@option}%
      {\pgfopts@list@add\@unusedoptionlist\pgfopts@current@option}%
  }%
}
%    \end{macrocode}
% For packages, the local options are simply added to the list already
% set up from the global values.    
%    \begin{macrocode}
\newcommand*\pgfopts@process@local@options@package{%
  \pgfopts@list@add\pgfopts@options@clist\pgfopts@local@options
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%
%\begin{macro}{\pgfopts@get@key@name}
%\begin{macro}{\pgfopts@get@key@name@aux}
% A simple function to leave only the key \meta{name} in the input
% stream, whether there is a \meta{value} or not. This needs to work 
% with options which do not contain an equals sign at all. It is
% designed to work with a stored value.
%    \begin{macrocode}
\newcommand\pgfopts@get@key@name[1]{%
  \expandafter\pgfopts@get@key@name@aux#1=\@nil
}
\def\pgfopts@get@key@name@aux#1=#2\@nil{#1}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%
%\begin{macro}{\pgfopts@list@add@a@toks}
%\begin{macro}{\pgfopts@list@add@b@toks}
%\begin{macro}{\pgfopts@list@add@temp}
%\begin{macro}{\pgfopts@list@add}
% This function takes "#1" as the name of a comma-separated list and
% "#2" as the name of a macro containing content to add to the list. 
% After the appropriate checks it adds the content of "#2" to the 
% right hand end of variable "#1".
%    \begin{macrocode}
\newtoks\pgfopts@list@add@a@toks
\newtoks\pgfopts@list@add@b@toks
\newcommand*\pgfopts@list@add@temp{}
\newcommand\pgfopts@list@add[2]{%
  \pgfopts@list@add@a@toks\expandafter{#2}%
  \def\pgfopts@list@add@temp{#2}%
  \pgfopts@list@add@b@toks\expandafter{#1}%
  \ifx\pgfopts@options@clist\@empty
    \edef#1{\the\pgfopts@list@add@a@toks}%
  \else
    \ifx\pgfopts@list@add@temp\@empty\else
      \edef#1%
        {\the\pgfopts@list@add@b@toks,\the\pgfopts@list@add@a@toks}%
    \fi  
  \fi
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%    
%\begin{macro}{\ProcessPgfOptions}
%\begin{macro}{\ProcessPgfPackageOptions}
%\begin{macro}{\pgfopts@star@check}
% The two user functions set the flag for class option processing. 
% A shared internal function then checks for a star, and either adds
% the current name or looks to pick one up from the input stream.
%    \begin{macrocode}
\newcommand*\ProcessPgfOptions{%
  \pgfopts@process@classtrue
  \pgfopts@star@check
}
\newcommand*\ProcessPgfPackageOptions{%
  \pgfopts@process@classfalse
  \pgfopts@star@check
}
\newcommand*\pgfopts@star@check{%
  \@ifstar
    {%
      \begingroup
      \edef\@tempa
        {%
          \endgroup
          \noexpand\pgfopts@process@options{/\@currname}%
        }% 
      \@tempa
    }%
    {\pgfopts@process@options}%
}
\@onlypreamble\ProcessPgfOptions
\@onlypreamble\ProcessPgfPackageOptions
%</package>
%    \end{macrocode}
%\end{macro} 
%\end{macro} 
%\end{macro}  
% 
%\Finale