Debian Codegraph @

texmf-dist/source/latex/tensor/tensor.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: tensor.dtx
4 %%
5 % Copyright (C) 2023 Philip G. Ratcliffe <[email protected]>
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % http://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "tensor package" (the Work in LPPL)
15 % and all files in that package must be distributed together.
16 %
17 % The released version of this package is available from CTAN.
18 %
19 % This package has the LPPL maintenance status "maintained".
20 %
21 % The current maintainer of this package is Philip G. Ratcliffe.
22 %
23 % \fi
24 %
25 % \iffalse
26 %
27 %<package>\NeedsTeXFormat{LaTeX2e}[1995/12/01]
28 %<package>\ProvidesPackage{tensor}
29 %<package> [2023/07/18 v2.2 tensor indices package (PGR)]
30 %
31 %<*driver>
32 \documentclass{ltxdoc}
33 \usepackage{tensor}
34 \NewDocElement[macrolike=false]{Argument}{argument}
35 \NewDocElement[macrolike=false]{Option}{option}
36 %
37 \CodelineIndex
38 \EnableCrossrefs
39 \RecordChanges
40 \DoNotIndex{\let,\def,\gdef,\newcommand,\renewcommand}
41 \DoNotIndex{\if,\else,\fi,\ifx,\fi,\iffalse,\fi,\iftrue,\fi}
42 \DoNotIndex{\@empty,\expandafter,\makebox,\relax,\string,\the,\z@}
43 \DoNotIndex{\MessageBreak,\on@line}
44 %
45 \begin{document}
46 \DocInput{tensor.dtx}
47 \end{document}
48 %</driver>
49 % \fi
50 %
51 % \CheckSum{342}
52 %
53 % \CharacterTable
54 % {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
55 % 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
56 % Digits \0\1\2\3\4\5\6\7\8\9
57 % Exclamation \! Double quote \" Hash (number) \#
58 % Dollar \$ Percent \% Ampersand \&
59 % Acute accent \' Left paren \( Right paren \)
60 % Asterisk \* Plus \+ Comma \,
61 % Minus \- Point \. Solidus \/
62 % Colon \: Semicolon \; Less than \<
63 % Equals \= Greater than \> Question mark \?
64 % Commercial at \@ Left bracket \[ Backslash \\
65 % Right bracket \] Circumflex \^ Underscore \_
66 % Grave accent \` Left brace \{ Vertical bar \|
67 % Right brace \} Tilde \~}
68 %
69 %
70 % \changes{v1.0}{1996/06/03}{original version}
71 % \changes{v2.0}{2004/04/01}{extended \cs{tensor}, added \cs{indices} and
72 % \cs{nuclide}, substituted \cs{newcommand} with \cs{DeclareRobustCommand} in
73 % user commands, documented and packaged}
74 %
75 % \GetFileInfo{tensor.sty}
76 %
77 % \DoNotIndex{\CodelineIndex,\EnableCrossrefs,\RecordChanges}
78 %
79 % \title{%
80 % The \textsf{tensor}\relax
81 % \,\thanks{%
82 % Based on and extending the original package of the same name by Mike Piff
83 % (1996/06/03).}
84 % \space
85 % package for \LaTeX2e
86 % }
87 % \author{%
88 % Philip G. Ratcliffe\relax
89 % \,\thanks{E-mail: \textsf{[email protected]}}
90 % \\
91 % Dipartimento di Scienza e Alta Tecnologia
92 % \\
93 % Universit\`a degli Studi dell'Insubria---Como
94 % }
95 %
96 % \date{(\fileversion, last revision \filedate)}
97 %
98 % \maketitle
99 %
100 % \begin{abstract}
101 % This is a complete revision and extension of Mike Piff's original |tensor|
102 % package; it defines two commands for typesetting tensors with mixed upper and
103 % lower indices in which the correct horizontal spacing must be observed.
104 % Various forms of alignment are available and spaces may be replaced by dots
105 % or other symbols. Consistent preposing of indices is now made possible while
106 % backwards compatibility is maintained. A special-purpose command to typeset
107 % nuclides is also defined.
108 % \end{abstract}
109 %
110 % \section{Introduction}
111 %
112 % It is common in both physics and mathematics to use tensors with mixed upper
113 % and lower indices in which the relative horizontal positions and spacing are
114 % significant, for example
115 % \begin{displaymath}
116 % \Gamma\indices{^\mu_{\nu\rho}},\quad R\indices{^\mu_\nu^\rho_\sigma}
117 % \quad\mathrm{or}\quad
118 % \epsilon\indices{^\mu^\nu^\rho_\sigma}.
119 % \end{displaymath}
120 % The macros defined in this package automatically maintain consistent
121 % horizontal positioning. Another common need addressed is the
122 % preposing of upper and lower indices, as in
123 % \begin{displaymath}
124 % \tensor
125 % [_{\rm H}]
126 % {\langle q',t' \vert \mathcal{U}(t,t') \vert q,t\rangle}
127 % {_{\rm H}}
128 % \quad\mathrm{or}\quad
129 % \nuclide[14][6]{C}.
130 % \end{displaymath}
131 % Note the correct spacing of the pre-index H in the above example. It should
132 % also be noted that constant vertical positioning is maintained for lone
133 % indices; consider the following (examine carefully the last lower index $o$
134 % on the right):
135 % \begin{displaymath}
136 % {\rm M}\indices*{^o_o} \vert \tensor[_o]{{\rm M}}{}
137 % \quad\mbox{\emph{cf.}}\quad
138 % {\rm M}^o_o \vert _o{\rm M},
139 % \end{displaymath}
140 % where the former group was typeset using \cs{indices}, the latter using `|_|'
141 % and `|^|'.
142 %
143 % \section{Usage}
144 %
145 % Two robust math-mode commands, \cs{tensor} and \cs{indices}, are defined (the
146 % first of which remains backwards compatible with Mike Piff's original
147 % definition). A new, robust text- and math-mode command, \cs{nuclide}, is also
148 % defined specifically for typesetting nuclides, as in the above example.
149 %
150 % \subsection{User commands}
151 %
152 % \DescribeMacro{\indices}
153 % To produce a mathematical expression (typically a tensor) with mixed upper
154 % and lower indices, simply enter
155 % \meta{object}|\indices{|%
156 % |^|\meta{sup$_{\,1}$}|_|\meta{sub$_{\,1}$}%
157 % |^|\meta{sup$_{\,2}$}|_|\meta{sub$_{\,2}$}%
158 % \dots|}|.
159 % Thus, in math mode it is sufficient to type \emph{e.g.}
160 % \begin{center}
161 % |M\indices{^a_b^{cd}_e}|\quad to obtain\quad
162 % $M\indices{^a_b^{cd}_e}$.
163 % \end{center}
164 %
165 % \DescribeMacro{\tensor}
166 % This variant has been retained in a completely backwards compatible form
167 % while also being considerably extended; the syntax for the previous
168 % expression is |\tensor{M}{^a_b^{cd}_e}|, for which the resulting output is
169 % identical. The extended form of \cs{tensor} defined here has an optional
170 % argument for indices to be placed \emph{before} the tensor, thus:
171 % \begin{center}
172 % |\tensor[^a_b^c_d]{M}{^e_f^g_h}|\quad produces\quad
173 % $\tensor[^a_b^c_d]{M}{^e_f^g_h}$.
174 % \end{center}
175 % A fairly robust (if somewhat crude) attempt is made to ensure the correct
176 % spacing and skew of the preposed indices with respect to the tensor object
177 % itself.
178 %
179 % Note that also \cs{sb} and \cs{sp} may be used in place of `|_|' and `|^|'
180 % respectively for both the above macros.
181 %
182 % \DescribeMacro{\indices*}
183 % \DescribeMacro{\tensor*}
184 % These two macros have starred forms, which collapse the spacing (\emph{i.e.}
185 % return to standard form). While \cs{indices*} is clearly redundant (and is
186 % included merely for symmetry), \cs{tensor*} also \emph{right} justifies the
187 % \emph{pre}-index strings, so that \emph{e.g.} nuclides may be typeset as
188 % follows (though see below for a purpose-built command):
189 % \begin{center}
190 % |\tensor*[^{14}_6]{\mathrm{C}}{}|\quad produces\quad
191 % $\tensor*[^{14}_6]{\mathrm{C}}{}$.
192 % \end{center}
193 % For those familiar with the |amsmath| package, this is more-or-less a
194 % generalisation of (though \emph{not intended} as a substitute for) the
195 % \cs{sideset} command (which itself is \emph{only valid} for objects defined
196 % with \cs{mathop}). Note that to use \cs{tensor*} as a substitute for
197 % \cs{sideset}, it is necessary to insert a \cs{nolimits} command, thus:
198 % \begin{center}
199 % |\tensor*[^*_*]{\prod\nolimits}{^*_*}|\quad produces\quad
200 % $\displaystyle\tensor*[^*_*]{\prod\nolimits}{^*_*}$.
201 % \end{center}
202 % The output appears identical to that of |\sideset{_*^*}{_*^*}{\prod}|.
203 %
204 % \DescribeArgument{*}
205 % The \cs{indices*} and \cs{tensor*} forms \emph{alone}, allow a |*| to also be
206 % placed as the first entry in either index-list argument, causing alignment
207 % (\emph{left} justification) of the successive pairs of upper and lower
208 % indices. A warning is issued if a |*| appears in an argument string of either
209 % \emph{non}-starred commands. Thus,
210 % \begin{center}
211 % |\tensor*{M}{*^{i_1}_{m_1}^{i_2}_{m_2}^{i_3}_{m_3}^{i_4}_{m_4}}|
212 % \\[2ex]
213 % produces\hspace{\stretch{2}}
214 % $\tensor*{M}{*^{i_1}_{m_1}^{i_2}_{m_2}^{i_3}_{m_3}^{i_4}_{m_4}}$\quad
215 % (\emph{cf.} $M^{i_1i_2i_3i_4}_{m_1m_2m_3m_4}$).
216 % \hspace*{\stretch{2}}
217 % \end{center}
218 % Note that \emph{no warning} is issued for improper pairing of successive
219 % indices.
220 %
221 % \DescribeMacro{\indexmarker}
222 % In analogy with the |tensind| package, the command \cs{indexmarker} (by
223 % default empty) may redefined (using \cs{renewcommand}) to introduce a visible
224 % place marker for the index spaces (though not all |tensind| functionality is
225 % reproduced here); a simple possibility is
226 % \begin{center}
227 % \vspace*{-2ex}
228 % |\renewcommand\indexmarker{\cdot}|,
229 % \\
230 % after which,\hspace*{\stretch{2}}
231 % \\
232 % |\tensor{M}{^a_b^c_d}|
233 % \\[1ex]
234 % produces\hspace*{\stretch{2}}\null
235 % \\
236 % $\renewcommand\indexmarker{\cdot}\tensor{M}{^a_b^c_d}$\quad instead of\quad
237 % $\tensor{M}{^a_b^c_d}$\,.
238 % \end{center}
239 %
240 % \DescribeMacro{\nuclide}
241 % This command, available in both math and text modes, is defined with the same
242 % purpose and result as the \cs{isotope} command (from the package of the same
243 % name). The syntax is
244 % \begin{center}
245 % |\nuclide[|\meta{mass no.}|][|\meta{atomic no.}|]{|\meta{symbol}|}|.
246 % \end{center}
247 % Thus, the earlier example of $\nuclide[14][6]{C}$ is obtained with
248 % |\nuclide[14][6]{C}| while |\nuclide[4][2]{\alpha}| gives
249 % $\nuclide[4][2]{\alpha}$. As indicated by the square brackets, the
250 % \meta{mass no.} and \meta{atomic no.} arguments are optional. Note that there
251 % is a little more space (|1mu|) between the numbers and the chemical symbol
252 % than appears in the example constructed manually with \cs{tensor*}.
253 %
254 % All the above-defined commands may be used recursively, \emph{i.e.} a
255 % \cs{tensor} may occur as an index to another \cs{tensor} and should behave
256 % according to the current superscript--subscript level. The user commands are
257 % defined here as `robust'; they may thus appear as so-called moving arguments,
258 % \emph{i.e.} to \cs{caption}, \cs{section} \emph{etc}.
259 %
260 % \DescribeMacro{\nuclideFont}
261 % \DescribeMacro{\massnumFont}
262 % By default, the fonts used in \cs{nuclide} for the chemical symbol, mass and
263 % atomic numbers are \cs{mathrm}; \emph{i}.\emph{e}., \cs{nuclideFont} (for the
264 % chemical symbol) is initially defined as \cs{mathrm} and \cs{massnumFont}
265 % (for the mass/atomic numbers) as \cs{nuclideFont} (for backwards
266 % compatibility). This then now allows for independent font variation of the
267 % chemical symbol and mass/atomic numbers. Both macros may be reset with
268 % \cs{renewcommand} to \cs{mathsf}, \cs{mathbf}, \cs{mathtt} \emph{etc}., or
269 % simply \cs{relax} (this last for \cs{nuclideFont} has the chemical symbol
270 % font default to \cs{mathit} for correct spacing, while for \cs{massnumFont}
271 % the mass and atomic numbers revert to standard math font).
272 %
273 % \subsection{Package options}
274 %
275 % As of v2.2, the package includes four options relating to the vertical
276 % alignment of indices. \LaTeX's behaviour in this regard is not always optimal
277 % or what the user may desire. Consider the following output (constructed using
278 % `|_|' and `|^|'.):
279 % \begin{displaymath}
280 % \epsilon\indices{^\mu_\rho^\nu_{\tilde\lambda}} \;
281 % g\indices{^\mu^\nu} \,.
282 % \end{displaymath}
283 % While the indices within each single mathematical object are mutually
284 % vertically aligned correctly, between separate objects they may not be. This
285 % is because \LaTeX\ sets the baseline according to the height and depth of the
286 % given indices on a per-object basis. To obviate such behaviour, this package
287 % takes the simplest route of using \cs{smash} to hide the height and depth of
288 % each superscript and subscript string so that they are always set with the
289 % same baselines. This naturally leads to a somewhat cramped form (superscripts
290 % are set a little too low and subscripts high) and so a specially defined
291 % \cs{strut} is included, which slightly raises superscripts and lowers
292 % subscripts; by default, this is only implemented in displayed math, as the
293 % impact on inline text may be too disruptive.
294 %
295 % \DescribeOption{align}
296 % \DescribeOption{text}
297 % \DescribeOption{nosmash}
298 % \DescribeOption{nostrut}
299 % The options thus introduced are |align|, |text|, |nosmash| and |nostrut|. The
300 % first implements both \cs{smash} and a \cs{strut} as outlined above, with
301 % |text| extending the implementation of the \cs{strut} to inline text, while
302 % |nosmash| and |nostrut| cancel the single effects (using both entirely
303 % negates |align|). Option ordering is irrelevant and the last three are
304 % inoperative without the first.
305 %
306 % The desired effects are implemented via two internal commands, which may also
307 % be redefined by the user.
308 % \DescribeMacro{\tensorSmash}
309 % The first, \cs{tensorSmash}, is set equal to \cs{smash}, which then takes each
310 % index string as an argument.
311 % \DescribeMacro{\tensorStrut}
312 % The second, \cs{tensorStrut}, is set equal to the height of `|l|' and depth
313 % of `|j|' in the relevant font, (by default though only inside displayed math
314 % environments) and is appended to each \cs{smash}'ed index string.
315 %
316 % \subsection{Caveats}
317 %
318 % Grouping of multi-token indices should be performed as normal (\emph{i.e.}
319 % via enclosure within a brace pair |{|\,|}|). Moreover, owing to the method by
320 % which index strings are parsed, any index constructs such as |\mathrm{H}|
321 % must also be entirely enclosed in braces, thus: |\indices{_{\mathrm{H}}^x}|.
322 %
323 % Spacing is not guaranteed to always appear optimal, especially when between
324 % \emph{pre}-pended indices and the tensor object itself. Recall too that
325 % screen viewing often distorts small spaces owing to resolution effects.
326 %
327 % \subsection{External package requirements}
328 %
329 % No external packages are required or called.
330 %
331 % \subsection{Package conflicts}
332 %
333 % There are few conflicts with standard \LaTeX2e\ packages; a problem with the
334 % |color| package in the first version has now been corrected, as too a
335 % recently flagged problem with the |underscore| package.
336 %
337 % However, the macros defined here fail as arguments of \cs{bm} from the |bm|
338 % package (due to parsing conflicts) or, consequently, of macros defined by the
339 % \cs{maybebm} package. A work around for, say, a chapter or section header is
340 % \begin{center}
341 % |{\let\nuclideFont\maybebm \nuclide[4][2]{\textup{He}}}|,
342 % \end{center}
343 % which should render \nuclide[\mathbf4][\mathbf2]{\textbf{He}} in the header,
344 % but \nuclide[4][2]{\textup{He}} in the contents listing.
345 %
346 % \StopEventually{\PrintChanges\PrintIndex}
347 %
348 % \section{Implementation}
349 %
350 % \subsection{User options}
351 %
352 % First, the package options with their related \cs{if}\dots\ conditionals are
353 % defined and processed.
354 % \begin{macrocode}
355 \newif\iftnsr@Aln
356 \DeclareOption{align}{\tnsr@Alntrue}
357 \newif\iftnsr@Txt
358 \DeclareOption{text}{\tnsr@Txttrue}
359 \newif\iftnsr@Sma \tnsr@Smatrue
360 \DeclareOption{nosmash}{\tnsr@Smafalse}
361 \newif\iftnsr@Str \tnsr@Strtrue
362 \DeclareOption{nostrut}{\tnsr@Strfalse}
363 \ProcessOptions
364 % \end{macrocode}
365 %
366 % \subsection{User commands}
367 %
368 % The |tensor| package defines three basic user commands:
369 % \begin{macro}{\tensor}
370 % The first takes three possible arguments (an optional index string to be
371 % \emph{preposed}, the tensor object, the index string) and also has a starred
372 % form, which suppresses spacing (it is backwards compatible with Mike Piff's
373 % original version).
374 % \begin{macrocode}
375 \DeclareRobustCommand\tensor{%
376 \tnsr@Prp
377 \@ifstar{\tnsr@Spcfalse\tnsr@Aux}{\tnsr@Spctrue\tnsr@Aux}%
378 }
379 % \end{macrocode}
380 % \end{macro}
381 % \begin{macro}{\indices}
382 % The second is a `\emph{lightweight}' form, which is placed immediately
383 % \emph{following} the tensor object, takes just one argument (the index
384 % string) and also has a starred form (this form was \emph{not} however present
385 % in the original package).
386 % \changes{v2.1}{2004/12/20}{added starred form, for symmetry with
387 % \cs{tensor*}}
388 % \begin{macrocode}
389 \DeclareRobustCommand\indices{%
390 \tnsr@Prp
391 \@ifstar{\tnsr@Spcfalse\ndcs@Aux}{\tnsr@Spctrue\ndcs@Aux}%
392 }
393 % \end{macrocode}
394 % \end{macro}
395 %
396 % \begin{macro}{\nuclide}
397 % This additional new command takes one direct argument (an optional mass
398 % number) and two indirect arguments (an optional atomic number, the chemical
399 % symbol---these last two are handled by an auxiliary macro). Since usage is
400 % common in text, math mode is ensured.
401 % \begin{macrocode}
402 \DeclareRobustCommand\nuclide[1][]{%
403 \ncld@Mno{#1}%
404 \ncld@Aux
405 }
406 % \end{macrocode}
407 % \end{macro}
408 %
409 % \begin{macro}{\nuclideFont}
410 % \begin{macro}{\massnumFont}
411 % These set the fonts for \cs{nuclide}; the defaults are \cs{mathrm} for both
412 % \cs{nuclideFont} and \cs{massnumFont}. They may be redefined as \emph{e.g.}
413 % \changes{v2.2}{2023/07/18}{added independent mass/atomic-no.\ font control}
414 % \cs{mathsf}, \cs{mathbf}, \cs{mathtt}, \cs{mathit} \emph{etc}., or even
415 % simply \cs{relax} or \cs{renewcommand}\cs{nuclideFont}|{}|.
416 % \begin{macrocode}
417 \newcommand\nuclideFont{\mathrm}
418 \newcommand\massnumFont{\nuclideFont}
419 % \end{macrocode}
420 % \end{macro}
421 % \end{macro}
422 %
423 % \subsection{Internal token registers}
424 %
425 % \begin{macro}{\tnsr@Sps}
426 % \begin{macro}{\tnsr@Sbs}
427 % \begin{macro}{\tnsr@Spe}
428 % \begin{macro}{\tnsr@Sbe}
429 % The token registers that hold the upper and lower index strings, and the most
430 % recent upper and lower index elements respectively:
431 % \begin{macrocode}
432 \newtoks\tnsr@Sps
433 \newtoks\tnsr@Sbs
434 \newtoks\tnsr@Spe
435 \newtoks\tnsr@Sbe
436 % \end{macrocode}
437 % \end{macro}
438 % \end{macro}
439 % \end{macro}
440 % \end{macro}
441 %
442 % \begin{macro}{\ncld@Mno}
443 % This token register temporarily holds the mass number for \cs{nuclide}.
444 % \begin{macrocode}
445 \newtoks\ncld@Mno
446 % \end{macrocode}
447 % \end{macro}
448 %
449 % \subsection{Internal switches}
450 %
451 % \begin{macro}{\iftnsr@Spc}
452 % The switch to select or suppress index element spacing.
453 % \begin{macrocode}
454 \newif\iftnsr@Spc
455 % \end{macrocode}
456 % \end{macro}
457 %
458 % \subsection{Internal macros}
459 %
460 % \begin{macro}{\tnsr@Prp}
461 % \begin{macro}{\tnsr@Wrn}
462 % Here we simply reset token registers and the warning macro before commencing.
463 % \begin{macrocode}
464 \newcommand\tnsr@Wrn{}
465 \newcommand\tnsr@Prp{%
466 \tnsr@Sps{}%
467 \tnsr@Sbs{}%
468 \def\tnsr@Wrn{}
469 }
470 % \end{macrocode}
471 % \end{macro}
472 % \end{macro}
473 %
474 % \begin{macro}{\ndcs@Aux}
475 % This (lightweight) auxiliary macro for \cs{indices} takes one argument (an
476 % index string); it calls \cs{tnsr@Set}, prints the indices and then issues any
477 % warnings.
478 % \begin{macrocode}
479 \newcommand\ndcs@Aux[1]{%
480 \tnsr@Erx
481 \def\tnsr@Obj{}%
482 \tnsr@Set{#1}%
483 \tnsr@Fin
484 \tnsr@Wrn
485 }
486 % \end{macrocode}
487 % \end{macro}
488 %
489 % \begin{macro}{\tnsr@Aux}
490 % This auxiliary macro for \cs{tensor} takes three possible arguments (an
491 % optional pre-index string, the tensor object, the post-index string) and
492 % passes everything via \cs{mathpalette} to \cs{tnsr@Plt}.
493 % \begin{macrocode}
494 \newcommand\tnsr@Aux[3][]{%
495 \tnsr@Erx
496 \mathpalette{\tnsr@Plt{#1}{#3}}{#2}%
497 \tnsr@Wrn
498 }
499 % \end{macrocode}
500 % \end{macro}
501 %
502 % \begin{macro}{\tnsr@Plt}
503 % This takes four arguments (the pre-index string---may be empty, the
504 % post-index, the current math style, the tensor object) and calls
505 % \cs{tnsr@Set} separately for both pre- and post-index strings.
506 % \begin{macrocode}
507 \newcommand\tnsr@Plt[4]{%
508 \def\tnsr@Obj{#3#4}%
509 \def\tnsr@Tmp{#1}%
510 \ifx\tnsr@Tmp\@empty\else
511 \tnsr@Set{#1}%
512 \hphantom{{}\tnsr@Fin}%
513 \tnsr@Sps\expandafter{%
514 \expandafter\tnsr@Krn\expandafter{\the\tnsr@Sps}%
515 }%
516 \tnsr@Sbs\expandafter{%
517 \expandafter\tnsr@Krn\expandafter{\the\tnsr@Sbs}%
518 }%
519 \fi
520 \tnsr@Set{#2}%
521 #4\tnsr@Fin
522 }
523 % \end{macrocode}
524 % \end{macro}
525 %
526 % \begin{macro}{\tnsr@Set}
527 % This takes one argument (a pre- or post-index string) and starts processing.
528 % \begin{macrocode}
529 \newcommand\tnsr@Set[1]{%
530 \let\tnsr@Swx\relax
531 \tnsr@Pro#1\tnsr@Err
532 }
533 % \end{macrocode}
534 % \end{macro}
535 %
536 % \begin{macro}{\tnsr@Krn}
537 % This has one argument (a processed index string) and inserts the necessary
538 % offsets.
539 % \changes{v2.1}{2004/12/20}{slightly altered spacing}
540 % \begin{macrocode}
541 \newcommand\tnsr@Krn[1]{%
542 \settowidth\@tempdima{$\m@th\tnsr@Obj^{#1}\mkern-1mu$}%
543 \kern-\@tempdima
544 #1
545 \settowidth\@tempdima{$\m@th\tnsr@Obj$}%
546 \kern\@tempdima
547 }
548 % \end{macrocode}
549 % \end{macro}
550 %
551 % \begin{macro}{\tnsr@Pro}
552 % This is the index-string processing macro, it takes one argument (an index
553 % string):
554 % \changes{v2.2}{2023/07/18}{substituted \cs{ifx} with \cs{if} to avoid
555 % \textsf{underscore} package conflict}
556 % \begin{macrocode}
557 \newcommand\tnsr@Pro[1]{%
558 \ifx#1\tnsr@Err
559 \let\tnsr@Nxt\relax
560 \else
561 \if#1*
562 \iftnsr@Spc
563 \gdef\tnsr@Wrn{%
564 \PackageWarning{tensor}{%
565 '*' not allowed in argument here; I shall ignore it.%
566 \MessageBreak Either remove it or use '\string\tensor*'%
567 }%
568 }%
569 \else
570 \let\tnsr@Swx\tnsr@Swa
571 \fi
572 \let\tnsr@Nxt\tnsr@Pro
573 \else
574 \if#1^
575 \def\tnsr@Nxt{\tnsr@Add{\tnsr@Sps}{\tnsr@Sbs}{\tnsr@Spe}}%
576 \else
577 \if#1_
578 \def\tnsr@Nxt{\tnsr@Add{\tnsr@Sbs}{\tnsr@Sps}{\tnsr@Sbe}}%
579 \else
580 \tnsr@Err
581 \let\tnsr@Nxt\tnsr@Pro
582 \fi
583 \fi
584 \fi
585 \fi
586 \tnsr@Nxt
587 }
588 % \end{macrocode}
589 % \end{macro}
590 %
591 % \begin{macro}{\tnsr@Swa}
592 % Here we flip the state of \cs{tnsr@Swx} to \cs{tnsr@Swb}.
593 % \begin{macrocode}
594 \newcommand\tnsr@Swa{\let\tnsr@Swx\tnsr@Swb}
595 % \end{macrocode}
596 % \end{macro}
597 %
598 % \begin{macro}{\tnsr@Swb}
599 % Here we flip the state of \cs{tnsr@Swx} to \cs{tnsr@Swa} then calculate and
600 % insert the necessary padding for horizontal index alignment.
601 % \begin{macrocode}
602 \newcommand\tnsr@Swb{%
603 \let\tnsr@Swx\tnsr@Swa
604 \settowidth\@tempdima{$\m@th\tnsr@Obj{}^{\the\tnsr@Spe}$}%
605 \settowidth\@tempdimb{$\m@th\tnsr@Obj{}_{\the\tnsr@Sbe}$}%
606 \addtolength\@tempdima{-\@tempdimb}%
607 \ifdim\@tempdima=\z@\else
608 \ifdim\@tempdima>\z@
609 \tnsr@Sbs\expandafter\expandafter\expandafter{%
610 \expandafter\the\expandafter\tnsr@Sbs
611 \expandafter\kern\the\@tempdima
612 }%
613 \else
614 \@tempdima=-\@tempdima
615 \tnsr@Sps\expandafter\expandafter\expandafter{%
616 \expandafter\the\expandafter\tnsr@Sps
617 \expandafter\kern\the\@tempdima
618 }%
619 \fi
620 \fi
621 }
622 % \end{macrocode}
623 % \end{macro}
624 %
625 % \begin{macro}{\tnsr@Add}
626 % This macro takes four arguments (the token-register target for the next index
627 % token, the token-register target for the phantom element, the token-register
628 % target for the most-recent element, the next index token). It adds the next
629 % index token to the upper or lower string and (if spacing is \emph{on}) a
630 % place-holder (\cs{tnsr@Hph}) of the same size to the lower or upper string,
631 % respectively. It also calls \cs{tnsr@Swx} to flip state (if activated). The
632 % use of \cs{leavevmode} avoids conflict with the |color| package.
633 % \changes{v2.1}{2004/12/20}{added \cs{leavevmode}, to avoid \textsf{color}
634 % package conflict}
635 % \begin{macrocode}
636 \newcommand\tnsr@Add[4]{%
637 #1\expandafter{\the#1\leavevmode{#4}}%
638 \iftnsr@Spc
639 #2\expandafter{\the#2\tnsr@Hph{#4}}%
640 \fi
641 #3{\leavevmode{#4}}%
642 \tnsr@Swx
643 \tnsr@Pro
644 }
645 % \end{macrocode}
646 % \end{macro}
647 %
648 % \begin{macro}{\tnsr@Hph}
649 % The place-holder macro, uses \cs{mathpalette} to call the contents
650 % \cs{tnsr@Mph}:
651 % \begin{macrocode}
652 \newcommand\tnsr@Hph{\expandafter\mathpalette\expandafter\tnsr@Mph}
653 % \end{macrocode}
654 % \end{macro}
655 %
656 % \begin{macro}{\tnsr@Mph}
657 % The place-holder macro contents:
658 % \changes{v2.1}{2004/12/20}{substituted \cs{hbox} with \cs{makebox}}
659 % \begin{macrocode}
660 \newcommand\tnsr@Mph[2]{%
661 \settowidth\@tempdima{$\m@th#1{#2}$}%
662 \makebox[\@tempdima][c]{$\m@th#1\indexmarker$}%
663 }
664 % \end{macrocode}
665 % \end{macro}
666 %
667 % \begin{macro}{\indexmarker}
668 % The default (blank) placeholder for index spacing:
669 % \changes{v2.1}{2004/12/20}{added capability to insert place holders}
670 % \begin{macrocode}
671 \newcommand\indexmarker{}
672 % \end{macrocode}
673 % \end{macro}
674 %
675 % \begin{macro}{\tnsr@Fin}
676 % Finally, we put the index strings into place:
677 % \changes{v2.2}{2023/07/18}{added vertical alignment capability}
678 % \begin{macrocode}
679 \newcommand\tnsr@Fin{%
680 ^{\tensorSmash{\the\tnsr@Sps}\tnsr@Str}%
681 _{\tensorSmash{\the\tnsr@Sbs}\tnsr@Str}%
682 }
683 % \end{macrocode}
684 % \end{macro}
685 % \begin{macro}{\tensorSmash}
686 % Initialise \cs{tensorSmash} as \cs{relax} and then conditionally set it equal
687 % to \cs{smash} (it is user redefinable).
688 % \begin{macrocode}
689 \let\tensorSmash\relax
690 \iftnsr@Aln
691 \iftnsr@Sma
692 \let\tensorSmash\smash
693 \fi
694 \fi
695 % \end{macrocode}
696 % \end{macro}
697 % \begin{macro}{\tensorStrut}
698 % \begin{macro}{\tnsr@Str}
699 % Initialise \cs{tensorStrut} as \cs{relax} and then conditionally set it to
700 % the height and depth of `|jl|'. By default, it is only applied to displayed
701 % math environments (passed on via \cs{tnsr@Str}, which is \cs{def}'ed as
702 % \cs{tensorStrut} to be user redefinable), but always (\emph{i.e.} extended to
703 % inline text) if the package option |text| is present.
704 % \begin{macrocode}
705 \newcommand\tensorStrut{}
706 \let\tnsr@Str\relax
707 \iftnsr@Aln
708 \iftnsr@Str
709 \renewcommand\tensorStrut{\vphantom{jl}}
710 \iftnsr@Txt
711 \def\tnsr@Str{\tensorStrut}
712 \else
713 \everydisplay\expandafter{\the\everydisplay\let\tnsr@Str\tensorStrut}
714 \fi
715 \fi
716 \fi
717 % \end{macrocode}
718 % \end{macro}
719 % \end{macro}
720 %
721 % \begin{macro}{\ncld@Aux}
722 % This auxiliary macro takes two arguments (an optional atomic number and the
723 % chemical symbol). The mass number is passed on via \cs{ncld@Mno}. Math mode
724 % is ensured since usage is common in text. The spacing is increased by |1mu|
725 % for better appearance.
726 % \begin{macrocode}
727 \newcommand\ncld@Aux[2][]{%
728 \ensuremath{%
729 \tensor*[^{\massnumFont{\the\ncld@Mno}}_{\massnumFont{#1}}]%
730 {\mkern1mu{\mathit{\nuclideFont{#2}}{}}}{}%
731 }%
732 }
733 % \end{macrocode}
734 % \end{macro}
735 %
736 % \begin{macro}{\tnsr@Err}
737 % This is invoked in the only error situations considered.
738 % \begin{macrocode}
739 \newcommand\tnsr@Err{}
740 \newcommand\tnsr@Erx{%
741 \def\tnsr@Err{%
742 \global\let\tnsr@Err\relax
743 \PackageError{tensor}{%
744 Misordered sub/superscript items\on@line;
745 \MessageBreak index tokens may have been lost.
746 \MessageBreak Press <return> and I shall try to continue%
747 }{Index string probably has extra/missing '^' or '_'.}%
748 }%
749 }
750 % \end{macrocode}
751 % \end{macro}
752 %
753 % \Finale
754 %
755 \endinput
756 %%
757 %% End of file 'tensor.dtx'.