Debian Codegraph @

texmf-dist/source/latex/physics2/physics2-code.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2023 by Zhang Tingxuan <[email protected]>
4 %
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either
7 % version 1.3c of this license or (at your option) any later
8 % version. The latest version of this license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.3 or later is part of all distributions of
13 % LaTeX version 2005/12/01 or later.
14 %
15 % This work has the LPPL maintenance status `maintained'.
16 %
17 % The Current Maintainer of this work is Zhang Tingxuan.
18 %
19 % This work consists of the files physics2-code.dtx,
20 % and the derived files physics2.ins,
21 % physics2.sty,
22 % phy-<module>.sty,
23 % physics2-code.pdf,
24 % and README.md.
25 %
26 %<*internal>
27 \iffalse
28 %</internal>
29 %
30 %<*readme>
31 # The `physics2` package
32
33 This package defines commands for typesetting math formulae faster and
34 more simply. `physics2` is a modularized package, each module provides its
35 own function while `physics2.sty` itself only provides very few functions.
36 Therefore you can load modules separately after loading `physics2`.
37
38 Modules of `physics2` provide the following supports:
39
40 - Automatic braces;
41 - Dirac bra-ket notation;
42 - Easy way to typeset diagonal matrices and matrices with similar entries;
43 - Double cross and double dot (binary) operators for tensors;
44
45 **Welcome to pull requests
46 [here](https://github.com/AlphaZTX/physics2/pulls).**
47
48 ## License
49
50 This work may be distributed and/or modified under the conditions of the
51 [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
52 either version 1.3c of this license or (at your option) any later version.
53
54 This work has the LPPL maintenance status `maintained`.
55
56 The Current Maintainer of this work is
57 [Zhang Tingxuan](https://www.ctan.org/author/zhang-tx).
58
59 %</readme>
60 %
61 %<*internal>
62 \fi
63 \begingroup
64 \def\NameOfLaTeXe{LaTeX2e}
65 \expandafter\endgroup\ifx\NameOfLaTeXe\fmtname\else
66 \csname fi\endcsname
67 %</internal>
68 %
69 %<*install>
70 \input docstrip.tex
71 \keepsilent
72
73 \preamble
74
75 Copyright (C) 2023 by Zhang Tingxuan <[email protected]>
76
77 This work may be distributed and/or modified under the
78 conditions of the LaTeX Project Public License, either
79 version 1.3c of this license or (at your option) any later
80 version. The latest version of this license is in:
81
82 http://www.latex-project.org/lppl.txt
83
84 and version 1.3 or later is part of all distributions of
85 LaTeX version 2005/12/01 or later.
86
87 This work has the LPPL maintenance status `maintained'.
88
89 The Current Maintainer of this work is Zhang Tingxuan.
90
91 This work consists of the files physics2-code.dtx,
92 and the derived files physics2.ins,
93 physics2.sty,
94 phy-<module>.sty,
95 physics2-code.pdf,
96 and README.md.
97
98 \endpreamble
99
100 \generate{
101 \usedir{tex/latex/physics2}
102 \file{physics2.sty} {\from{physics2-code.dtx}{package}}
103 \file{phy-ab.sty} {\from{physics2-code.dtx}{ab}}
104 \file{phy-ab.braket.sty} {\from{physics2-code.dtx}{ab.braket}}
105 \file{phy-braket.sty} {\from{physics2-code.dtx}{braket}}
106 \file{phy-diagmat.sty} {\from{physics2-code.dtx}{diagmat}}
107 \file{phy-doubleprod.sty} {\from{physics2-code.dtx}{doubleprod}}
108 \file{phy-xmat.sty} {\from{physics2-code.dtx}{xmat}}
109 \file{phy-ab.legacy.sty} {\from{physics2-code.dtx}{ab.legacy}}
110 \file{phy-bm-um.legacy.sty} {\from{physics2-code.dtx}{bm-um.legacy}}
111 \file{phy-nabla.legacy.sty} {\from{physics2-code.dtx}{nabla.legacy}}
112 \file{phy-op.legacy.sty} {\from{physics2-code.dtx}{op.legacy}}
113 \file{phy-qtext.legacy.sty} {\from{physics2-code.dtx}{qtext.legacy}}
114 %</install>
115 %<*internal>
116 \usedir{source/latex/physics2}
117 \file{physics2.ins} {\from{physics2-code.dtx}{install}}
118 %</internal>
119 %<*install>
120 \usedir{doc/latex/physics2}
121 \nopreamble\nopostamble
122 \file{README.md} {\from{physics2-code.dtx}{readme}}
123 }
124
125 \obeyspaces
126 \Msg{**********************************************************}
127 \Msg{* *}
128 \Msg{* To finish the installation you have to move the *}
129 \Msg{* following files into a directory searched by TeX: *}
130 \Msg{* *}
131 \Msg{* physics2.sty, *}
132 \Msg{* (for all modules) phy-<module>.sty. *}
133 \Msg{* *}
134 \Msg{* The recommended directory is *}
135 \Msg{* TEXMFDIST/tex/latex/physics2 *}
136 \Msg{* *}
137 \Msg{* To produce the documentation, run the file *}
138 \Msg{* physics2-code.dtx through XeLaTeX or LuaLaTeX. *}
139 \Msg{* XeLaTeX is recommended. *}
140 \Msg{* *}
141 \Msg{* Happy TeXing! *}
142 \Msg{* *}
143 \Msg{**********************************************************}
144
145 \endbatchfile
146 %</install>
147 %
148 %<*internal>
149 \fi
150 %</internal>
151 %
152 %<*driver>
153 \ProvidesFile{fixdif.dtx}[2024/01/10 (c) Copyright 2022-2024 by Zhang Tingxuan]
154 \documentclass{l3doc}
155 \usepackage{doc}
156 \hypersetup{hidelinks}
157 \usepackage{color}
158 \definecolor{pkgcolor}{RGB}{0,120,80}
159 \definecolor{moducolor}{RGB}{0,0,150}
160 \definecolor{gray}{rgb}{.5,.5,.5}
161 \DeclareRobustCommand\pkg[1]{\texorpdfstring{\textcolor{pkgcolor}{\textsf{#1}}}{“#1”}}
162 \DeclareRobustCommand\modu[1]{\texorpdfstring{\textcolor{moducolor}{\textsf{#1}}}{“#1”}}
163 \DeclareRobustCommand\note{\textbf{Note:} }
164 \usepackage{amsmath}
165 \usepackage{fontspec}
166 \setmonofont{Latin Modern Mono Light}
167 \usepackage{fancyvrb}
168 \fvset{gobble=2,xleftmargin=2em}
169 \usepackage{physics2}
170 \usephysicsmodule{ab}
171 \makeatletter
172 \@addtoreset{CodelineNo}{section}
173 \newbox\@tempboxb
174 \DeclareDocumentCommand\macrodef{O{4em}O{.6}mo+m}{\leavevmode
175 \ensuremath{%
176 \setbox\@tempboxa\hbox to #1 {\hss\cs{#3}}%
177 \setbox\@tempboxb\vbox{\hsize=#2\textwidth
178 \textwidth=\hsize
179 \leftskip=\z@
180 \parindent=\z@
181 \textbf{begindef}\IfValueT{#4}{ \textcolor{gray}{(#4)}}\par
182 \begingroup\leftskip2em\parindent-1em#5\par\endgroup
183 \textbf{enddef}%
184 }%
185 \dp\@tempboxb=\dimexpr\ht\@tempboxb-\ht\@tempboxa\relax
186 \ht\@tempboxb=\ht\@tempboxa
187 \box\@tempboxa\leftarrow\box\@tempboxb
188 }}
189 \protected\long\def\macroifelse#1#2#3{\par
190 \textbf{if} #1 \textbf{then}\par
191 \begingroup\advance\leftskip1em#2\par\endgroup
192 \textbf{else}\par
193 \begingroup\advance\leftskip1em#3\par\endgroup
194 \textbf{endif}\par
195 }
196 \protected\long\def\macroif#1#2{\par
197 \textbf{if} #1 \textbf{then}\par
198 \begingroup\advance\leftskip1em#2\par\endgroup
199 \textbf{endif}\par
200 }
201 \protected\def\macrocr{\par}
202 \newdimen\ttwd
203 \protected\def\updatettwd{%
204 \setbox\@tempboxa\hbox{\normalsize\ttfamily a}
205 \ttwd=\wd\@tempboxa
206 }
207 \updatettwd
208 \makeatother
209 \title{Code documentation to the \pkg{physics2} package}
210 \author{Zhang Tingxuan}
211 \date{2024/01/10 v1.0.1}
212 \begin{document}
213 \DocInput{physics2-code.dtx}
214 \end{document}
215 %</driver>
216 % \fi
217 %
218 % \maketitle
219 %
220 % \tableofcontents\newpage
221 %
222 %^^A******************************************************************************** [physics2.sty]
223 % \part{The bare \pkg{physics2}}
224 % \section{The \pkg{physics2} package}
225 % \begin{macrocode}
226 %<*package>
227 \NeedsTeXFormat{LaTeX2e}[2020/10/01]
228 \ProvidesPackage{physics2}
229 [2024/01/10 v1.0.1 Tools for typesetting math for physics.]
230 % \end{macrocode}
231 % \subsection{Common variables}
232 % \begin{variable}{\phy@temp..}
233 % \begin{syntax}
234 % \cs{phy@temp}\meta{register type}\meta{a or b}
235 % \end{syntax}
236 % Some \LaTeXe\ variables starting with ``|\phy@temp|''.
237 % These variables can be shared by any module of \pkg{physics2}.
238 % \end{variable}
239 % \begin{macrocode}
240 \newcount \phy@tempcnta
241 \newdimen \phy@tempdima
242 \newdimen \phy@tempdimb
243 \newskip \phy@tempskipa
244 \newmuskip \phy@tempmuskipa
245 \newbox \phy@tempboxa
246 \newif \ifphy@tempswa
247 \newtoks \phy@toksa
248 % \end{macrocode}
249 % \subsection{Package requirements and module-loading methods}
250 % \pkg{physics2} requires \pkg{keyval} (part of the graphics bundle) to
251 % process options of modules.
252 % \begin{macrocode}
253 \RequirePackage{keyval}
254 \def\phy@true{true}
255 \def\phy@false{false}
256 % \end{macrocode}
257 % \begin{function}{\phy@define@key, \phy@setkeys, \phy@processkeyopt}
258 % \begin{syntax}
259 % \cs{phy@define@key} \marg{module} \marg{key} \oarg{default value} \marg{code}
260 % \cs{phy@setkeys} \marg{module} \marg{key-val list}
261 % \cs{phy@processkeyopt} \marg{module}
262 % \end{syntax}
263 % The position of \cs{phy@processkeyopt} in a \pkg{physics2} module is just the same
264 % as the position of \cs{ProcessOptions} in a regular \LaTeX\ package.
265 % \end{function}
266 % \begin{macrocode}
267 \long\def\phy@define@key#1{\define@key{phy-#1}}
268 \long\def\phy@setkeys#1{\setkeys{phy-#1}}
269 \def\phy@processkeyopt#1{\let\reserved@a\@empty%
270 \edef\reserved@a{\@ptionlist{\@currname.\@currext}}%
271 \edef\reserved@a{\noexpand\phy@setkeys{#1}{\reserved@a}}%
272 \reserved@a% the next line thanks to `geometry'
273 \AtEndOfPackage{\let\@unprocessedoptions\relax}}
274 % \end{macrocode}
275 % We use almost the same way to load \pkg{physics2} modules as \LaTeXe\ kernel
276 % does. We use a lot of kernel commands in \LaTeXe.
277 %
278 % \begin{function}{\usephysicsmodule, \phy@requiremodule}
279 % \begin{syntax}
280 % \cs{usephysicsmodule} \oarg{key-val options} \marg{module} \oarg{key-val options}
281 % \cs{phy@requiremodule} \oarg{key-val options} \marg{module} \oarg{key-val options}
282 % \end{syntax}
283 % \cs{usephysicsmodule} is a user command, and \cs{phy@requiremodule} is a developer
284 % command.
285 % \end{function}
286 % \begin{macrocode}
287 \def\usephysicsmodule{\phy@FWoptions\@pkgextension}
288 \let\phy@requiremodule\usephysicsmodule
289 \@onlypreamble\usephysicsmodule
290 \def\phy@FWoptions#1{\@ifnextchar[%]
291 {\phy@FW@ptions#1}{\phy@FW@ptions#1[]}}
292 \@onlypreamble\phy@FWoptions
293 \def\phy@FW@ptions#1[#2]#3{\@ifnextchar[%]
294 {\phy@FW@pti@ns#1[{#2}]#3}{\phy@FW@pti@ns#1[{#2}]#3[]}}
295 \@onlypreamble\phy@FW@ptions
296 \def\phy@FW@pti@ns#1[#2]#3[#4]{%
297 \def\reserved@b##1,{%
298 \ifx\@nnil##1\relax\else
299 \ifx\@nnil##1\@nnil\else
300 \noexpand\@onefilewithoptions{phy-##1}[{\unexpanded{#2}}][{#4}]%
301 \noexpand\@pkgextension
302 \fi
303 \expandafter\reserved@b
304 \fi}%
305 \edef\reserved@a{\zap@space#3 \@empty}%
306 \edef\reserved@a{\expandafter\reserved@b\reserved@a,\@nnil,}%
307 \reserved@a}
308 \@onlypreamble\phy@FW@pti@ns
309 % \end{macrocode}
310 % \subsection{The (used to be) \modu{common} module}
311 % The code below used to be the automatically-loaded \modu{common} module,
312 % but now we load it together with \pkg{physics2}'s code. This change may
313 % bring better performance in Windows system.
314 %
315 % Check if \pkg{unicode-math} loaded and (re)define the vert symbols.
316 % The \cs{relax}'s at the ends of \cs{vert} and \cs{Vert}'s definitions must not be
317 % removed. They are for \cs{ifx} to compare. \pkg{unicode-math} sets these symbols
318 % \cs{fam}1, \cs{symoperators} is equal to 1 in \LaTeXe\ kernel. Moreover, we make
319 % \cs{mid} as a delimiter but it may not work.
320 % \begin{macrocode}
321 \AtBeginDocument{\ifcsname symrm\endcsname
322 \protected\def\|{\Udelimiter 0 \symoperators "2016 }%
323 \protected\def\vert{\Udelimiter 0 \symoperators "007C\relax}%
324 \protected\def\Vert{\Udelimiter 0 \symoperators "2016\relax}%
325 \protected\def\mid{\Udelimiter 3 \symoperators "007C }%
326 \fi}
327 \protected\def\Vert{\delimiter"026B30D\relax}
328 \protected\def\mid{\delimiter"326A30C }
329 % \end{macrocode}
330 % \begin{function}{\delopen, \delclose}
331 % \begin{syntax}
332 % \cs{delopen} \meta{left delimiter}
333 % \cs{delclose} \meta{right delimiter}
334 % \end{syntax}
335 % Actually in \TeX, \cs{left} and \cs{right} will enclose the subformula as ``inner'',
336 % but \cs{delopen} and \cs{delclose} will make the subformula an empty open node and
337 % a non-empty close node.
338 % \end{function}
339 % \begin{macrocode}
340 \DeclareRobustCommand\delopen{\mathopen{}\mathclose\bgroup\left}
341 \DeclareRobustCommand\delclose{\aftergroup\egroup\right}
342 % Extension to 2e kernel's or amsmath's biggggg commands.
343 % \end{macrocode}
344 % \cs{bBigg@} is a command from \pkg{amsmath}. The code below should
345 % update with \pkg{amsmath} together.
346 % \begin{macrocode}
347 \ifdefined\bBigg@
348 \DeclareRobustCommand\biggg{\bBigg@{3}}
349 \DeclareRobustCommand\Biggg{\bBigg@{3.5}}
350 \else
351 \DeclareRobustCommand\biggg[1]{\leavevmode@ifvmode
352 {\hbox{$\left#1\vbox to20.5\p@{}\right.\n@space$}}}
353 \DeclareRobustCommand\Biggg[1]{\leavevmode@ifvmode
354 {\hbox{$\left#1\vbox to23.5\p@{}\right.\n@space$}}}
355 \AtBeginDocument{\ifdefined\bBigg@
356 \DeclareRobustCommand\biggg{\bBigg@{3}}%
357 \DeclareRobustCommand\Biggg{\bBigg@{3.5}}%
358 \fi}
359 \fi
360 \DeclareRobustCommand\bigggl{\mathopen\biggg}
361 \DeclareRobustCommand\bigggm{\mathrel\biggg}
362 \DeclareRobustCommand\bigggr{\mathclose\biggg}
363 \DeclareRobustCommand\Bigggl{\mathopen\Biggg}
364 \DeclareRobustCommand\Bigggm{\mathrel\Biggg}
365 \DeclareRobustCommand\Bigggr{\mathclose\Biggg}
366 % \end{macrocode}
367 % \begin{function}{\phy@mathvphantom}
368 % \begin{syntax}
369 % \cs{phy@mathvphantom} \marg{math mode material}
370 % \end{syntax}
371 % This command is just like \cs{vphantom} in \LaTeXe\ kernel but only works
372 % in math mode.
373 % \end{function}
374 % \begin{macrocode}
375 \def\phy@mathvphantom#1{\setbox\phy@tempboxa=\hbox{}%
376 \mathchoice
377 {\setbox\@tempboxa\hbox{$\displaystyle#1$}%
378 \ht\phy@tempboxa=\ht\@tempboxa
379 \dp\phy@tempboxa=\dp\@tempboxa
380 \box\phy@tempboxa}
381 {\setbox\@tempboxa\hbox{$\textstyle#1$}%
382 \ht\phy@tempboxa=\ht\@tempboxa
383 \dp\phy@tempboxa=\dp\@tempboxa
384 \box\phy@tempboxa}
385 {\setbox\@tempboxa\hbox{$\scriptstyle#1$}%
386 \ht\phy@tempboxa=\ht\@tempboxa
387 \dp\phy@tempboxa=\dp\@tempboxa
388 \box\phy@tempboxa}
389 {\setbox\@tempboxa\hbox{$\scriptscriptstyle#1$}%
390 \ht\phy@tempboxa=\ht\@tempboxa
391 \dp\phy@tempboxa=\dp\@tempboxa
392 \box\phy@tempboxa}%
393 }
394 % \end{macrocode}
395 % \subsection{The (used to be) \modu{explsetup} module}
396 % Some common variables and functions for experimental \LaTeX3 syntax.
397 % \begin{macrocode}
398 %<@@=phy>
399 \ExplSyntaxOn
400 \int_new:N \l_@@_tmpa_int
401 \int_new:N \l_@@_tmpb_int
402 \tl_new:N \l_@@_tmpa_tl
403 \tl_new:N \l_@@_tmpb_tl
404 % \end{macrocode}
405 % The function that can gobble one token.
406 % \begin{macrocode}
407 \cs_new:Npn \_@@_gobble_i:n #1 { }
408 \ExplSyntaxOff
409 %<@@=>
410 %</package>
411 % \end{macrocode}
412 %
413 %^^A******************************************************************************** [l2e modules]
414 % \part{Modules written in \LaTeXe\ syntax}
415 % \section{The \modu{ab} module}
416 % \begingroup\color{gray}%
417 % \noindent$\langle*\hbox{\sffamily gibberish}\rangle$\par
418 % This module is important but the code is hard to read. One of the motivations
419 % I manage \pkg{physics2} with \pkg{DocStrip} is that, when I tried to write a
420 % new module based on \modu{ab} after 5 months when I maintained \pkg{physics2}
421 % the last time, I found that I could not understand the code I wrote at all!
422 % Therefore, it's significant to comment out the alien code in \modu{ab}.\par
423 % \noindent$\langle/\hbox{\sffamily gibberish}\rangle$\par
424 % \endgroup
425 % \begin{macrocode}
426 %<*ab>
427 \ProvidesFile{phy-ab.sty}
428 [2023/10/24 `ab' (autobraces) module of physics2]
429 % \end{macrocode}
430 % If you don't know when to use \cs{phy@define@key}, \cs{phy@setkeys} and
431 % \cs{phy@processkeyopt} in a module, see ahead. In \modu{ab}, the |tightbraces|
432 % option can control if the automatically-sized braces are tight or not. Do you
433 % remember \cs{delopen} and \cs{delclose}?
434 % \begin{macrocode}
435 \phy@define@key{ab}{tightbraces}[true]{\def\@phy@abtight{#1}}
436 \phy@setkeys{ab}{tightbraces=true}
437 \phy@processkeyopt{ab}
438 % \end{macrocode}
439 % \begin{function}{\phy@abopen, \phy@abclose}
440 % \begin{syntax}
441 % \cs{phy@abopen} \meta{left delimiter}
442 % \cs{phy@abclose} \meta{right delimiter}
443 % \end{syntax}
444 % They are defined either \{\cs{delopen}, \cs{delclose}\} or
445 % \{\cs{left}, \cs{right}\}. If a module requires \modu{ab},
446 % these two commands are likely to be used.
447 % \end{function}
448 % \begin{macrocode}
449 \ifx\@phy@abtight\phy@true
450 \let\phy@abopen\delopen
451 \let\phy@abclose\delclose
452 \else
453 \let\phy@abopen\left
454 \let\phy@abclose\right
455 \fi
456 % \end{macrocode}
457 % \subsection{The implementation of \cs{ab}}
458 % This is the alienest part of \modu{ab}. It's better to draw something rather
459 % than write boring comments. First let's take a look at \cs{ab}'s syntax.
460 % After \cs{ab} should be a pair of delimiters; take \texttt{()} as an example.
461 % Between \cs{ab} and ``\texttt{(}'' can be a biggg command or star, or even nothing.
462 % \cs{ab} is defined as follows:
463 % \[ \macrodef{ab}{\cs{phy@d@lx} \texttt{\{mb\}} \texttt{\{ab\}}} \]
464 % where \texttt{ab} is the branch name of |\ab()|, and \texttt{mb} is the branch
465 % name of |\ab\big()| and |\ab*()|. Then let's see the syntax of \cs{phy@d@lx}.
466 % \[ \hbox{\ttfamily\cs{phy@d@lx} \marg{biggg or star branch name} \marg{automatic branch name} \{\#3\}} \]
467 % Here exists an \texttt{\#3}. \texttt{\#3} is one token immediately following \cs{ab},
468 % which can be \{\,a biggg command or a star\,\} or a ``\texttt{(}'', under our assumption.
469
470 % \cs{phy@d@lx} is defined as follows:
471 % \[
472 % \macrodef {phy@d@lx} [\#1: biggg or star branch name, \meta{mb}; \#2: automatic branch name, \meta{ab}; \#3, the token after \cs{ab}]
473 % {%
474 % \macroifelse {\#3 == biggg or \#3 == star ($\Leftrightarrow$ csname \{phy@del\cs{string}\#3\} is defined)}
475 % {let \meta{next cs} = csname \{phy@d@lx\meta{mb}\}}
476 % {let \meta{next cs} = csname \{phy@d@lx\meta{ab}\}}
477 % \meta{next cs} \#3
478 % }
479 % \]
480 % The condition should be true when \#3 is |\big| or |*|, and it should be false when
481 % \#3 is ``|(|''. Accordingly, in math mode,
482 % \begin{align*}
483 % \hbox{\ttfamily\cs{ab} \cs{big} (}&\quad\to\quad\hbox{\ttfamily\cs{phy@d@lxmb} \cs{big} (}\\
484 % \hbox{\ttfamily\cs{ab} \phantom{\cs{big}} (}&\quad\to\quad\hbox{\ttfamily\cs{phy@d@lxab} \phantom{\cs{big}} (}
485 % \end{align*}
486 % Then we meet two new commands --- \cs{phy@d@lxmb} and \cs{phy@d@lxab}. Syntax is as follows.
487 % \begin{align*}
488 % &\hbox{\ttfamily\cs{phy@d@lxmb} \meta{biggg or *}
489 % \meta{left delimiter} \meta{subformula} \meta{right delimiter}}\\
490 % &\hbox{\ttfamily\cs{phy@d@lxab} \phantom{\meta{biggg or *}}
491 % \meta{left delimiter} \meta{subformula} \meta{right delimiter}}
492 % \end{align*}
493 % Notice that \texttt{ab} and \texttt{mb} in the above commands are names of \cs{ab}'s
494 % two branches --- they are like namespaces. \cs{phy@d@lxmb} and \cs{phy@d@lxab} are
495 % defined by the following two lines:
496 % \begin{Verbatim}
497 % \phy@d@l@genxm{mb}
498 % \phy@d@l@genxa{ab}
499 % \end{Verbatim}
500 % \cs{phy@d@l@genxm} and \cs{phy@d@l@genxa} are defined as follows:
501 % \begin{align*}
502 % & \macrodef {phy@d@l@genxm} [\#1: biggg or star branch name, \meta{mb}]
503 % {%
504 % \macrodef [12\ttwd] {phy@d@lx\meta{mb}} [\#\#1: biggg or star; \#\#2: left delimiter]
505 % {%
506 % \cs{begingroup}
507 % \macroifelse {\#\#1 == star}
508 % {\meta{temp} $\gets$ \cs{relax}}
509 % {\meta{temp} $\gets$ \#\#1}
510 % csname \{phy@\meta{mb}@\cs{string}\#\#2\} \meta{temp} \#\#2 \macrocr
511 % \% requires an \cs{endgroup} after the right delimiter
512 % }
513 % } \\[1ex]
514 % & \macrodef {phy@d@l@genxa} [\#1: automatic branch name, \meta{ab}]
515 % {%
516 % \macrodef [12\ttwd] {phy@d@lx\meta{ab}} [\#\#1: left delimiter]
517 % {%
518 % csname \{phy@\meta{ab}@\cs{string}\#\#1\} \#\#1
519 % }
520 % }
521 % \end{align*}
522 % So we can get
523 % \begin{align*}
524 % \hbox{\ttfamily\cs{ab} \cs{big} (}&\quad\to\quad\hbox{\cs{begingroup} csname \{phy@mb@(\} \cs{big}\hskip2\ttwd\ \texttt(}\\
525 % \hbox{\ttfamily\cs{ab} *\phantom{big} (}&\quad\to\quad\hbox{\cs{begingroup} csname \{phy@mb@(\} \cs{relax} \texttt(}\\
526 % \hbox{\ttfamily\cs{ab} \phantom{\cs{big}} (}&\quad\to\quad\hbox{\hskip11\ttwd\ csname \{phy@ab@(\} \texttt(}
527 % \end{align*}
528 % The csnames above (\cs{phy@mb@(} and \cs{phy@ab@(}) are generated with \cs{phy@AB@gen}.
529 % \[ \hbox{\cs{phy@AB@gen} \marg{branch name} \meta{left delimiter} \marg{arg spec} \marg{definition}} \]
530 % If \meta{branch name} is \texttt{mb}, \marg{arg spec} should be |mr()|, where |m| is for biggg or star;
531 % If \meta{branch name} is \texttt{ab}, \marg{arg spec} should be |r()|.
532 %
533 % \note The ``|(|'' in the example above must not be replaced by a subformula braced by a pair of |{}|.
534 %
535 % \begin{function}{\phy@AB@gen}
536 % \begin{syntax}
537 % \cs{phy@AB@gen} \marg{branch name} \meta{left delimiter} \marg{arg spec} \marg{definition}
538 % \end{syntax}
539 % \end{function}
540 % \begin{macrocode}
541 \def\phy@AB@gen#1#2{\expandafter\DeclareDocumentCommand\csname phy@#1@\string#2\endcsname}
542 \phy@AB@gen{ab}({r()}{\phy@abopen(#1\phy@abclose)}
543 \phy@AB@gen{ab}[{r[]}{\phy@abopen[#1\phy@abclose]}
544 \phy@AB@gen{ab}\{{r\{\}}{\phy@abopen\{#1\phy@abclose\}}
545 \phy@AB@gen{ab}|{r||}{\phy@abopen|#1\phy@abclose|}
546 \phy@AB@gen{ab}\|{r\|\|}{\phy@abopen\|#1\phy@abclose\|}
547 \phy@AB@gen{ab}<{r<>}{\phy@abopen<#1\phy@abclose>}
548 \phy@AB@gen{ab}\lbrace{r\lbrace\rbrace}{\phy@abopen\lbrace#1\phy@abclose\rbrace}
549 \phy@AB@gen{ab}\vert{r\vert\vert}{\phy@abopen\vert#1\phy@abclose\vert}
550 \phy@AB@gen{ab}\Vert{r\Vert\Vert}{\phy@abopen\Vert#1\phy@abclose\Vert}
551 \phy@AB@gen{ab}\langle{r\langle\rangle}{\phy@abopen\langle#1\phy@abclose\rangle}
552 % \end{macrocode}
553 % \cs{endgroup}'s in the end of the following definitions are corresponding to
554 % \cs{begingroup}'s in the definition of \cs{phy@d@l@genxm}.
555 % \begin{macrocode}
556 \phy@AB@gen{mb}({mr()}{\mathopen#1(#2\mathclose#1)\endgroup}
557 \phy@AB@gen{mb}[{mr[]}{\mathopen#1[#2\mathclose#1]\endgroup}
558 \phy@AB@gen{mb}\{{mr\{\}}{\mathopen#1\lbrace#2\mathclose#1\rbrace\endgroup}
559 \phy@AB@gen{mb}|{mr||}{\mathopen#1\vert#2\mathclose#1\vert\endgroup}
560 \phy@AB@gen{mb}\|{mr\|\|}{\mathopen#1\Vert#2\mathclose#1\Vert\endgroup}
561 \phy@AB@gen{mb}<{mr<>}{\mathopen#1\langle#2\mathclose#1\rangle\endgroup}
562 \phy@AB@gen{mb}\lbrace{mr\lbrace\rbrace}{\mathopen#1\lbrace#2\mathclose#1\rbrace\endgroup}
563 \phy@AB@gen{mb}\vert{mr\vert\vert}{\mathopen#1\vert#2\mathclose#1\vert\endgroup}
564 \phy@AB@gen{mb}\Vert{mr\Vert\Vert}{\mathopen#1\Vert#2\mathclose#1\Vert\endgroup}
565 \phy@AB@gen{mb}\langle{mr\langle\rangle}{\mathopen#1\langle#2\mathclose#1\rangle\endgroup}
566 % \end{macrocode}
567 % \begin{function}{\phy@del\string.}
568 % The syntax seems not important. These following lines seems only for \cs{ifcsname}
569 % to judge if the commands are defined.
570 % \end{function}
571 % \begin{macrocode}
572 \def\phy@del#1#2#3{\phy@abopen#1#3\phy@abclose#2}
573 \expandafter\def\csname phy@del\string*\endcsname#1#2#3{\mathopen#1#3\mathclose#2}
574 \expandafter\def\csname phy@del\string\big\endcsname#1#2#3{\bigl#1#3\bigr#2}
575 \expandafter\def\csname phy@del\string\Big\endcsname#1#2#3{\Bigl#1#3\Bigr#2}
576 \expandafter\def\csname phy@del\string\bigg\endcsname#1#2#3{\biggl#1#3\biggr#2}
577 \expandafter\def\csname phy@del\string\Bigg\endcsname#1#2#3{\Biggl#1#3\Biggr#2}
578 \expandafter\def\csname phy@del\string\biggg\endcsname#1#2#3{\bigggl#1#3\bigggr#2}
579 \expandafter\def\csname phy@del\string\Biggg\endcsname#1#2#3{\Bigggl#1#3\Bigggr#2}
580 % \end{macrocode}
581 % \begin{function}{\phy@d@lx}
582 % \begin{syntax}
583 % \cs{phy@d@lx} \marg{biggg or star branch name} \marg{automatic branch name} \{\#3\}
584 % \end{syntax}
585 % \end{function}
586 % \begin{macrocode}
587 \def\phy@d@lx#1#2#3{%
588 \ifcsname phy@del\string#3\endcsname
589 \def\reserved@a{#1}% #3 is star or \biggg
590 \else
591 \def\reserved@a{#2}% #3 is delimiter
592 \fi
593 \csname phy@d@lx\reserved@a\endcsname#3}
594 % \end{macrocode}
595 % \begin{function}{\phy@d@l@genxm, \phy@d@l@genxa}
596 % \begin{syntax}
597 % \cs{phy@d@l@genxm} \marg{biggg or star branch name}
598 % \cs{phy@d@l@genxa} \marg{automatic branch name}
599 % \end{syntax}
600 % \end{function}
601 % \begin{macrocode}
602 \def\phy@d@l@genxm#1{%
603 \expandafter\def\csname phy@d@lx#1\endcsname##1##2{%
604 \begingroup % \endgroup is at the end of #4 of \phy@AB@gen
605 \ifx##1*\let\phy@tempa=\relax\else\let\phy@tempa=##1\fi
606 \csname phy@#1@\string##2\endcsname\phy@tempa##2}}
607 \def\phy@d@l@genxa#1{%
608 \expandafter\def\csname phy@d@lx#1\endcsname##1{%
609 \csname phy@#1@\string##1\endcsname##1}}
610 % \end{macrocode}
611 % \begin{function}{\phy@d@lxmb, \phy@d@lxab}
612 % \begin{syntax}
613 % \cs{phy@d@lxmb} \meta{biggg or *} \meta{left delimiter} \meta{subformula} \meta{right delimiter}
614 % \cs{phy@d@lxab} \phantom{\meta{biggg or *}} \meta{left delimiter} \meta{subformula} \meta{right delimiter}
615 % \end{syntax}
616 % \end{function}
617 % \begin{macrocode}
618 \phy@d@l@genxm{mb}
619 \phy@d@l@genxa{ab}
620 % \end{macrocode}
621 % \begin{function}{\ab}
622 % The users' command \cs{ab}.
623 % \end{function}
624 % \begin{macrocode}
625 \DeclareRobustCommand\ab{\phy@d@lx{mb}{ab}}
626 % \end{macrocode}
627 % \subsection{\cs{pab}-like commands}
628 % This is so simple. No need to comment a lot.
629 %
630 % \begin{function}{\phy@d@l@geny}
631 % \begin{syntax}
632 % \cs{phy@d@l@geny} \meta{command} \meta{left delimiter} \meta{right delimiter}
633 % \end{syntax}
634 % This command used to define commands like \cs{pab}.
635 % \end{function}
636 % \begin{macrocode}
637 \def\phy@d@l@geny#1#2#3{%
638 \DeclareDocumentCommand#1{som}{% ##1: star; ##2: bigg (csname); ##3: subformula.
639 \IfBooleanTF{##1}%
640 {#2##3#3}%
641 {\IfValueTF{##2}%
642 {\csname##2l\endcsname#2##3\csname##2r\endcsname#3}%
643 {\phy@abopen#2##3\phy@abclose#3}%
644 }%
645 }%
646 }
647 \phy@d@l@geny\pab()
648 \phy@d@l@geny\bab[]
649 \phy@d@l@geny\Bab\lbrace\rbrace
650 \phy@d@l@geny\vab\vert\vert
651 \phy@d@l@geny\aab\langle\rangle
652 \phy@d@l@geny\Vab\Vert\Vert
653 %</ab>
654 % \end{macrocode}
655 % \section{The \modu{ab.braket} module}
656 % \begin{macrocode}
657 %<*ab.braket>
658 \ProvidesFile{phy-ab.braket.sty}
659 [2023/10/24 `ab.braket' module of physics2]
660 % \end{macrocode}
661 % This module requires \cs{phy@abopen} and \cs{phy@abclose} from \modu{ab}.
662 % This module may have conflict with \modu{braket}.
663 % \begin{macrocode}
664 \phy@requiremodule{ab}
665 \ifdefined\phy@bra@@
666 \PackageWarning{physics2}{You cannot load `ab.braket' and `braket'
667 modules together.\MessageBreak Only `ab.braket' module works now.}
668 \fi
669 % \end{macrocode}
670 % \begin{function}{\bra}
671 % \begin{syntax}
672 % \cs{bra} \char`\<\ \meta{subformula} \char`\|
673 % \end{syntax}
674 % \end{function}
675 % \begin{macrocode}
676 \phy@AB@gen{br.m}<{mr<|}{\mathopen#1\langle#2\mathclose#1\vert\endgroup}
677 \phy@AB@gen{br.a}<{r<|}{\phy@abopen\langle#1\phy@abclose\vert}
678 \phy@d@l@genxm{br.m}
679 \phy@d@l@genxa{br.a}
680 \DeclareRobustCommand\bra{\phy@d@lx{br.m}{br.a}}
681 % \end{macrocode}
682 % \begin{function}{\ket}
683 % \begin{syntax}
684 % \cs{ket} \char`\|\ \meta{subformula} \char`\>
685 % \end{syntax}
686 % \end{function}
687 % \begin{macrocode}
688 \phy@AB@gen{kt.m}|{mr|>}{\mathopen#1\vert#2\mathclose#1\rangle\endgroup}
689 \phy@AB@gen{kt.a}|{r|>}{\phy@abopen\vert#1\phy@abclose\rangle}
690 \phy@d@l@genxm{kt.m}
691 \phy@d@l@genxa{kt.a}
692 \DeclareRobustCommand\ket{\phy@d@lx{kt.m}{kt.a}}
693 % \end{macrocode}
694 % \begin{function}{\braket}
695 % \begin{syntax}
696 % \cs{braket} \char`\<\ \meta{subformula 1} \char`\|\ \meta{subformula 2} {\color{gray}\textrm{[}\char`\|\ \meta{subformula 3} \textrm{\dots]}} \char`\>
697 % \end{syntax}
698 % \end{function}
699 % \begin{macrocode}
700 \begingroup
701 \catcode`\|=\active
702 \gdef\phy@@mb@bk#1#2{\begingroup
703 \mathcode`\|="8000\def|{\egroup#1\vert\bgroup}%
704 \def\<{\mathrel{<}}\def\>{\mathrel{>}}%
705 \mathopen#1\langle\bgroup#2\egroup\mathclose#1\rangle\endgroup}
706 \gdef\phy@@ab@bk#1{\begingroup
707 \mathcode`\|="8000\def|{\egroup\phy@abb@bkv\bgroup}%
708 \def\<{\mathrel{<}}\def\>{\mathrel{>}}%
709 \phy@abopen\langle\bgroup#1\egroup\phy@abclose\rangle\endgroup}
710 \endgroup
711 \def\phy@abb@bkv{\middle\vert}
712 \phy@AB@gen{bk.m}<{mr<>}{\phy@@mb@bk#1{#2}\endgroup}
713 \phy@AB@gen{bk.a}<{r<>}{\phy@@ab@bk{#1}}
714 \phy@d@l@genxm{bk.m}
715 \phy@d@l@genxa{bk.a}
716 \DeclareRobustCommand\braket{\phy@d@lx{bk.m}{bk.a}}
717 % \end{macrocode}
718 % \begin{function}{\ketbra}
719 % \begin{syntax}
720 % \cs{braket} \char`\|\ \meta{subformula 1} \char`\>\ \meta{subformula 2} \char`\<\ \meta{subformula 3} \char`\|
721 % \end{syntax}
722 % \end{function}
723 % \begin{macrocode}
724 \begingroup
725 \catcode`\<=\active
726 \catcode`\>=\active
727 \gdef\phy@@mb@kb#1#2{\begingroup
728 \mathcode`\<="8000 \mathcode`\>="8000%
729 \def<{#1\langle}\def>{#1\rangle}%
730 \def\<{\phy@abb@l}\def\>{\phy@abb@r}%
731 \mathopen#1\vert#2\mathclose#1\vert\endgroup}
732 \endgroup
733 \gdef\phy@@ab@kb#1>#2<#3\phy@@end{\begingroup
734 \def\<{\phy@abb@l}\def\>{\phy@abb@r}%
735 \phy@abopen\vert\mathopen{\phy@mathvphantom{#3}}#1\phy@abclose\rangle#2%
736 \phy@abopen\langle#3\mathclose{\phy@mathvphantom{#1}}\phy@abclose\vert
737 \endgroup}
738 \AtBeginDocument{\ifcsname symbf\endcsname
739 \def\phy@abb@l{\Umathchar 3 \symoperators "003C }%
740 \def\phy@abb@r{\Umathchar 3 \symoperators "003E }%
741 \fi}
742 \def\phy@abb@l{\mathchar"313C }
743 \def\phy@abb@r{\mathchar"313E }
744 \phy@AB@gen{kb.m}|{mr||}{\phy@@mb@kb#1{#2}\endgroup}
745 \phy@AB@gen{kb.a}|{r||}{\phy@@ab@kb#1\phy@@end}
746 \phy@d@l@genxm{kb.m}
747 \phy@d@l@genxa{kb.a}
748 \DeclareRobustCommand\ketbra{\phy@d@lx{kb.m}{kb.a}}
749 %</ab.braket>
750 % \end{macrocode}
751 % \section{The \modu{braket} module}
752 % \begin{macrocode}
753 %<*braket>
754 \ProvidesFile{phy-braket.sty}
755 [2023/10/24 `braket' module of physics2]
756 % \end{macrocode}
757 % This module requires \cs{phy@abopen} and \cs{phy@abclose} from \modu{ab}.
758 % This module may have conflict with \modu{ab.braket}.
759 % \begin{macrocode}
760 \phy@requiremodule{ab}
761 \ifdefined\phy@abb@bkv
762 \PackageWarning{physics2}{You cannot load `ab.braket' and `braket'
763 modules together.\MessageBreak Only `braket' module works now.}
764 \fi
765 % \end{macrocode}
766 % \begin{function}{\bra}
767 % \begin{syntax}
768 % \cs{bra} * \oarg{biggg} \marg{subformula}
769 % \end{syntax}
770 % \end{function}
771 % \begin{macrocode}
772 \DeclareDocumentCommand\bra{ s o m }{%
773 \IfBooleanTF{#1}
774 {\mathopen\langle#3\mathclose\vert}
775 {\IfValueTF{#2}
776 {\csname#2l\endcsname\langle#3\csname#2r\endcsname\vert}
777 {\phy@abopen\langle#3\phy@abclose\vert}%
778 }%
779 }
780 % \end{macrocode}
781 % \begin{function}{\ket}
782 % \begin{syntax}
783 % \cs{ket} * \oarg{biggg} \marg{subformula}
784 % \end{syntax}
785 % \end{function}
786 % \begin{macrocode}
787 \DeclareDocumentCommand\ket{ s o m }{%
788 \IfBooleanTF{#1}
789 {\mathopen\vert#3\mathclose\rangle}
790 {\IfValueTF{#2}
791 {\csname#2l\endcsname\vert#3\csname#2r\endcsname\rangle}
792 {\phy@abopen\vert#3\phy@abclose\rangle}%
793 }%
794 }
795 % \end{macrocode}
796 % \begin{function}{\braket}
797 % \begin{syntax}
798 % \cs{ket} * [\meta{biggg}, $n \in \{1,2,3\}$] \marg{subformula 1} \dots \marg{subformula $n$}
799 % \end{syntax}
800 % \end{function}
801 % \begin{macrocode}
802 \DeclareDocumentCommand\braket{ s O{} }{%
803 \IfBooleanTF{#1}%
804 {%
805 \gdef\@phy@bk@argnum{ii}%
806 \phy@bk@doopt{#2}%
807 \gdef\@phy@bk@l{mathopen}%
808 \gdef\@phy@bk@m{mathord}%
809 \gdef\@phy@bk@r{mathclose}%
810 }%
811 {%
812 \gdef\@phy@bk@argnum{ii}%
813 \gdef\@phy@bk@l{phy@abopen}%
814 \gdef\@phy@bk@m{middle}%
815 \gdef\@phy@bk@r{phy@abclose}%
816 \phy@bk@doopt{#2}%
817 }%
818 \csname phy@bk@in@\@phy@bk@argnum\endcsname%
819 }
820 % \end{macrocode}
821 % \begin{function}{\phy@bk@in@i, \phy@bk@in@ii, \phy@bk@in@iii}
822 % \begin{syntax}
823 % \cs{phy@bk@in@\meta{n.roman}} \marg{subformula 1} \dots \marg{subformula $n$}
824 % \end{syntax}
825 % \meta{n.roman} is $n$ in roman lowercase, where $n \in \{1,2,3\}$.
826 % \end{function}
827 % \begin{macrocode}
828 \def\phy@bk@in@i#1{%
829 \csname\@phy@bk@l\endcsname\langle{#1}%
830 \csname\@phy@bk@r\endcsname\rangle}
831 \def\phy@bk@in@ii#1#2{%
832 \csname\@phy@bk@l\endcsname\langle{#1}%
833 \csname\@phy@bk@m\endcsname\vert{#2}%
834 \csname\@phy@bk@r\endcsname\rangle}
835 \def\phy@bk@in@iii#1#2#3{%
836 \csname\@phy@bk@l\endcsname\langle{#1}%
837 \csname\@phy@bk@m\endcsname\vert{#2}%
838 \csname\@phy@bk@m\endcsname\vert{#3}%
839 \csname\@phy@bk@r\endcsname\rangle}
840 % \end{macrocode}
841 % \begin{function}{\phy@bk@doopt, \phy@bk@do@pt}
842 % \begin{syntax}
843 % \cs{phy@bk@doopt} \marg{clist}
844 % \end{syntax}
845 % Parse the optional argument of \cs{braket}. This will add 3 entries to hash.
846 % \end{function}
847 % \begin{macrocode}
848 \def\@phy@bk@do@pt#1,{\ifx#1\relax\@empty\else
849 \edef\reserved@a{\zap@space#1 \@empty}%
850 \ifx\reserved@a\@empty\else
851 \ifcsname phy@del\expandafter\string\csname\reserved@a\endcsname\endcsname
852 \xdef\@phy@bk@l{\reserved@a l}%
853 \xdef\@phy@bk@m{\reserved@a}% but not m (m stands for \mathrel)
854 \xdef\@phy@bk@r{\reserved@a r}%
855 \else
856 \ifnum\reserved@a>3%
857 \PackageError{physics2}{\string\braket\space can only take 3
858 mandatory arguments at most}{Check if you had written a number
859 more than 3 in the [optional] argument.}%
860 \fi
861 \xdef\@phy@bk@argnum{\romannumeral\reserved@a}%
862 \fi
863 \fi
864 \expandafter\@phy@bk@do@pt\fi}
865 \def\phy@bk@doopt#1{\@phy@bk@do@pt#1,\relax,}
866 % \end{macrocode}
867 % \begin{function}{\ketbra}
868 % \begin{syntax}
869 % \cs{ketbra} * [\meta{biggg}] \marg{subformula 1} \oarg{between 1 and 2} \marg{subformula 2}
870 % \end{syntax}
871 % \end{function}
872 % \begin{macrocode}
873 \DeclareDocumentCommand\ketbra{ s o m O{} m }{%
874 \IfBooleanTF{#1}%
875 {\mathopen\vert#3\mathclose\rangle#4\mathopen\langle#5\mathclose\vert}%
876 {\IfValueTF{#2}%
877 {\csname#2l\endcsname\vert#3\csname#2r\endcsname\rangle#4%
878 \csname#2l\endcsname\langle#5\csname#2r\endcsname\vert}%
879 {\begingroup
880 \phy@abopen\vert\mathopen{\phy@mathvphantom{#5}}#3\phy@abclose\rangle#4%
881 \phy@abopen\langle#5\mathclose{\phy@mathvphantom{#3}}\phy@abclose\vert
882 \endgroup}%
883 }%
884 }
885 %</braket>
886 % \end{macrocode}
887 % \section{The \modu{doubleprod} module}
888 % \begin{macrocode}
889 %<*doubleprod>
890 \ProvidesFile{phy-doubleprod.sty}
891 [2023/10/24 `doubleprod' (vertically stacked binary operators) module of physics2]
892 % \end{macrocode}
893 % Boolean options.
894 % \begin{macrocode}
895 \phy@define@key{doubleprod}{crosssymbol}{\def\@phy@dbl@c{#1}}
896 \phy@define@key{doubleprod}{dotsymbol}{\def\@phy@dbl@d{#1}}
897 \phy@define@key{doubleprod}{crossscale}{\def\@phy@dbl@sc{#1}}
898 \phy@define@key{doubleprod}{dotscale}{\def\@phy@dbl@sd{#1}}
899 \phy@define@key{doubleprod}{crossopenup}{\def\@phy@dbl@oc{#1}}
900 \phy@define@key{doubleprod}{dotopenup}{\def\@phy@dbl@od{#1}}
901 \phy@setkeys{doubleprod}{crosssymbol=\times,dotsymbol=\ldotp,
902 crossscale=0.8,dotscale=1,crossopenup=.02,dotopenup=.2}
903 \phy@processkeyopt{doubleprod}
904 \def\phy@dbl@gen#1#2#3#4{%
905 \DeclareRobustCommand#1{\mathbin{\vcenter{\baselineskip\z@skip%
906 \lineskip#4\phy@dblcurrf@size%
907 \setbox\@tempboxa=\hbox{\fontsize{#2\phy@dblcurrf@size}\z@$#3$}%
908 \copy\@tempboxa\box\@tempboxa}}}}
909 \def\phy@dblcurrf@size{\dimexpr\f@size pt\relax}
910 \phy@dbl@gen\doublecross\@phy@dbl@sc\@phy@dbl@c\@phy@dbl@oc
911 \phy@dbl@gen\doubledot\@phy@dbl@sd\@phy@dbl@d\@phy@dbl@od
912 %</doubleprod>
913 % \end{macrocode}
914 %^^A******************************************************************************** [l3 modules]
915 % \part{Modules written in \LaTeX3 syntax}
916 % We use |phy| as the namespace for \pkg{physics2} modules.
917 % \begin{macrocode}
918 %<@@=phy>
919 % \end{macrocode}
920 % \section{The \modu{diagmat} module}
921 % \begin{macrocode}
922 %<*diagmat>
923 \ProvidesExplFile{phy-diagmat.sty}{2024/01/10}{}
924 {`diagmat' module of physics2}
925 \RequirePackage { amsmath }
926 \phy@define@key { diagmat } { empty } [ 0 ] { \tl_gset:Nn \l_@@_mat_empty_tl { #1 } }
927 % \end{macrocode}
928 % This module requires some new variables.
929 % \begin{macrocode}
930 \clist_new:N \l_@@_mat_diag_clist
931 \int_new:N \l_@@_mat_dim_int
932 \tl_new:N \l_@@_mat_line_tl
933 \tl_new:N \l_@@_diagmat_tl
934 \tl_new:N \l_@@_mat_empty_tl
935 \tl_gset:Nn \l_@@_mat_empty_tl { 0 }
936 \phy@processkeyopt { diagmat }
937 \keys_define:nn { phy/diagmat }
938 {
939 empty .tl_set:N = \l_@@_mat_empty_tl ,
940 }
941 % \end{macrocode}
942 % \begin{function}{\diagmat}
943 % \begin{syntax}
944 % \cs{\meta{delimiter type}diagmat} \oarg{key-val list} \marg{diagonal}
945 % \end{syntax}
946 % \end{function}
947 % \begin{macrocode}
948 \DeclareDocumentCommand \diagmat { O{} m }
949 { \_@@_diagmat_type:nnn { } { #1 } { #2 } }
950 \DeclareDocumentCommand \pdiagmat { O{} m }
951 { \_@@_diagmat_type:nnn { p } { #1 } { #2 } }
952 \DeclareDocumentCommand \bdiagmat { O{} m }
953 { \_@@_diagmat_type:nnn { b } { #1 } { #2 } }
954 \DeclareDocumentCommand \Bdiagmat { O{} m }
955 { \_@@_diagmat_type:nnn { B } { #1 } { #2 } }
956 \DeclareDocumentCommand \vdiagmat { O{} m }
957 { \_@@_diagmat_type:nnn { v } { #1 } { #2 } }
958 \DeclareDocumentCommand \Vdiagmat { O{} m }
959 { \_@@_diagmat_type:nnn { V } { #1 } { #2 } }
960 % \end{macrocode}
961 % \begin{function}{\_@@_diagmat_type:nnn}
962 % \begin{syntax}
963 % \cs{_@@_diagmat_type:nnn} \marg{delimiter type} \marg{key-val list} \marg{diagonal}
964 % \end{syntax}
965 % \end{function}
966 % \begin{macrocode}
967 \cs_new:Npn \_@@_diagmat_type:nnn #1#2#3
968 {
969 \group_begin:
970 \clist_set:Nn \l_@@_mat_diag_clist { #3 }
971 \int_set:Nn \l_@@_mat_dim_int { \clist_count:N \l_@@_mat_diag_clist }
972 \int_compare:nNnT { \l_@@_mat_dim_int } > { \value { MaxMatrixCols } }
973 { \setcounter { MaxMatrixCols } { \l_@@_mat_dim_int } }
974 \keys_set:nn { phy/diagmat } { #2 }
975 \tl_gclear:N \l_@@_diagmat_tl
976 \int_step_inline:nnn { 1 } { \l_@@_mat_dim_int }
977 {
978 \int_step_inline:nnn { 1 } { \l_@@_mat_dim_int }
979 {
980 \int_compare:nNnTF { ##1 } = { ####1 }
981 {
982 \clist_gpop:NN \l_@@_mat_diag_clist \l_@@_tmpa_tl
983 \tl_if_empty:NTF \l_@@_tmpa_tl
984 { \tl_gput_right:Nn \l_@@_mat_line_tl { \l_@@_mat_empty_tl } }
985 % \end{macrocode}
986 % Maybe it's better to use \verb|\expandafter\scantokens\expandafter{\l_@@_tmpa_tl}| in the next line.
987 % \begin{macrocode}
988 { \tl_gput_right:No \l_@@_mat_line_tl { \l_@@_tmpa_tl } }
989 }
990 { \tl_gput_right:Nn \l_@@_mat_line_tl { \l_@@_mat_empty_tl } }
991 \int_compare:nNnTF { ####1 } = { \l_@@_mat_dim_int }
992 {
993 \tl_gput_right:Nn \l_@@_mat_line_tl { \\ }
994 }
995 {
996 \tl_gput_right:Nn \l_@@_mat_line_tl { & }
997 }
998 }
999 \tl_gput_right:No \l_@@_diagmat_tl { \l_@@_mat_line_tl }
1000 \tl_gclear:N \l_@@_mat_line_tl
1001 }
1002 \begin { #1 matrix }
1003 \tl_use:N \l_@@_diagmat_tl
1004 \end { #1 matrix }
1005 \group_end:
1006 }
1007 %</diagmat>
1008 % \end{macrocode}
1009 % \section{The \modu{xmat} module}
1010 % \begin{macrocode}
1011 %<*xmat>
1012 \ProvidesExplFile{phy-xmat.sty}{2023/10/24}{}
1013 {`xmat' module of physics2}
1014 \RequirePackage { amsmath }
1015 \phy@define@key { xmat } { showtop }
1016 { \int_gset:Nn \l_@@_xmat_showtop_int { #1 } }
1017 \phy@define@key { xmat } { showleft }
1018 { \int_gset:Nn \l_@@_xmat_showleft_int { #1 } }
1019 % \end{macrocode}
1020 % This module requires some new variables.
1021 % \begin{macrocode}
1022 \bool_new:N \l_@@_xmat_extra_vdots_bool
1023 \bool_new:N \l_@@_xmat_extra_cdots_bool
1024 \int_new:N \l_@@_xmat_showtop_int
1025 \int_new:N \l_@@_xmat_showleft_int
1026 \tl_new:N \l_@@_xmat_tl
1027 \int_gset:Nn \l_@@_xmat_showtop_int { \value { MaxMatrixCols } - 2 }
1028 \int_gset:Nn \l_@@_xmat_showleft_int { \value { MaxMatrixCols } - 2 }
1029 \cs_new:Npn \_@@_xmat_entry_format:nnn #1#2#3
1030 {
1031 #1 \c_math_subscript_token { #2 #3 }
1032 }
1033 \phy@processkeyopt { xmat }
1034 \DeclareDocumentCommand \xmat { O{} m m m }
1035 { \_@@_xmat_type:nnnnn { } { #1 } { #2 } { #3 } { #4 } }
1036 \DeclareDocumentCommand \pxmat { O{} m m m }
1037 { \_@@_xmat_type:nnnnn { p } { #1 } { #2 } { #3 } { #4 } }
1038 \DeclareDocumentCommand \bxmat { O{} m m m }
1039 { \_@@_xmat_type:nnnnn { b } { #1 } { #2 } { #3 } { #4 } }
1040 \DeclareDocumentCommand \Bxmat { O{} m m m }
1041 { \_@@_xmat_type:nnnnn { B } { #1 } { #2 } { #3 } { #4 } }
1042 \DeclareDocumentCommand \vxmat { O{} m m m }
1043 { \_@@_xmat_type:nnnnn { v } { #1 } { #2 } { #3 } { #4 } }
1044 \DeclareDocumentCommand \Vxmat { O{} m m m }
1045 { \_@@_xmat_type:nnnnn { V } { #1 } { #2 } { #3 } { #4 } }
1046 \keys_define:nn { phy/xmat }
1047 {
1048 format .cs_set:Np = \_@@_xmat_entry_format:nnn #1#2#3 ,
1049 showtop .int_set:N = \l_@@_xmat_showtop_int ,
1050 showleft.int_set:N = \l_@@_xmat_showleft_int ,
1051 }
1052 % \end{macrocode}
1053 % \begin{function}[pTF]{\_@@_if_digits_only:n}
1054 % \begin{syntax}
1055 % \cs{_@@_if_digits_only:nTF} \marg{token list} \marg{true code} \marg{flase code}
1056 % \end{syntax}
1057 % Use \LaTeX3 regular expression to tell if \meta{token list}
1058 % (the numbers of rows or columns) contain digits only.
1059 % \end{function}
1060 % \begin{macrocode}
1061 \prg_new_conditional:Npnn \_@@_if_digits_only:n #1 { TF }
1062 {
1063 \regex_match:nnTF { \A [[:digit:]]* \Z } { #1 }
1064 { \prg_return_true: } { \prg_return_false: }
1065 }
1066 % \end{macrocode}
1067 % \begin{function}{\_@@_xmat_type:nnnnn}
1068 % \begin{syntax}
1069 % \cs{_@@_xmat_type:nnnnn} \marg{delimiter type} \marg{key-val list} \marg{common entry} \marg{row number} \marg{column number}
1070 % \end{syntax}
1071 % \end{function}
1072 % \begin{macrocode}
1073 \cs_new:Npn \_@@_xmat_type:nnnnn #1#2#3#4#5
1074 {
1075 \group_begin:
1076 \tl_gclear:N \l_@@_xmat_tl
1077 \keys_set:nn { phy/xmat } { #2 } %
1078 \_@@_if_digits_only:nTF { #4 }
1079 {
1080 \int_compare:nNnTF { #4 } < { \l_@@_xmat_showtop_int + 1 }
1081 {
1082 \int_set:Nn \l_@@_xmat_showtop_int { #4 }
1083 \bool_set_false:N \l_@@_xmat_extra_vdots_bool
1084 }
1085 {
1086 \bool_set_true:N \l_@@_xmat_extra_vdots_bool
1087 }
1088 }
1089 {
1090 \bool_set_true:N \l_@@_xmat_extra_vdots_bool
1091 }
1092 \_@@_if_digits_only:nTF { #5 }
1093 {
1094 \int_compare:nNnTF { #5 } < { \l_@@_xmat_showleft_int + 1 }
1095 {
1096 \int_set:Nn \l_@@_xmat_showleft_int { #5 }
1097 \bool_set_false:N \l_@@_xmat_extra_cdots_bool
1098 }
1099 {
1100 \bool_set_true:N \l_@@_xmat_extra_cdots_bool
1101 }
1102 }
1103 {
1104 \bool_set_true:N \l_@@_xmat_extra_cdots_bool
1105 }
1106 \int_step_inline:nn { \l_@@_xmat_showtop_int }
1107 {
1108 \tl_put_right:Nn \l_@@_xmat_tl
1109 { \_@@_xmat_entry_format:nnn { #3 } { ##1 } { 1 } }
1110 \int_step_inline:nnn { 2 } { \l_@@_xmat_showleft_int }
1111 {
1112 \tl_put_right:Nn \l_@@_xmat_tl
1113 { & \_@@_xmat_entry_format:nnn { #3 } { ##1 } { ####1 } }
1114 }
1115 \bool_if:NT \l_@@_xmat_extra_cdots_bool
1116 {
1117 \tl_put_right:Nn \l_@@_xmat_tl
1118 { & \cdots & \_@@_xmat_entry_format:nnn { #3 } { ##1 } { #5 } }
1119 }
1120 \tl_put_right:Nn \l_@@_xmat_tl { \\ }
1121 }
1122 \bool_if:NT \l_@@_xmat_extra_vdots_bool
1123 {
1124 \tl_put_right:Nn \l_@@_xmat_tl { \vdots }
1125 \prg_replicate:nn { \l_@@_xmat_showleft_int - 1 }
1126 {
1127 \tl_put_right:Nn \l_@@_xmat_tl { & \vdots }
1128 }
1129 % Add \ddots if vdots_bool and cdots_bool be true simultaneously.
1130 \bool_if:NT \l_@@_xmat_extra_cdots_bool
1131 {
1132 \tl_put_right:Nn \l_@@_xmat_tl { & \ddots & \vdots }
1133 } % else relax
1134 \tl_put_right:Nn \l_@@_xmat_tl { \\ }
1135 % The last row.
1136 \tl_put_right:Nn \l_@@_xmat_tl
1137 { \_@@_xmat_entry_format:nnn { #3 } { #4 } { 1 } }
1138 \int_step_inline:nnn { 2 } { \l_@@_xmat_showleft_int }
1139 {
1140 \tl_put_right:Nn \l_@@_xmat_tl
1141 { & \_@@_xmat_entry_format:nnn { #3 } { #4 } { ##1 } }
1142 }
1143 \bool_if:NT \l_@@_xmat_extra_cdots_bool
1144 {
1145 \tl_put_right:Nn \l_@@_xmat_tl
1146 { & \cdots & \_@@_xmat_entry_format:nnn { #3 } { #4 } { #5 } }
1147 }
1148 } % else relax
1149 \begin { #1 matrix }
1150 \tl_use:N \l_@@_xmat_tl
1151 \end { #1 matrix }
1152 \group_end:
1153 }
1154 %</xmat>
1155 % \end{macrocode}
1156 % This part ends here.
1157 % \begin{macrocode}
1158 %<@@=>
1159 % \end{macrocode}
1160 %^^A******************************************************************************** [l2e legacy]
1161 % \part{Legacy modules written in \LaTeXe\ syntax}
1162 % \section{The \modu{ab.legacy} module}
1163 % \begin{macrocode}
1164 %<*ab.legacy>
1165 \ProvidesFile{phy-ab.legacy.sty}
1166 [2023/10/24 `ab.legacy' module of physics2]
1167 % \end{macrocode}
1168 % Requires \modu{ab}'s |tight| option.
1169 % \begin{macrocode}
1170 \phy@requiremodule{ab}
1171 \phy@define@key{ab.legacy}{order}[\mathcal{O}]{\def\phy@ab@ordersym{#1}}
1172 \phy@setkeys{ab.legacy}{order}
1173 \phy@processkeyopt{ab.legacy}
1174 \phy@d@l@geny\abs\vert\vert
1175 \phy@d@l@geny\norm\Vert\Vert
1176 \DeclareDocumentCommand\order{som}{%
1177 \phy@ab@ordersym
1178 \IfBooleanTF{#1}
1179 {(#3)}
1180 {\IfValueTF{#2}
1181 {\csname#2l\endcsname(#3\csname#2r\endcsname)}
1182 {\phy@abopen(#3\phy@abclose)}%
1183 }%
1184 }
1185 \phy@d@l@geny\eval.\vert
1186 \phy@d@l@geny\peval(\vert
1187 \phy@d@l@geny\beval[\vert
1188 %</ab.legacy>
1189 % \end{macrocode}
1190 % \section{The \modu{nabla.legacy} module}
1191 % \begin{macrocode}
1192 %<*nabla.legacy>
1193 \ProvidesFile{phy-nabla.legacy.sty}
1194 [2023/10/24 `nabla.legacy' module of physics2]
1195 \phy@requiremodule{ab}
1196 % \end{macrocode}
1197 % Requires \pkg{fixdif} version 2.$x$.
1198 % \begin{macrocode}
1199 \RequirePackage{fixdif}[2023/01/31]
1200 \letdif\phy@nl@nabla{nabla}
1201 \AtBeginDocument{\ifcsname div\endcsname\let\divsymbol\div\fi
1202 \DeclareRobustCommand\grad{\phy@nl@nabla\ab}%
1203 \DeclareRobustCommand\div{\phy@nl@nabla\cdot\ab}%
1204 \DeclareRobustCommand\curl{\phy@nl@nabla\times\ab}%
1205 \DeclareRobustCommand\laplacian{\phy@nl@nabla^2\ab}%
1206 }
1207 %</nabla.legacy>
1208 % \end{macrocode}
1209 % \section{The \modu{op.legacy} module}
1210 % \begin{macrocode}
1211 %<*op.legacy>
1212 \ProvidesFile{phy-op.legacy.sty}
1213 [2023/10/24 `op.legacy' module of physics2]
1214 \phy@define@key{op.lega}{ReIm}[true]{\def\phy@reserveda{#1}}
1215 \phy@define@key{op.lega}{PV}{\def\@phy@oplega@PV{#1}}
1216 \phy@define@key{op.lega}{pv}{\def\@phy@oplega@pv{#1}}
1217 \phy@setkeys{op.lega}{PV=\mathcal{P},pv={p.v.},ReIm=true}
1218 \phy@processkeyopt{ab}
1219 \DeclareRobustCommand\asin{\mathop{\operator@font asin}\nolimits}
1220 \DeclareRobustCommand\acos{\mathop{\operator@font acos}\nolimits}
1221 \DeclareRobustCommand\atan{\mathop{\operator@font atan}\nolimits}
1222 \DeclareRobustCommand\acsc{\mathop{\operator@font acsc}\nolimits}
1223 \DeclareRobustCommand\asec{\mathop{\operator@font asec}\nolimits}
1224 \DeclareRobustCommand\acot{\mathop{\operator@font acot}\nolimits}
1225 \DeclareRobustCommand\Tr{\mathop{\operator@font Tr}\nolimits}
1226 \DeclareRobustCommand\tr{\mathop{\operator@font tr}\nolimits}
1227 \DeclareRobustCommand\rank{\mathop{\operator@font rank}\nolimits}
1228 \DeclareRobustCommand\erf{\mathop{\operator@font erf}\nolimits}
1229 \DeclareRobustCommand\Res{\mathop{\operator@font Res}\nolimits}
1230 \DeclareRobustCommand\res{\mathop{\operator@font res}\nolimits}
1231 \DeclareRobustCommand\PV{\mathord{\@phy@oplega@PV}}
1232 \DeclareRobustCommand\pv{\mathop{\operator@font\@phy@oplega@pv{}}\nolimits}
1233 % \end{macrocode}
1234 % Restore \cs{Re} and \cs{Im} in \cs{Resymbol} and \cs{Imsymbol}. The \cs{AtBeginDocument}
1235 % hook is used for the compatibility of \pkg{unicode-math}.
1236 % \begin{macrocode}
1237 \ifx\phy@reserveda\phy@true
1238 \AtBeginDocument{%
1239 \let\Resymbol\Re%
1240 \let\Imsymbol\Im%
1241 \DeclareRobustCommand\Re{\mathop{\operator@font Re}\nolimits}%
1242 \DeclareRobustCommand\Im{\mathop{\operator@font Im}\nolimits}%
1243 }
1244 \fi
1245 %</op.legacy>
1246 % \end{macrocode}
1247 % \section{The \modu{qtext.legacy} module}
1248 % This module is written for the compatibility with the bad commands provided by
1249 % \pkg{physics} only. The commands in this module should NEVER be used!
1250 % \begin{macrocode}
1251 %<*qtext.legacy>
1252 \ProvidesFile{phy-qtext.legacy.sty}
1253 [2023/10/24 `qtext.legacy' module of physics2.sty]
1254 \RequirePackage{amstext}
1255 \def\phy@qtext@#1#2{#1\text{#2}\quad}
1256 \DeclareRobustCommand\qqtext{\@ifstar{\phy@qtext@{}}{\phy@qtext@\quad}}
1257 \DeclareRobustCommand\qq{\qqtext}
1258 \DeclareRobustCommand\qcomma{,\quad}
1259 \DeclareRobustCommand\qc{\qcomma}
1260 \DeclareRobustCommand\qcc{\@ifstar{\phy@qtext@{}{c.c}}{\phy@qtext@\quad{c.c}}}
1261 \def\phy@qtext@lega@gen@#1{%
1262 \expandafter\DeclareRobustCommand\csname q#1\endcsname%
1263 {\@ifstar{\phy@qtext@{}{#1}}{\phy@qtext@\quad{#1}}}}
1264 \phy@qtext@lega@gen@{if}
1265 \phy@qtext@lega@gen@{then}
1266 \phy@qtext@lega@gen@{else}
1267 \phy@qtext@lega@gen@{otherwise}
1268 \phy@qtext@lega@gen@{unless}
1269 \phy@qtext@lega@gen@{give}
1270 \phy@qtext@lega@gen@{using}
1271 \phy@qtext@lega@gen@{unless}
1272 \phy@qtext@lega@gen@{assume}
1273 \phy@qtext@lega@gen@{since}
1274 \phy@qtext@lega@gen@{let}
1275 \phy@qtext@lega@gen@{for}
1276 \phy@qtext@lega@gen@{all}
1277 \phy@qtext@lega@gen@{even}
1278 \phy@qtext@lega@gen@{odd}
1279 \phy@qtext@lega@gen@{integer}
1280 \phy@qtext@lega@gen@{and}
1281 \phy@qtext@lega@gen@{or}
1282 \phy@qtext@lega@gen@{as}
1283 \phy@qtext@lega@gen@{in}
1284 %</qtext.legacy>
1285 % \end{macrocode}
1286 %^^A******************************************************************************** [l3 legacy]
1287 % \part{Legacy modules written in \LaTeX3 syntax}
1288 % \begin{macrocode}
1289 %<@@=phy>
1290 % \end{macrocode}
1291 % \section{The \modu{bm-um.legacy} module}
1292 % \begin{macrocode}
1293 %<*bm-um.legacy>
1294 \ProvidesExplFile{phy-bm-um.legacy.sty}{2023/10/24}{}
1295 {`bm-um.legacy' module of physics2}
1296 \AtBeginDocument
1297 {
1298 \cs_if_exist:cF { symbf }
1299 {
1300 \PackageError { physics2 }
1301 {
1302 The ~ `bm-um.legacy' ~ module ~ requires ~
1303 `unicode-math' ~ package
1304 }
1305 {
1306 Have ~ you ~ used ~ `unicode-math' ~
1307 in ~ the ~ preamble?
1308 }
1309 }
1310 }
1311 \DeclareDocumentCommand \bm { m }
1312 {
1313 \mode_if_math:TF
1314 {
1315 \tl_if_head_eq_catcode:nNTF { #1 } A
1316 {
1317 \symbfit { #1 }
1318 }
1319 {
1320 \symbf { #1 }
1321 }
1322 }
1323 {
1324 \PackageError { physics2 }
1325 {
1326 The ~ \string\bm\space command ~ should ~ be ~
1327 used ~ in ~ math ~ mode ~ only. \MessageBreak
1328 This ~ is ~ an ~ error ~ from ~ `bm-um.legacy' ~ module
1329 }
1330 {
1331 Check ~ if ~ any ~ `\string\bm' ~ is ~ out ~
1332 of ~ math ~ mode.
1333 }
1334 }
1335 }
1336 %</bm-um.legacy>
1337 % \end{macrocode}
1338 % This part ends here.
1339 % \begin{macrocode}
1340 %<@@=>
1341 % \end{macrocode}
1342 \endinput