Debian Codegraph @

texmf-dist/doc/latex/withargs/withargs.tex

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \iffalse %%%%
2 % %
3 % Copyright (c) 2016 - Michiel Helvensteijn (www.mhelvens.net) %
4 % %
5 % This work may be distributed and/or modified under the conditions %
6 % of the LaTeX Project Public License, either version 1.3 of this %
7 % license or (at your option) any later version. The latest version %
8 % of this license is in http://www.latex-project.org/lppl.txt %
9 % and version 1.3 or later is part of all distributions of LaTeX %
10 % version 2005/12/01 or later. %
11 % %
12 % This work has the LPPL maintenance status `maintained'. %
13 % %
14 % The Current Maintainer of this work is Michiel Helvensteijn. %
15 % %
16 % This work consists of the files withargs.tex and withargs.sty. %
17 % %
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \fi %%%%
19
20 \documentclass[a4paper]{withargs-packagedoc}
21
22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23 % Setup %
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25
26 \moretexcs{
27 withargs,foo,bar,bas,expectedResult,
28 actualResult,MyPost,A,uniquecsname,NewPost,
29 cs_generate_variant:Nn,
30 withargs:nn,withargs:cn
31 }
32
33 % Ugly hack, because of the unfortunate deprecation of 'n' to 'c' conversion,
34 % plus the fact that kernel errors, even non-fatal ones, cannot be redirected.
35 % Will think of a better solution at some point, but not now.
36 \ExplSyntaxOn
37 \let\__withargs_docs_old_kernel_msg_error:nnnnnn\__kernel_msg_error:nnnnnn
38 \def\__kernel_msg_error:nnnnnn#1#2{
39 \str_if_eq:nnTF { #1/#2 } { kernel/deprecated-variant }{
40 \__kernel_msg_warning:nnnnnn{#1}{#2}
41 }{
42 \__withargs_docs_old_kernel_msg_error:nnnnnn{#1}{#2}
43 }
44 }
45 \ExplSyntaxOff
46
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 % Global Changes %
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50
51 \changes{0.0.1}{2013/10/01}
52 {initial version}
53
54 \changes{0.0.2}{2013/10/11}
55 {renamed package to \texttt{withargs}}
56
57 \changes{0.1.0}{2015/09/07}
58 {adjusted code to new version of expl3}
59
60 \changes{0.2.0}{2016/12/19}
61 {adjusted code to new version of expl3}
62
63 \changes{0.3.1}{2019/11/04}
64 {adjusted code to new version of expl3}
65
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 \begin{document} %
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69
70 \maketitle
71
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 \section{Introduction} %
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
76 An example is worth a thousand words:
77
78 \begin{latex-example-show}
79 \withargs [!] [Hello] [world] { #2 #3#1 }
80 \end{latex-example-show}
81
82 A quick explanation: we're passing three pieces of \LaTeX\ code in the form
83 of optional arguments to the final argument, which forms the output. We're
84 defining a new anonymous macro and invoking it right away.
85 Up to seven optional arguments are supported.
86
87
88
89 \subsection{Why is this useful?} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91 One of the main use-cases for this package is to insert the expanded
92 result of a computation into the middle of a big
93 blob of code that \emph{shouldn't} be expanded. This is possible because
94 argument substitution bypasses the expansion process:
95
96 \begin{latex-example-show}
97 \def \expectedResult {\bar}
98 \def \actualResult {\bas}
99 \withargs (oo) [\actualResult] [\expectedResult] {
100 \texttt{\detokenize{
101 The \foo variable resulted in `#1' instead of `#2'!
102 }}
103 }
104 \end{latex-example-show}
105
106 `|(oo)|' indicates that both arguments should be expanded `once'.
107 Any optional \LaTeX3 style argument specification between parentheses
108 may be specified to control expansion.
109
110 \needspace{10\baselineskip}
111 You can define your own commands accepting `templates'
112 with \TeX-style parameters:
113
114 \begin{latex-example-show}
115 \newcommand{\NewPost}[4]{
116 % #1: author % #2: title
117 % #3: content % #4: template
118 \withargs (xnn) [#1] [#2] [#3] {#4}
119 }
120
121 \def\MyPost{ \NewPost{Author}{Title}
122 {Boy, do I have some important stuff to say!} }
123
124 \begin{tabular}{p{3.5cm}p{3.5cm}p{3.5cm}}
125 \MyPost{\textbf{#2\hfill#1}\vskip1mm\hrule\vskip1mm\textit{#3}} &
126 \MyPost{\fbox{#1: ``#2''}\par#3\vskip1mm\hrule} &
127 \MyPost{#2: #3\hfill\textit{(#1)}}
128 \end{tabular}
129 \end{latex-example-show}
130
131 Generally, if speed is not a concern, |\withargs| can be used
132 to make \LaTeX\ code more readable in a variety of situations.
133
134
135
136 %\vskip-.4\baselineskip
137 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 \section{Document Level Commands} %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %\vskip-.5\baselineskip
141
142 \def\mystrut{\rule{0pt}{4mm}}
143
144 \describemacro{\withargs}{
145 \texttt{\bfseries(}%
146 \(\overbrace{\mystrut\hbox{\meta{argument specification}}}^x\)%
147 \texttt{\bfseries)}
148 \(\overbrace{\mystrut\hbox{\oarg{1} \oarg{2} \oarg{3} \oarg{4} \oarg{5} \oarg{6} \oarg{7}}}^x\)
149 \marg{body}
150 }
151
152 Leaves \meta{body} in the output with |#1|\ldots|#7| replaced
153 by optional arguments \meta{1}\ldots\!\meta{7}.
154
155 An optional \LaTeX3 style \meta{argument specification} between parentheses
156 can be provided, in which case the arguments undergo expansion as specified
157 before being placed in the \meta{body}.
158 %%
159 Here is an example demonstrating the possible expansion types:
160
161 \begin{latex-example-show}
162 \def\foo{\bar} \def\s{s} \def\bar{ba\s} \def\bas{FOO-BAR-BAS}
163
164 \withargs (n o f x c v )
165 [\foo][\foo][\foo][\foo][\foo][\foo] {
166 \begin{tabular}{llll}
167 n: & \texttt{\detokenize{#1}} & % no expansion
168 o: & \texttt{\detokenize{#2}} \\ % expand once
169 f: & \texttt{\detokenize{#3}} & % expand until unexpandable
170 x: & \texttt{\detokenize{#4}} \\ % exhaustive expansion
171 c: & \texttt{\detokenize{#5}} & % \csname conversion
172 v: & \texttt{\detokenize{#6}} % `c', then `o'
173 \end{tabular}
174 }
175 \end{latex-example-show}
176
177 Note that all spaces in the \meta{argument specification} are ignored.
178 For details about \LaTeX3 argument specifications, have a look at the
179 following documentation:
180
181 \begin{center}
182 \url{http://www.ctan.org/pkg/expl3}
183 \end{center}
184
185
186 \describemacro{\uniquecsname}{}\vskip-2\baselineskip
187
188 Perhaps a bit misleading, |\uniquecsname| is not actually defined as a command,
189 but |\withargs| recognizes it as a special marker. If an optional |\withargs|
190 argument consists entirely of |\uniquecsname|, it is replaced by a
191 command sequence name which is guaranteed to be unique\ldots unless
192 you look at the source code (Section~\ref{sec:implementation})
193 and intentionally replicate the naming scheme.
194
195 \begin{latex-example-show}
196 \withargs (ccc) [\uniquecsname][\uniquecsname][\uniquecsname] {
197 \def#1{A} \let\A#1
198 \def#2{B}
199 \def#3{C}
200 #3#2#1%
201 }%
202 (\A)
203 \end{latex-example-show}
204
205 \vskip5mm
206
207
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 \section{\LaTeX3 Functions} %
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211
212 The \LaTeX3 functions underlying the functionality of |\withargs| are
213 also available as is.
214
215 \describeanymacro{\texttt{\textbackslash withargs:\smash{\(\overbrace{\mystrut\hbox{nnnnnnnn}\!}^x\,\)}n}}{
216 \(\overbrace{\mystrut\hbox{\marg{1} \marg{2} \marg{3} \marg{4} \marg{5} \marg{6} \marg{7} \marg{8}}}^x\)
217 \marg{body}
218 }
219
220 These functions do pretty much the same thing as the main |\withargs| macro,
221 except that the argument specification is embedded in the function name
222 as per \LaTeX3 coding convention, so a parameter slot is freed up for
223 custom use. They are slightly faster than |\withargs|, as there is no
224 need to gather optional arguments or do any error checking.
225 The downside is that the |\uniquecsname| marker doesn't work for these.
226
227 To use a specific expansion scheme, you have to define a variant:
228
229 \begin{latex-example-show}
230 \ExplSyntaxOn
231 \cs_generate_variant:Nn \withargs:nn {cn}
232 \withargs:cn {LaTeX} {#1}
233 \ExplSyntaxOff
234 \end{latex-example-show}
235
236 Read the \LaTeX3 documentation for details.
237
238
239
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 \end{document} %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%