summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/keycommand/keycommand.dtx
blob: 1a5c641058a7068e223e98dc48be38046bff8e0d (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
% \iffalse meta-comment
% keycommand : an easy way to define commands with keys
%
% This work 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
%
% This work consists of the main source file keycommand.dtx
% and the derived files
%    keycommand.sty, keycommand.pdf, keycommand.ins,
%    keycommand-example.tex
%
% Unpacking:
%    (a) If keycommand.ins is present:
%           etex keycommand.ins
%    (b) Without keycommand.ins:
%           etex keycommand.dtx
%    (c) If you insist on using LaTeX
%           latex \let\install=y\input{keycommand.dtx}
%        (quote the arguments according to the demands of your shell)
%
% Documentation:
%           (pdf)latex keycommand.dtx
% Copyright (C) 2009 by Florent Chervet <florent.chervet@free.fr>
%<*ignore>
\begingroup
  \def\x{LaTeX2e}%
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex
\Msg{************************************************************************}
\Msg{* Installation}
\Msg{* Package: keycommand 2009/07/22 v1.0 an easy way to define commands with keys}
\Msg{************************************************************************}

\keepsilent
\askforoverwritefalse

\let\MetaPrefix\relax
\preamble

This is a generated file.

keycommand : an easy way to define commands with keys

This work 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

This work consists of the main source file keycommand.dtx
and the derived files
   keycommand.sty, keycommand.pdf, keycommand.ins,
   keycommand-example.tex

keycommand : an easy way to define commands with keys
Copyright (C) 2009 by Florent Chervet <florent.chervet@free.fr>

\endpreamble
\let\MetaPrefix\DoubleperCent

\generate{%
  \file{keycommand.ins}{\from{keycommand.dtx}{install}}%
  \file{keycommand.sty}{\from{keycommand.dtx}{package}}%
  \file{keycommand-example.tex}{\from{keycommand.dtx}{example}}%
}

\obeyspaces
\Msg{************************************************************************}
\Msg{*}
\Msg{* To finish the installation you have to move the following}
\Msg{* file into a directory searched by TeX:}
\Msg{*}
\Msg{*     keycommand.sty}
\Msg{*}
\Msg{* To produce the documentation run the file `keycommand.dtx'}
\Msg{* through LaTeX.}
\Msg{*}
\Msg{* Happy TeXing!}
\Msg{*}
\Msg{************************************************************************}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\documentclass[a4paper,oneside]{ltxdoc}
\usepackage[latin9]{inputenc}
\usepackage[american]{babel}
\usepackage{etex,holtxdoc,geometry,tocloft,fancyhdr,color,bbding}\CodelineNumbered
\lastlinefit999
\font\ding=dingbat scaled 800
\geometry{top=2cm,headheight=1cm,headsep=.3cm,bottom=2cm,left=5cm,right=3cm}
\fancyhf{}\fancyhead[L]{The \xpackage{keycommand} package -- easy way to define commands with keys}
\fancyfoot[L]{\color[gray]{.35}\scriptsize\xpackage{keycommand}\quad\copyright\oldstylenums{2009}\,\lower.3ex\hbox{\NibRight}\,Florent Chervet}
\fancyfoot[R]{\oldstylenums{\thepage} / \oldstylenums{\pageref{LastPage}}}
\pagestyle{fancy}
\newcommand\macrocodecolor{\color{macrocode}}\definecolor{macrocode}{rgb}{0.18,0.00,0.45}
\newcommand\reflinkcolor{\color{reflink}}\definecolor{reflink}{rgb}{0.49,0.00,0.00}
\hypersetup{%
  pdftitle={The keycommand package},
  pdfsubject={An e-TeX package for more useful tools for LaTeX package writers},
  pdfauthor={Florent CHERVET},
  colorlinks,linkcolor=reflink,
  pdfstartview={FitH},
  pdfkeywords={tex, e-tex, latex, package, keys, keycommand, newcommand, keyval, kvsetkeys, programming},
  bookmarksopen=true,bookmarksopenlevel=2}
\begin{document}
   \DocInput{keycommand.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{673}
%
% \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         \~}
%
% \DoNotIndex{\begin,\CodelineIndex,\CodelineNumbered,\def,\DisableCrossrefs}
% \DoNotIndex{\DocInput,\documentclass,\EnableCrossrefs,\end,\GetFileInfo}
% \DoNotIndex{\NeedsTeXFormat,\OnlyDescription,\RecordChanges,\usepackage}
% \DoNotIndex{\ProvidesClass,\ProvidesPackage,\ProvidesFile,\RequirePackage}
% \DoNotIndex{\filename,\fileversion,\filedate,\let}
% \DoNotIndex{\@listctr,\@nameuse,\csname,\else,\endcsname,\expandafter}
% \DoNotIndex{\gdef,\global,\if,\item,\newcommand,\nobibliography}
% \DoNotIndex{\par,\providecommand,\relax,\renewcommand,\renewenvironment}
% \DoNotIndex{\stepcounter,\usecounter,\nocite,\fi}
% \DoNotIndex{\@fileswfalse,\@gobble,\@ifstar,\@unexpandable@protect}
% \DoNotIndex{\AtBeginDocument,\AtEndDocument,\begingroup,\endgroup}
% \DoNotIndex{\frenchspacing,\MessageBreak,\newif,\PackageWarningNoLine}
% \DoNotIndex{\protect,\string,\xdef,\ifx,\texttt,\@biblabel,\bibitem}
% \DoNotIndex{\z@,\wd,\wheremsg,\vrule,\voidb@x,\verb,\bibitem}
% \DoNotIndex{\FrameCommand,\MakeFramed,\FrameRestore,\hskip,\hfil,\hfill,\hsize,\hspace,\hss,\hbox,\hb@xt@,\endMakeFramed,\escapechar}
% \DoNotIndex{\do,\date,\if@tempswa,\@tempdima,\@tempboxa,\@tempswatrue,\@tempswafalse,\ifdefined,\ifhmode,\ifmmode,\cr}
% \DoNotIndex{\box,\author,\advance,\multiply,\Command,\outer,\next,\leavevmode,\kern,\title,\toks@,\trcg@where,\tt}
% \DoNotIndex{\the,\width,\star,\space,\section,\subsection,\textasteriskcentered,\textwidth}
% \DoNotIndex{\",\:,\@empty,\@for,\@gtempa,\@latex@error,\@namedef,\@nameuse,\@tempa,\@testopt,\@width,\\,\m@ne,\makeatletter,\makeatother}
% \DoNotIndex{\maketitle,\parindent,\setbox,\x,\kernel@ifnextchar}
% \DoNotIndex{\KVS@CommaComma,\KVS@CommaSpace,\KVS@EqualsSpace,\KVS@Equals,\KVS@Global,\KVS@SpaceEquals,\KVS@SpaceComma,\KVS@Comma}
%
% \title{The \xpackage{keycommand}\kern6pt package}
% \author{Florent Chervet \xemail{florent.chervet@free.fr}}
% \date{July 22, 2009}
%
% \MakeShortVerb{\+}
%
% \catcode`\§\active\def§{\par\nobreak\vskip-\parskip}
% \makeatletter
% \parindent\z@\parskip.4\baselineskip\topsep\parskip\partopsep\z@
% \g@addto@macro\macro@font{\macrocodecolor\let\AltMacroFont\macro@font}
% \def\smex{\leavevmode\hb@xt@2em{\hfil$\longrightarrow$\hfil}}
% \makeatother
%
% \maketitle
%
% \begin{abstract}\parindent0pt\noindent
%
% \xpackage{keycommand} provides an easy way to define commands or environments
% with optional keys. It provides \cs{newkeycommand} and its relative
% \cs{renewkeycommand}, \cs{newkeyenvironment}, \cs{renewkeyenvironment}
% and \cs{providekeycommand}.
%
% Moreover it is possible to define key-commands using +\def+, +\edef+, +\gdef+ or +\xdef+
% via the +\keycmd+ prefix.
%
% This package requires and is based on the package \xpackage{kvsetkeys} by Heiko Oberdiek.
%
% It is designed to work with \eTeX{} for the code uses the primives +\unexpanded+ and +\protected+.
%
% \end{abstract}\vskip-\baselineskip
% \makeatletter\let\@thefnmark\@empty
% \@footnotetext{This documentation is produced with the +DocStrip+ utility.
% \begin{tabbing}
% \qquad\=\smex\=To get the documentation, \= run (thrice):\quad\= \texttt{pdflatex keycommand.dtx} \\
% \>\smex\>To get the package, \> run:\>        \texttt{etex keycommand.dtx}
% \end{tabbing}}\makeatother
%
% \cftbeforesecskip=5pt plus2pt minus4pt
% \tableofcontents
% \tracingnone
%
% \def\B#1{\texttt{[}\meta{#1}\texttt{]}}
%
% \section{Introduction}
%
% \subsection{User interface}
%
% With \xpackage{keycommand} it becomes very easy to define commands with optional keys.
% Just say:
% \begin{tabbing}
% \qquad\=+\newkeycommand+\=+\CommandWithKeys[kOne=default,+...+][2]{%+\\
% \>\> definition with +\commandkey{kOne}+ etc. +#1+ and +#2}+
% \end{tabbing}
%
% As far as the keys are optional, it is not allowed to have another optional
% parameter in a key-command.
%
% \goodbreak
% \xpackage{keycommand} enables us to define key-environments as well, and provides:
% \begin{center}\begin{tabular}{rll}
% &+\newkeycommand+ & +\renewkeycommand+ \\
% &+\newkeyenvironment+ & +\renewkeyenvironment+ \\
% and:&+\providekeycommand+
% \end{tabular}\end{center}

% Moreover, if you need (or prefer) the syntax of +\def+ (or |\gdef|, |\edef|, |\xdef|)
% you shall refer to the section \nameref{subsec:keycmd} (in \nameref{Implementation}).
%
% \begin{declcs}{newkeycommand}\,\M{control sequence}\,\B{key-value list}\,\B{number of args}\,\M{definition}
% \end{declcs}
%
% \xpackage{keycommand} allow \LaTeX{} users to define commands with key in a easy way.
% Better is a small example than a long talking: let's define a command \cs{Rule} whose
% +width+, +thickness+ and +raise+ can be specified as keys. 
%
% With \xpackage{keycommand} we just have to say:§
% \begin{tabbing}\label{Rule}
% \,\=+\new+\=+keycommand\Rule[raise=.4ex,width=1em,thick=.4pt][1]{%+\\
% \>\>+\rule[\commandkey{raise}]{\commandkey{width}}{\commandkey{thick}}+\\
% \>\>+#1%+\\
% \>\>+\rule[\commandkey{raise}]{\commandkey{width}}{\commandkey{thick}}}+
% \end{tabbing}
%
% which defines the keys +width+, +thick+ and +raise+ with their default values (if not specified):
% +1em+, +.4pt+ and +.4ex+. Now \cs{Rule} can be used as follow:
% \begin{tabbing}
% \=1:\quad\=+\Rule[width=2em]{hello}+\hskip2cm\=\smex\,\=+width=2em,thick=.4pt,raise=.4ex+\\
% \>2:\>+\Rule[thick=1pt,width=2em]{hello}+\>\smex\>+width=2em,thick=1pt,raise=.4ex+\\
% \>3:\>+\Rule{hello}+\quad \>\smex\>+width=1em,thick=.4pt,raise=.4ex+\\
% \>4:\>+\Rule[thick=2pt,raise=1ex]{hello}+\>\smex\>+width=1em,thick=2pt,raise=1ex+\\
% \> \textit{et c\ae tera}.
% \end{tabbing}
%
% They will produce:
% \begin{tabbing}
% \=1:\quad\=\rule[.4ex]{2em}{.4pt}hello\rule[.4ex]{2em}{.4pt}\\
% \>2:\> \rule[.4ex]{2em}{1pt}hello\rule[.4ex]{2em}{1pt}\\
% \>3:\> \rule[.4ex]{1em}{.4pt}hello\rule[.4ex]{1em}{.4pt}\\
% \>4:\> \rule[1ex]{1em}{2pt}hello\rule[1ex]{1em}{2pt}
% \end{tabbing}
%
% \begin{declcs}{newkeyenvironment}\,\M{envir name}\,\B{key-values pairs}\,\B{number of args}\,\M{begin}\,\M{end}
% \end{declcs}
%
% In the same way, you may define environments with optional keys as follow:§
% \begin{tabbing}
% \qquad\=+\newkeyenvironment+\=+{EnvirWithKeys}[kOne=+default value,...+][n]+\\
% \>\>+{+ commands at begin +EnvirWithKeys }+ \\
% \>\>+{+ commands at end +EnvirWithKeys }+
% \end{tabbing}
%
% Where $n$ is the number of mandatory other arguments (\emph{ie} without keys), if any.
%
% A example of a key-environment is left in the file: +keycommand-example.tex+.
%
% \enlargethispage{\baselineskip}
% \subsection{Error messages}
%
% If you use the command +\Rule+ (defined in \ref{Rule}) with a key say: +height+
% that has not been declared at the definition of the key-command, you will get an
% error message like this:
% \begin{quote}\tt
% There was no key ``height''§
% in the keycommand \string\Rule!§
% see the definition of the keycommand.
% \end{quote}
%
% However, if you use +\commandkey{height}+ \textbf{in the definition} of +\Rule+ you
% will not have any error message: +\commandkey{height}+ will just be expanded into +\relax+
% at +\Rule+ expansion time.
%
% To be honest, when you redefine a key-command using |\renewkeycommand| or
% |\renewkeyenvironment| or |\keycmd\def| the keys defined before for the old command are undefined.
% This way you have the expected error message in all cases.
%
% \StopEventually{
% }
%
% \begin{center}\vskip6pt$\star$\hskip4em\lower12pt\hbox{$\star$}\hskip4em$\star$\vadjust{\vskip12pt}\end{center}
%
% \section{Implementation} \label{Implementation}
%
% \subsection{Identification}
%
% This package is intended to use with \LaTeX{} so we don't check if it is loaded twice.
%
%    \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}% LaTeX 2.09 can't be used (nor non-LaTeX)
   [2005/12/01]% LaTeX must be 2005/12/01 or younger (see kvsetkeys.dtx).
\ProvidesPackage{keycommand}
   [2009/07/22 an easy way to define commands with keys]
%    \end{macrocode}
%
% \subsection{Requirements}
%
% The package is based on \xpackage{kvsetkeys}. \xpackage{kvsetkeys} is more
% reliable than \xpackage{keyval} as far as spaces and bracket (groups) are concerned.
% Please refer to the \xpackage{kvsetkeys} documentation for more information.
%
% As long as we use \eTeX{} primitives in \xpackage{keycommand} we also load the
% \xpackage{etex} package in order to get an error message if \eTeX{} is not running.
%
%    \begin{macrocode}
\RequirePackage{etex,kvsetkeys}
%    \end{macrocode}
%
% \iffalse
% \subsection{Syntactical enhancement}
%
% \begin{macro}{\`name`}
% We define a shortcut for +\csname+...+\endcsname+ all along this package.
%    \begin{macrocode}
\let\kcmd@AtEnd=\`
\def\`#1`{\csname #1\endcsname}
%    \end{macrocode}
% \end{macro}
% \fi
%
% \subsection{Defining keys}
%
% \begin{macro}{\kcmd@keydef}
% To handle the case where the key-command was defined as +\global+, we have to
% define keys globally too. Therefore, we can't use the +\define@key+ macro of
% the \xpackage{keyval} package.
%
%    \begin{macrocode}
\def\kcmd@keydef#1#2#3#4#5{% #1=\global(eventually), #2=command, #3=family, #4=key, #5=def
   #1\expandafter\edef\csname kcmd@keys\string#2\endcsname{%
      \csname kcmd@keys\string#2\endcsname,#4}%
   #1\@namedef{KV@#3@#4@default\expandafter}\expandafter{%
      \csname KV@#3@#4\endcsname{#5}}%
   #1\@namedef{KV@#3@#4}##1}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\kcmd@definekey}
% In order to define keys, we will use the +\kv@parse+ macro (\xpackage{kvsetkeys}).
% Therefore, the only requirement is to define the \emph{processor}.
%
%    \begin{macrocode}
\def\kcmd@definekey#1#2#3#4#5{%
   \begingroup\edef\@tempa{\endgroup
      \unexpanded{\kcmd@keydef{#1}{#2}{#3}{#4}{#5}}{\def
            \expandafter\noexpand\csname #3@#4\endcsname{####1}}%
   }\@tempa}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kcmd@undefinekeys}
% Now in case we redefine a key-command, we would like the old keys (\emph{ie} the keys
% associated to the old definition of the command) to be cleared, undefined.
% That's the job of +\kcmd@undefinekeys+:
%
%    \begin{macrocode}
\def\kcmd@undefinekeys#1#2{%
   \@ifundefined{kcmd@keys\string#2}
      \relax
      {\expandafter\@for\expandafter\kcmp@temp
         \expandafter:\expandafter=\csname kcmd@keys\string#2\endcsname
      \do{#1\expandafter\let
            \csname KV@kcmd@\expandafter\@gobble\string #2@\kcmp@temp @default\endcsname
            \@undefined
         #1\expandafter\let
            \csname KV@kcmd@\expandafter\@gobble\string #2@\kcmp@temp\endcsname
            \@undefined}}%
   #1\@namedef{kcmd@keys\string#2}{\@gobble}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \cs{keycmd} prefix} \label{subsec:keycmd}
%
% |\keycmd| acts just like a (expandable) prefix for |\def| or |\edef|:
%
% The syntax is:§
% \begin{tabbing}
% %\qquad\= +\keycmd+\=\textcolor{red}{\textasteriskcentered} \hskip6cm \textcolor{red}{\textasteriskcentered}\,\= optional\\
% \>\>                 {(\color{red}+\protected|\long|\outer|\global+})\> optional \\
% \>\>                 (+\def|\gdef|\edef|\xdef+)\> required \\
% \>\>                 +\CommandWithKeys+ \> Name of the command to define \\
% \>\>                 +[key=value pairs]+ \> keys and default values \\
% \>\>                 \textcolor{red}{\emph{parameter string}} \> optional (same syntax as |\def|) \\
% \>\>                 +{+ \emph{replacement text} +}+ \> required
% \end{tabbing}
%
% Without the star form, +\long+ is assumed; but it can always be specified as +\long+ \textbf{after}
% +\keycmd+. Example:§
% \qquad+\keycmd\gdef\CommandWithKeys[kOne=defOne,kTwo=defTwo]#1#2{+...+}+
%
% \begin{macro}{\keycmd}
% First we have to read the prefixes, if any:
%    \begin{macrocode}
\DeclareRobustCommand\keycmd{\@star@or@long\kcmd@prefix}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kcmd@prefix}
% This macro reads the prefixes one after another (including the +\def+ word)
% and stores them in +\kcmd@prfx+. We open a group for all declarations will
% be local until the final definition of +\CommandWithKeys+.
%
%    \begin{macrocode}
\def\kcmd@prefix{\begingroup
   \let\kcmd@gbl\@empty
   \def\kcmd@prfx{\l@ngrel@x}%
   \futurelet\x\kcmd@pref@x}
\def\kcmd@pref@x{%
   \ifx\x\@sptoken         \let\next\kcmd@pref@x@space
   \else                   \let\next\kcmd@addto@prfx
      \ifx\x\long
      \else\ifx\x\outer
      \else\ifx\x\protected
      \else\ifx\x\global   \def\kcmd@gbl{\global}%
      \else
         \def\kcmd@pref@x@next{\expandafter\key@cmd\noexpand}%
         \ifx\x\def
         \else\ifx\x\edef
         \else\ifx\x\gdef  \def\kcmd@gbl{\global}%
         \else\ifx\x\xdef  \def\kcmd@gbl{\global}%
         \else \def\next{\kcmd@error{a \string\def\space
                     was expected after \string\keycmd.}}%
         \fi\fi\fi\fi
      \fi\fi\fi\fi
   \fi\next}
\begingroup\def\:{\kcmd@pref@x@space}
\expandafter\gdef\: {\futurelet\x\kcmd@pref@x}
\endgroup
\def\kcmd@addto@prfx#1{\expandafter\def\expandafter
   \kcmd@prfx\expandafter{\kcmd@prfx#1}\kcmd@pref@x@next}
\def\kcmd@pref@x@next{\futurelet\x\kcmd@pref@x}
\def\kcmd@error#1{\@latex@error{#1}\@ehd}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\key@cmd, \@keycmd}
% +\key@cmd+ will take the name of the command to be defined as its first argument
% and checks if there are keys-values placed between brackets just after. Then,
% +\@keycmd+ will check if the command is definable; if it is not, then the switch
% +\@tempswa+ is set to false: the definition is processed nevertheless with a basic +\def+,
% but the group (opened in +\kcmd@prefix+) is closed just after the assignment, canceling
% everything out.
%
%    \begin{macrocode}
\def\key@cmd#1{\@testopt{\expandafter\@keycmd\noexpand#1}{}}
\def\@keycmd#1[#2]{\@tempswafalse\expandafter
   \@rc@ifdefinable\noexpand#1{\@tempswatrue}%
   \if@tempswa
      \let#1=\relax
      \def\next{\kcmd@def#1{#2}}%
   \else \def\next{\afterassignment\endgroup
      \def\kcmd@notdefinable}%
   \fi\next}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kcmd@relaxify}
% We temporarily assign the value +\relax+ to some commands in order to avoid
% so many +\noexpand+ during the expanded definition of +\kcmd@def@+:
%    \begin{macrocode}
\def\kcmd@relaxify{%
   \let\commandkey\relax
   \let\kvsetkeys\relax
   \let\kv@parse\relax
   \let\@testopt\relax
   \let\kv@set@family@handler\relax
   \let\kcmd@undefinekeys\relax
   \let\kcmd@keyerr\relax
   \let\kcmd@definekey\relax
   \def\"##1"{\expandafter\noexpand\csname ##1\endcsname}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kcmd@def}
% +\kcmd@def+ will define the keys and the command itself:
%    \begin{macrocode}
\def\kcmd@def#1#2{% #1=\Command, #2=key-values
   \edef\kcmd@fam{kcmd@\expandafter\@gobble\string#1}%
   \kcmd@relaxify
   \edef\kcmd@def@##1{\endgroup
      \kv@set@family@handler{\kcmd@fam}{\kcmd@keyerr{#1}{####1}{####2}}%
      \kcmd@undefinekeys{\kcmd@gbl}{#1}%
      \kv@parse{##1}{\kcmd@definekey{\kcmd@gbl}{#1}{\kcmd@fam}}%
      \kcmd@gbl\protected\def#1{% entry point
         \protected\def\commandkey########1{\noexpand\csname\kcmd@fam @########1\endcsname}%
         \def\"kcmd\string#1"########1[########2]{%
               \kvsetkeys{\kcmd@fam}{########1,########2}%
               \"\string #1"}%
         \@testopt{\"kcmd\string#1"{##1}}{}}%
      \let\commandkey\relax
      \expandafter\expandafter\expandafter
         \expandafter\expandafter\expandafter
            \expandafter\kcmd@prfx\"\string#1"%
   }\kcmd@def@{#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kcmd@keyerr}
% +\kcmd@keyerr+ is the default handler for key-commands. It is called
% whenever the user wants to use a key that was not defined in the key-command:
%
%    \begin{macrocode}
\def\kcmd@keyerr#1#2#3{%
   \let\wheremsg\@empty
   \ifdefined\trcg@where\trcg@where{#1}\fi
   \@latex@error{There was no key "#2" \MessageBreak
      in the keycommand \string#1!\MessageBreak
      see the definition of the keycommand (or environment)\wheremsg}\@ehd}
%    \end{macrocode}
% \end{macro}
%
% \subsection{new key-commands}
%
% \begin{macro}{\newkeycommand}
% The +\expandafter+...+\noexpand+ trick is there in case the command to (re-)define
% had been defined as +\outer+ before...
%    \begin{macrocode}
\DeclareRobustCommand\newkeycommand{\@star@or@long
   \expandafter\new@keycommand\noexpand}
\DeclareRobustCommand\renewkeycommand{\@star@or@long
   \expandafter\renew@keycommand\noexpand}
\DeclareRobustCommand\providekeycommand{\@star@or@long
   \expandafter\provide@keycommand\noexpand}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\def\new@keycommand#1{\@testopt{\expandafter\@newkeycommand\noexpand#1}{}}
\def\@newkeycommand#1[#2]{\begingroup
   \@tempswafalse\expandafter
   \@ifdefinable\noexpand#1{\@tempswatrue}%
   \if@tempswa
      \let#1=\relax
      \let\kcmd@gbl\@empty
      \def\kcmd@prfx##1{\unexpanded{\@testopt{\@argdef{##1}}0}}%
      \def\next{\kcmd@def#1{#2}}%
   \else \def\next{\afterassignment\endgroup
      \def\kcmd@notdefinable}%
   \fi\next}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\renew@keycommand#1{\begingroup
   \escapechar\m@ne\edef\@gtempa{{\string#1}}%
   \expandafter\@ifundefined\@gtempa
      {\endgroup\@latex@error{\noexpand#1undefined}\@ehc}
      \endgroup
   \let\@ifdefinable\@rc@ifdefinable
   \expandafter\new@keycommand\noexpand#1}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\provide@keycommand#1{\begingroup
   \escapechar\m@ne\edef\@gtempa{{\string#1}}%
   \expandafter\@ifundefined\@gtempa
      {\endgroup\new@keycommand#1}
      {\endgroup\let\kcmd@notdefinable\noexpand
      \renew@keycommand\kcmd@notdefinable}}
%    \end{macrocode}
%
% \subsection{new key-environments}
%
%    \begin{macrocode}
\DeclareRobustCommand\newkeyenvironment{\@star@or@long\new@keyenvironment}
\DeclareRobustCommand\renewkeyenvironment{\@star@or@long\renew@keyenvironment}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\new@keyenvironment#1{\@testopt{\@newkeyenva{#1}}{}}
\def\@newkeyenva#1[#2]{%
   \kernel@ifnextchar [{\@newkeyenvb{#1}[{#2}]}{\@newkeyenv{#1}{[{#2}][0]}}}
\def\@newkeyenvb#1[#2][#3]{\@newkeyenv{#1}{[{#2}][#3]}}
\def\@newkeyenv#1#2#3#4{%
   \@ifundefined{#1}%
      {\expandafter\let\csname #1\expandafter\endcsname
            \csname end#1\endcsname}%
      \relax
   \expandafter\@newkeycommand
      \csname #1\endcsname#2{#3}%
   \l@ngrel@x\expandafter\def\csname end#1\endcsname{#4}}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\renew@keyenvironment#1{%
  \@ifundefined{#1}%
     {\@latex@error{Environment #1 undefined}\@ehc
     }\relax
  \expandafter\let\csname#1\endcsname\relax
  \expandafter\let\csname\expandafter\string\csname #1\endcsname\endcsname\relax
  \expandafter\let\csname end#1\endcsname\relax
  \new@keyenvironment{#1}}
%    \end{macrocode}
%
% \subsection{\xpackage{kvsetkeys} correction}
%
% |\kv@normalize| inserts an unwilling blank space due to a small error in the code.
% Mr Oberdiek has been asked for correction...
%
%    \begin{macrocode}
\def\kv@normalize#1{%
  \begingroup
    \toks@{,#1,}%
    \KVS@Comma
    \KVS@SpaceComma{ }%
    \KVS@CommaSpace
    \KVS@CommaComma
    \KVS@Equals
    \KVS@SpaceEquals{ }%
    \KVS@EqualsSpace% <original code>\KVS@EqualsSpace{ }</original code>
    \xdef\KVS@Global{\the\toks@}%
  \endgroup
  \let\kv@list\KVS@Global
}
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \section{Example}
%
%    \begin{macrocode}
%<*example>
\ProvidesFile{keycommand-example}
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[american]{babel}
\usepackage{keycommand}
\usepackage{framed}
%
\makeatletter
\parindent\z@
\newkeycommand\Rule[raise=.4ex,width=1em,thick=.4pt][1]{%
   \rule[\commandkey{raise}]{\commandkey{width}}{\commandkey{thick}}%
   #1%
   \rule[\commandkey{raise}]{\commandkey{width}}{\commandkey{thick}}}

\newkeycommand\charleads[sep=1][2]{%
   \ifhmode\else\leavevmode\fi\setbox\@tempboxa\hbox{#2}\@tempdima=1.584\wd\@tempboxa%
   \cleaders\hb@xt@\commandkey{sep}\@tempdima{\hss\box\@tempboxa\hss}#1%
   \setbox\@tempboxa\box\voidb@x}
\newcommand\charfill[1][]{\charleads[{#1}]{\hfill\kern\z@}}
\newcommand\charfil[1][]{\charleads[{#1}]{\hfil\kern\z@}}
%
\newkeyenvironment{dblruled}[first=.4pt,second=.4pt,sep=1pt,left=\z@]{%
   \def\FrameCommand{%
      \vrule\@width\commandkey{first}%
      \hskip\commandkey{sep}
      \vrule\@width\commandkey{second}%
      \hspace{\commandkey{left}}}%
   \parindent\z@
   \MakeFramed {\advance\hsize-\width \FrameRestore}}
   {\endMakeFramed}
%
\makeatother
\begin{document}
\title{This is {\tt keycommand-example.tex}}
\author{Florent Chervet}
\date{July 22, 2009}
\maketitle

\section{Example of a keycommand : \texttt{\string\Rule}}

\begin{tabular*}\textwidth{rl}
\verb+\Rule[width=2em]{hello}+:&\Rule[width=2em]{hello}\cr
\verb+\Rule[thick=1pt,width=2em]{hello}+:&\Rule[thick=1pt,width=2em]{hello}\cr
\verb+\Rule{hello}+:&\Rule{hello}\cr
\verb+\Rule[thick=1pt,raise=1ex]{hello}+:&\Rule[thick=1pt,raise=1ex]{hello}
\end{tabular*}

\section{Example of a keycommand : \texttt{\string\charfill}}

\begin{tabular*}\textwidth{rp{.4\textwidth}}
\verb+\charfill{$\star$}+: & \charfill{$\star$}\cr
\verb+\charfill[sep=2]{$\star$}+: & \charfill[sep=2]{$\star$} \\
\verb+\charfill[sep=.7]{\textasteriskcentered}+: & \charfill[sep=.7]{\textasteriskcentered}
\end{tabular*}


\section{Example of a keyenvironment : \texttt{dblruled}}

\verb+\begin{dblruled}+\par
\verb+   test for dblruled key-environment\par+\par
\verb+   test for dblruled key-environment\par+\par
\verb+   test for dblruled key-environment+\par
\verb+\end{dblruled}+

\begin{dblruled}
 test for dblruled key-environment\par
 test for dblruled key-environment\par
 test for dblruled key-environment
\end{dblruled}


\verb+\begin{dblruled}[first=4pt,sep=2pt,second=.6pt,left=.2em]+\par
\verb+   test for dblruled key-environment\par+\par
\verb+   test for dblruled key-environment\par+\par
\verb+   test for dblruled key-environment+\par
\verb+\end{dblruled}+

\begin{dblruled}[first=4pt,sep=2pt,second=.6pt,left=.2em]
 test for dblruled key-environment\par
 test for dblruled key-environment\par
 test for dblruled key-environment
\end{dblruled}


\end{document}
%</example>
%    \end{macrocode}
%
% \begin{thebibliography}{9}
%
% \bibitem{kvsetkeys}
%   Heiko Oberdiek:
%   \textit{The \xpackage{kvsetkeys} package};
%   2007/09/29 v1.3;
%   \CTAN{macros/latex/contrib/oberdiek/kvsetkeys.dtx}.
%
% \bibitem{keyval}
%   David Carlisle:
%   \textit{The \xpackage{keyval} package};
%   1999/03/16 v1.13;
%   \CTAN{macros/latex/required/graphics/keyval.dtx}.
%
% \end{thebibliography}
%
% \PrintIndex
%
% \label{LastPage}
% \Finale