Debian Codegraph @

texmf-dist/source/latex/koma-script/scrkernel-variables.dtx

raw
1 % \iffalse meta-comment
2 % ======================================================================
3 % scrkernel-variables.dtx
4 % Copyright (c) Markus Kohm, 2006-2023
5 %
6 % This file is part of the LaTeX2e KOMA-Script bundle.
7 %
8 % This work may be distributed and/or modified under the conditions of
9 % the LaTeX Project Public License, version 1.3c of the license.
10 % The latest version of this license is in
11 % http://www.latex-project.org/lppl.txt
12 % and version 1.3c or later is part of all distributions of LaTeX
13 % version 2005/12/01 or later and of this work.
14 %
15 % This work has the LPPL maintenance status "author-maintained".
16 %
17 % The Current Maintainer and author of this work is Markus Kohm.
18 %
19 % This work consists of all files listed in MANIFEST.md.
20 % ======================================================================
21 %%% From File: $Id: scrkernel-variables.dtx 4151 2025-06-02 11:34:40Z kohm $
22 %<option>%%% (run: option)
23 %<body>%%% (run: body)
24 %<*dtx>
25 \ifx\ProvidesFile\undefined\def\ProvidesFile#1[#2]{}\fi
26 \begingroup
27 \def\filedate$#1: #2-#3-#4 #5${\def\filedate{#2/#3/#4}}
28 \filedate$Date: 2025-06-02 13:34:40 +0200 (Mo, 02. Jun 2025) $
29 \def\filerevision$#1: #2 ${\def\filerevision{r#2}}
30 \filerevision$Revision: 4151 $
31 \xdef\fileinfo{\filedate\space\filerevision\space}
32 \edef\reserved@a{%
33 \noexpand\endgroup
34 \noexpand\ProvidesFile{scrkernel-variables.dtx}%
35 [\filedate\space\filerevision\space
36 KOMA-Script source(koma variables)]
37 }%
38 \reserved@a
39 \documentclass[USenglish]{koma-script-source-doc}
40 \usepackage{babel}
41 \setcounter{StandardModuleDepth}{2}
42 \begin{document}
43 \DocInput{scrkernel-variables.dtx}
44 \end{document}
45 %</dtx>
46 % \fi
47 %
48 % \changes{v2.95}{2006/03/20}{new by splitting \file{scrclass.dtx}}
49 % \changes{v3.36}{2022/02/15}{switch over from \cls*{scrdoc} to
50 % \cls*{koma-script-source-doc}}
51 % \changes{v3.36}{2022/02/15}{whole implementation documentation in English}
52 % \changes{v3.40}{2023/04/17}{guide names changed}
53 %
54 % \GetFileInfo{scrkernel-variables.dtx}
55 % \title{Using Variables in
56 % \href{https://komascript.de}{\KOMAScript} Letter Classes and Package}
57 % \author{\href{mailto:[email protected]}{Markus Kohm}}
58 % \date{Revision \fileversion{} of \filedate}
59 % \maketitle
60 % \begin{abstract}
61 % Letters have several variables like the name and address of the sender and
62 % the addressee, for fields like customer ids phone numbers, email addresses
63 % etc. Depending on the purpose of a letter or a corporate identity much
64 % more field and so variables are possible. For all of them
65 % \file{scrkernel-variables.dtx} provides a general mechanism and user
66 % interface.
67 % \end{abstract}
68 % \tableofcontents
69 %
70 % \section{User Manual}
71 %
72 % You can find the user documentation the commands implemented here in the
73 % \KOMAScript{} manual, either the German \file{scrguide-de.pdf} or the
74 % English \file{scrguide-en.pdf}.
75 %
76 % \DescribeCommand{\Ifkomavarxempty}
77 % Similar to \cs{Ifkomavarempty} but the variable or description will be
78 % expanded before testing.
79 %
80 % \MaybeStop{\PrintIndex}
81 %
82 % \section{Implementation of Variables}
83 %
84 % \begin{macrocode}
85 %<*letter>
86 % \end{macrocode}
87 %
88 % \subsection{Options}
89 % The mechanism does not depend on options.
90 %
91 %
92 % \subsection{Commands and macros for variables}
93 %
94 % \begin{command}{newkomavar}
95 % \changes{v2.8q}{2002/01/14}{added}
96 % \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced \cs{kernel@ifstar} by}
97 % \changes{v3.42}{2023/08/25}{new implementation using \cs{NewDocumentCommand}}
98 % We have two kinds of fields and so variables:
99 % \begin{itemize}
100 % \item to be printed in the reference line,
101 % \item for other purpose, but not printed in the reference line.
102 % \end{itemize}
103 % But it is also possible to add variables later to (or even remove them from
104 % the reference line. But the star variant \cs{newkomavar*} automatically adds
105 % a variable to (the end of) the reference line. Both \cs{newkomavar} and
106 % \cs{newkomavar*} have one optional argument, the \meta{description}. It is
107 % intended to be printed. In opposite to this the first mandatory argument is
108 % the \meta{name} of the variable. It is not indented to be printed but used
109 % for access only. Only \emph{command sequence} characters are allowed
110 % here. It is recommended to use only letters for the \meta{name}. Internally
111 % macro \cs{scr@\meta{name}@name} and \cs{scr@\meta{name}@var} are
112 % defined. The second one is initialized to be empty.
113 % \begin{macrocode}
114 %<*body>
115 \NewDocumentCommand{\newkomavar}{som}{%
116 \@ifundefined{scr@#3@name}{%
117 \@ifundefined{scr@#3@var}{%
118 \IfValueT{#2}{\@namedef{scr@#3@name}{#2}}%
119 \@namedef{scr@#3@var}{}%
120 \IfBooleanT{#1}{\addtoreffields{#3}}%
121 }{%
122 %<class> \ClassError{scrlttr2}
123 %<package> \PackageError{scrletter}
124 {%
125 This should never happen%
126 }{%
127 The description of the KOMA-Script variable `#3'\MessageBreak
128 is undefined, but the name of the variable is
129 defined.\MessageBreak
130 This should never happen. So someone crashs me!%
131 }%
132 }%
133 }{%
134 %<class> \ClassError{scrlttr2}
135 %<package> \PackageError{scrletter}
136 {%
137 Variable `#3' already defined%
138 }{%
139 I'll ignore this command, if you'll continue.%
140 }%
141 }%
142 }
143 % \end{macrocode}
144 % \begin{macro}{\@newkomavar}
145 % \changes{v3.42}{2023/08/25}{removed}
146 % \end{macro}
147 % \end{command}
148 %
149 % \begin{command}{\setkomavar}
150 % \changes{v2.8q}{2002/01/14}{added}
151 % \changes{v3.08}{2010/10/28}{\cs{scr@\emph{Name}@postsetname} and
152 % \cs{scr@\emph{Name}@postsetvar} added}
153 % \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
154 % \changes{v3.20}{2016/04/12}{\cs{@ifnextchar} replaced by
155 % \cs{kernel@ifnextchar}}
156 % \changes{v3.42}{2024/04/24}{new implementation using
157 % \cs{NewDocumentCommand}}
158 % \changes{v3.43}{2024/10/23}{add missing call of \cs{scr@\emph{name}@postsetname}
159 % and \cs{scr@\meta{name}@postsetvar}}
160 % \changes{v3.44}{2025/06/02}{fix of error message macro}
161 % After defining a new variable we also need a command to set the
162 % \meta{value}. This is the second mandatory argument (\texttt{\#3}) of
163 % \cs{setkomavar}. The first mandatory argument (\texttt{\#2}) once again is
164 % the \meta{name}. The \meta{description} can be set with the optional first
165 % argument (\texttt{\#1}). But for the case only the \meta{description}
166 % should be changed but not the \meta{value} there is an additional star
167 % variant \cs{setkomavar*}. This has only two mandatory argument: \meta{name}
168 % (\texttt{\#1}) and \meta{description} (\texttt{\#2}). Both also executes two
169 % hooks \cs{scr@\meta{name}@postsetname} and \cs{scr@\meta{name}@postsetvar}
170 % after setting the \meta{description} resp. the \meta{value}. So package
171 % authors can define additional actions for the case a variable changes.
172 % \begin{macrocode}
173 \NewDocumentCommand{\setkomavar}{smo+m}{%
174 \Ifkomavar{#2}{%
175 \IfValueT{#3}{\@namedef{scr@#2@name}{#3}}%
176 \@namedef{scr@#2@\IfBooleanTF{#1}{name}{var}}{#4}%
177 \csname scr@#2@postset\IfBooleanTF{#1}{name}{var}\endcsname
178 }{%
179 \@komavar@err{set}{#2}%
180 }%
181 }
182 % \end{macrocode}
183 % \begin{macro}{\@setkomavar,\@@setkomavar,\@setkomaname}
184 % \changes{v2.8q}{2002/01/14}{added}
185 % \changes{v3.42}{2023/08/25}{removed}
186 % \end{macro}^^A \@setkomavar, \@@setkomavar, \@setkomaname
187 % \end{command}^^A \setkomavar*,\setkomavar
188 %
189 % \begin{macro}{\@komavar@err}
190 % \changes{v2.8q}{2002/01/14}{added}
191 % Error message for using or setting (\texttt{\#1}) unknown variables
192 % (\texttt{\#2}), used by \cs{setkomavar} or \cs{usekomavar}.
193 % \begin{macrocode}
194 \newcommand*{\@komavar@err}[2]{%
195 %<class> \ClassError{scrlttr2}
196 %<package> \PackageError{scrletter}
197 {%
198 KOMA-Script variable not defined%
199 }{%
200 You've tried to #1 the not defined KOMA-Script variable
201 `#2'.\MessageBreak
202 You have to define the variable using \string\newkomavar\space
203 before\MessageBreak
204 you do this.%
205 }%
206 }
207 % \end{macrocode}
208 % \end{macro}
209 %
210 % \begin{command}{\usekomavar}
211 % \changes{v2.8q}{2002/01/14}{added}
212 % \changes{v2.9i}{2002/09/04}{robust}
213 % \changes{v2.9i}{2002/09/04}{optional argument}
214 % \changes{v2.9j}{2002/09/18}{\cs{@firstofone} as default for the optional
215 % argument}
216 % \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
217 % \changes{v3.42}{2023/08/25}{new implementation using \cs{NewDocumentCommand}}
218 % \changes{v3.44}{2025/06/02}{fix of error message macro}
219 % User command to get either the \meta{description} (star variant) or the
220 % \meta{value} (non-star variant) of a variable. Once again the first
221 % mandatory argument (\texttt{\#2}) is the \meta{name} of the value. Both also
222 % have an optional first argument (\texttt{\#1}) \meta{code}. The \meta{code}
223 % is added before the \meta{description} or \meta{value} in the output stream
224 % and these are used as argument. You use it, e.g., to define a command with
225 % the content of \meta{description} or \meta{value} or change the formatting.
226 % \begin{macrocode}
227 \NewDocumentCommand{\usekomavar}{sO{\@firstofone}m}{%
228 \Ifkomavar{#3}{%
229 \IfBooleanTF{#1}{%
230 #2{\@nameuse{scr@#3@name}}%
231 }{%
232 #2{\@nameuse{scr@#3@var}}%
233 }%
234 }{%
235 \@komavar@err{use}{#3}%
236 }%
237 }
238 % \end{macrocode}
239 % \begin{macro}{\@usekomavar,\@usekomaname}
240 % \changes{v2.8q}{2002/01/14}{added}
241 % \changes{v3.42}{2023/08/25}{removed}
242 % \end{macro}^^A \@usekomavar,\@usekomaname
243 % \end{command}
244 %
245 % \begin{command}{\ifkomavar,\Ifkomavar}
246 % \changes{v3.03}{2009/03/04}{added}
247 % \changes{v3.28}{2019/11/15}{\cs{ifkomavar} renamed to {Ifkomavar}}
248 % \cs{Ifkomavar} tests if the variable with the \meta{name} of \texttt{\#1} is
249 % defined and execute the \meta{code} of \texttt{\#2} and otherwise the
250 % \meta{code} of \texttt{\#3}. The old command \cs{ifkomavar} is deprecated
251 % and should not be used any longer. This incompatible change has been done,
252 % because of a new recommendation of The \LaTeX{} Team (who does break its own
253 % recommendation, e.g., with \cs{ifToplevel}, \cs{ifthenelse} \dots).
254 % \begin{macrocode}
255 \newcommand*{\ifkomavar}{%
256 %<package> \PackageWarning{scrletter}{%
257 %<class> \ClassWarning{\KOMAClassName}{%
258 Usage of deprecated command `\string\ifkomavar'.\MessageBreak
259 The command has been renamed to because of a\MessageBreak
260 recommendation of LaTeX3 team members.\MessageBreak
261 Please replace `\string\ifkomavar' by `\string\Ifkomavar'%
262 }%
263 \Ifkomavar
264 }
265 \DeclareRobustCommand*{\Ifkomavar}[1]{%
266 \scr@ifundefinedorrelax{scr@#1@var}{%
267 \expandafter\@secondoftwo
268 }{%
269 \expandafter\@firstoftwo
270 }%
271 }
272 % \end{macrocode}
273 % \end{command}
274 %
275 %
276 % \begin{command}{\ifkomavarempty,\Ifkomavarempty}
277 % \changes{v2.8q}{2002/01/14}{added}
278 % \changes{v2.9i}{2002/09/04}{spurious spaced removed}
279 % \changes{v2.9i}{2002/09/04}{robust}
280 % \changes{v3.03}{2009/03/04}{using \cs{ifkomavar}}
281 % \changes{v3.03}{2009/03/04}{don't read the arguments but delegate them}
282 % \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
283 % \changes{v3.28}{2019/11/15}{\cs{ifkomavarempty} renamed to \cs{Ifkomavar}}
284 % \changes{v3.28}{2019/11/15}{\cs{ifkomavar} replaced by \cs{Ifkomavar}}
285 % \changes{v3.42}{2023/08/25}{new implementation using \cs{NewDocumentCommand}}
286 % \cs{Ifkomavarempty} tests if the variable with the \meta{name} of
287 % \texttt{\#1} is defined and the \meta{value} (non-star variant)
288 % resp. \meta{description} (star variant) is empty. If it is undefined or
289 % empty the \meta{code} of \texttt{\#2} is executed. If is defined and not
290 % empty the \meta{code} of \texttt{\#3} is executed.
291 % The old command \cs{ifkomavarempty} resp. \cs{ifkomavarempty*} is deprecated
292 % and should not be used any longer. This incompatible change has been done,
293 % because of a new recommendation of The \LaTeX{} Team (who does break its own
294 % recommendation, e.g., with \cs{ifToplevel}, \cs{ifthenelse} \dots).
295 % \begin{macrocode}
296 \newcommand*{\ifkomavarempty}{%
297 %<package> \PackageWarning{scrletter}{%
298 %<class> \ClassWarning{\KOMAClassName}{%
299 Usage of deprecated command `\string\ifkomavarempty'.\MessageBreak
300 The command has been renamed to because of a\MessageBreak
301 recommendation of LaTeX3 team members.\MessageBreak
302 Please replace `\string\ifkomavarempty' by `\string\Ifkomavarempty'%
303 }%
304 \Ifkomavarempty
305 }
306 \NewDocumentCommand{\Ifkomavarempty}{sm}{%
307 \Ifkomavar{#2}{%
308 \IfBooleanTF{#1}{%
309 \@ifundefined{scr@#2@name}\@firstoftwo{%
310 \expandafter\ifx\csname scr@#2@name\endcsname\@empty
311 \expandafter\@firstoftwo
312 \else
313 \expandafter\@secondoftwo
314 \fi
315 }%
316 }{%
317 \expandafter\ifx\csname scr@#2@var\endcsname\@empty
318 \expandafter\@firstoftwo
319 \else
320 \expandafter\@secondoftwo
321 \fi
322 }
323 }{%
324 \@komavar@err{use}{#2}%
325 \@gobbletwo
326 }%
327 }
328 % \end{macrocode}
329 % \begin{macro}{\if@komavarempty}
330 % \changes{v2.8q}{2002/01/14}{added}
331 % \changes{v3.42}{2023/08/25}{removed}
332 % \end{macro}%^^A \@ifkomavarempty
333 % \end{command}%^^A \ifkomavarempty, \Ifkomavarempty
334 %
335 % \begin{command}{\Ifkomavarxempty}
336 % \changes{v3.42}{2023/08/25}{added}
337 % This command is similar to the \cs{Ifkomavarempty} but uses \cs{Ifstr} for
338 % the comparison and therefore does a full expansion of the variable
339 % or output name.
340 % \begin{macrocode}
341 \NewDocumentCommand{\Ifkomavarxempty}{sm}{%
342 \Ifkomavar{#2}{%
343 \IfBooleanTF{#1}{%
344 \@ifundefined{scr@#2@name}\@firstoftwo{%
345 \Ifstr{\@nameuse{scr@#2@name}}{}%
346 }%
347 }{%
348 \Ifstr{\@nameuse{scr@#2@var}}{}%
349 }%
350 }{%
351 \@komavar@err{use}{#2}%
352 \@gobbletwo
353 }%
354 }
355 % \end{macrocode}
356 % \end{command}
357 %
358 %
359 % \begin{command}{\foreachkomavar}
360 % \changes{v3.27}{2019/03/13}{added}
361 % Executes \texttt{\#2} for each variable in the comma separated list
362 % \texttt{\#1}. An argument is appended to \texttt{\#2} which is the current
363 % variable to be processed. This command is short. So if you want so execute
364 % long code you should define a command for it. Sideeffect: \cs{reserved@a} is
365 % changed.
366 % \begin{macrocode}
367 \newcommand*{\foreachkomavar}[2]{%
368 \@for \reserved@a:=#1\do{%
369 \scr@trim@spaces\reserved@a
370 \ifx\reserved@a\@empty\else
371 \edef\reserved@a{\unexpanded{#2}{\reserved@a}}\expandafter\reserved@a
372 \fi
373 }%
374 }
375 % \end{macrocode}
376 % \end{command}
377 %
378 %
379 % \begin{command}{\foreachkomavarifempty}
380 % \changes{v3.27}{2019/03/13}{added}
381 % \changes{v3.28}{2019/11/15}{\cs{ifkomavarempty} replaced by \cs{Ifkomavarempty}}
382 % Same like \cs{foreachkomavar} but \texttt{\#2} is called only for empty
383 % variables and \texttt{\#3} is called for non empty.
384 % \begin{macrocode}
385 \newcommand*{\foreachkomavarifempty}[3]{%
386 \@for \reserved@a:=#1\do{%
387 \scr@trim@spaces\reserved@a
388 \ifx\reserved@a\@empty\else
389 \edef\reserved@a{%
390 \noexpand\Ifkomavarempty{\reserved@a}%
391 {\unexpanded{#2}{\reserved@a}}%
392 {\unexpanded{#3}{\reserved@a}}%
393 }\expandafter\reserved@a
394 \fi
395 }%
396 }
397 % \end{macrocode}
398 % \end{command}
399 %
400 % \begin{command}{\foreachemptykomavar}
401 % \changes{v3.27}{2019/03/13}{added}
402 % \changes{v3.28}{2019/11/15}{\cs{ifkomavarempty} replaced by \cs{Ifkomavarempty}}
403 % Similar to \cs{foreachemptykomavar} with \texttt{\#3}=\cs{@gobble}.
404 % \begin{macrocode}
405 \newcommand*{\foreachemptykomavar}[2]{%
406 \@for \reserved@a:=#1\do{%
407 \scr@trim@spaces\reserved@a
408 \ifx\reserved@a\@empty\else
409 \edef\reserved@a{%
410 \noexpand\Ifkomavarempty{\reserved@a}{\unexpanded{#2}{\reserved@a}}{}%
411 }\expandafter\reserved@a
412 \fi
413 }%
414 }
415 % \end{macrocode}
416 % \end{command}
417 %
418 % \begin{command}{\foreachnonemptykomavar}
419 % \changes{v3.27}{2019/03/13}{added}
420 % \changes{v3.28}{2019/11/15}{\cs{ifkomavarempty} replaced by \cs{Ifkomavarempty}}
421 % Similar to \cs{foreachemptykomavar} with \texttt{\#2}=\cs{@gobble}.
422 % \begin{macrocode}
423 \newcommand*{\foreachnonemptykomavar}[2]{%
424 \@for \reserved@a:=#1\do{%
425 \scr@trim@spaces\reserved@a
426 \ifx\reserved@a\@empty\else
427 \edef\reserved@a{%
428 \noexpand\Ifkomavarempty{\reserved@a}{}{\unexpanded{#2}{\reserved@a}}%
429 }\expandafter\reserved@a
430 \fi
431 }%
432 }
433 %</body>
434 % \end{macrocode}
435 % \end{command}
436 %
437 %
438 % \begin{macrocode}
439 %</letter>
440 % \end{macrocode}
441 %
442 % \Finale
443 % \PrintChanges
444 %
445 \endinput
446 % Local Variables:
447 % mode: doctex
448 % ispell-local-dictionary: "en_US"
449 % eval: (flyspell-mode 1)
450 % TeX-master: t
451 % TeX-engine: luatex-dev
452 % eval: (setcar (or (cl-member "Index" (setq-local TeX-command-list (copy-alist TeX-command-list)) :key #'car :test #'string-equal) (setq-local TeX-command-list (cons nil TeX-command-list))) '("Index" "mkindex %s" TeX-run-index nil t :help "makeindex for dtx"))
453 % End: