summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/conv-xkv/examples/convert2xkeyval.tex
blob: 04a2cd1d9c545a89bb9af7269b19587a94ab194d (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
\documentclass{article}
\usepackage{conv-xkv}
\usepackage{fancyvrb}

% Use ordinary askii characters to declare a delimiter, usually
% the delimiter is some combination of special characters: !@#:%^&*()_+=
\DeclareDelimiter{arrow}{->}
% The colon delimiter is already defined, and need not be declared
%\DeclareDelimiter{colon}{:}

% formatting cmd
\def\ameta#1{\ensuremath{\langle\textit{\texttt{#1}}\rangle}}

\makeatletter
\define@key{dps}{fname}[]{\def\fname{#1}}
\define@key{dps}{mname}[]{\def\mname{#1}}
\define@key{dps}{lname}[]{\def\lname{#1}}
\define@choicekey{dps}{favorite}{AeB, AeB Pro, conv-xkv}[AeB]{\def\favoritepkg{#1}}
\define@key{kaf}{home}[]{\def\myhome{#1}}
\makeatother

% Process keys using the arrow delimiter
\def\setName#1{\cxkvsetkeys[arrow]{dps,kaf}{#1}}
% we use the default delimiter, colon (:)
\def\setOtherName#1{\cxkvsetkeys{dps}{#1}}
% we define the dps family for xkeyval
\def\setStandName#1{\setkeys{dps}{#1}}

\let\pkg\textsf
\newcommand{\cs}[1]{\texttt{\char`\\#1}}
\def\newtopic{\par\medskip}

\parindent0pt

\begin{document}

In the preamble, we declare \verb~\DeclareDelimiter{arrow}{->}~.

\newtopic
The \pkg{conv-xkv} package converts key-values with alternate key-value delimiter
to a form the \pkg{xkeyval} package expects them to be in.

\setName {
    fname-> D.,
    mname-> P.,
    lname-> Story,
    favorite-> conv-xkv,
    home-> {Niceville, FL}
}
\begin{Verbatim}[xleftmargin=\leftmargini,fontsize=\small]
\def\setName#1{\cxkvsetkeys[arrow]{dps,kaf}{#1}}
\setName {
    fname-> D.,
    mname-> P.,
    lname-> Story,
    favorite-> conv-xkv,
    home-> {Niceville, FL}
}
\end{Verbatim}
The script above converts the notation \texttt{\ameta{key}\usekvdelim{arrow}\ameta{value}} to
the notation of \pkg{xkeyval}, \texttt{\ameta{key}=\ameta{value}}. It is then passed to the
\cs{setkeys} command of \pkg{xkeyval}.

\newtopic\textbf{Results:}
My name is {\fname} {\mname} {\lname} and my favorite {\LaTeX} package is
\textsf{\favoritepkg},  my home is \myhome.

\setOtherName {
    fname: D.,
    mname: Paul,
    lname: Story,
    favorite      % use default
}

\newtopic Now we use the colon (:) delimiter:
\begin{Verbatim}[xleftmargin=\leftmargini,fontsize=\small]
\def\setOtherName#1{\cxkvsetkeys{dps}{#1}}
\setOtherName {
    fname: D.,
    mname: Paul,
    lname: Story,
    favorite     % use default
}
\end{Verbatim}
The script above converts the colon notation \texttt{\ameta{key}\usekvdelim{colon}\ameta{value}} to
the notation of \pkg{xkeyval}, \texttt{\ameta{key}=\ameta{value}}. This enables the implementation of
\pkg{xkeyval} in packages that want to use object-property notation. Other variations are
possible.

\newtopic\textbf{Results:}
My name is {\fname} {\mname} {\lname} and my favorite {\LaTeX} package is
\textsf{\favoritepkg}.

\newtopic
Now here are the same results using the \pkg{xkeyval} package with the key-value delimiter
\texttt{\ameta{key}=\ameta{value}} to verify that nothing has been disturbed.

\setStandName {
    fname= Don,
    mname= Paulo,
    lname= Story,
    favorite
}
\begin{Verbatim}[xleftmargin=\leftmargini,fontsize=\small]
\def\setStandName#1{\setkeys{dps}{#1}}
\setStandName {
    fname=Don,
    mname=Paulo,
    lname=Story,
    favorite
}
\end{Verbatim}
\textbf{Results:}
My name is {\fname} {\mname} {\lname} and my favorite {\LaTeX} package is
\textsf{\favoritepkg}.


\end{document}