| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% Copyright (C) 2016 by Joseph Rabinoff <[email protected]> |
| 4 |
% ------------------------------------------------------- |
| 5 |
% |
| 6 |
% This file may be distributed and/or modified under the |
| 7 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 8 |
% of this license or (at your option) any later version. |
| 9 |
% The latest version of this license is in: |
| 10 |
% |
| 11 |
% http://www.latex-project.org/lppl.txt |
| 12 |
% |
| 13 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 14 |
% version 2005/12/01 or later. |
| 15 |
% |
| 16 |
% \fi |
| 17 |
% |
| 18 |
% \iffalse |
| 19 |
%<*driver> |
| 20 |
\ProvidesFile{spalign.dtx} |
| 21 |
%</driver> |
| 22 |
%<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01] |
| 23 |
%<package>\ProvidesPackage{spalign} |
| 24 |
%<*package> |
| 25 |
[2016/10/05 aligns delimited by spaces] |
| 26 |
%</package> |
| 27 |
% |
| 28 |
%<*driver> |
| 29 |
\documentclass{ltxdoc} |
| 30 |
\usepackage{spalign}[2016/10/05] |
| 31 |
\usepackage[charter,sfscaled,ttscaled,cal=cmcal]{mathdesign} |
| 32 |
\usepackage{amsmath} |
| 33 |
|
| 34 |
\def\usage#1#2{\textbf{Usage:} \texttt{\string#1} #2\vadjust{\vskip 1ex}\\} |
| 35 |
|
| 36 |
\newdimen\poptskip\poptskip=1.3cm |
| 37 |
\advance\poptskip by 1ex |
| 38 |
\def\poption #1:{% |
| 39 |
\hangindent=\poptskip\hangafter=1% |
| 40 |
\noindent\hbox to 1.3cm{\hfil\bf#1:}\hskip 1ex% |
| 41 |
} |
| 42 |
\def\endopt{\par\vskip2pt} |
| 43 |
|
| 44 |
\let\tvs=\textvisiblespace |
| 45 |
|
| 46 |
\EnableCrossrefs |
| 47 |
\CodelineIndex |
| 48 |
\RecordChanges |
| 49 |
\begin{document} |
| 50 |
\DocInput{spalign.dtx} |
| 51 |
\PrintChanges |
| 52 |
\PrintIndex |
| 53 |
\end{document} |
| 54 |
%</driver> |
| 55 |
% \fi |
| 56 |
% |
| 57 |
% \CharacterTable |
| 58 |
% {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 |
| 59 |
% 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 |
| 60 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 61 |
% Exclamation \! Double quote \" Hash (number) \# |
| 62 |
% Dollar \$ Percent \% Ampersand \& |
| 63 |
% Acute accent \' Left paren \( Right paren \) |
| 64 |
% Asterisk \* Plus \+ Comma \, |
| 65 |
% Minus \- Point \. Solidus \/ |
| 66 |
% Colon \: Semicolon \; Less than \< |
| 67 |
% Equals \= Greater than \> Question mark \? |
| 68 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 69 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 70 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 71 |
% Right brace \} Tilde \~} |
| 72 |
% |
| 73 |
% |
| 74 |
% \changes{v1.0}{2016/10/05}{Initial version} |
| 75 |
% |
| 76 |
% \GetFileInfo{spalign.dtx} |
| 77 |
% |
| 78 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 79 |
% |
| 80 |
% |
| 81 |
% \title{The \textsf{spalign} package\thanks{This document |
| 82 |
% corresponds to the version of \textsf{spalign} dated \filedate.}} |
| 83 |
% \author{Joseph Rabinoff \\ \texttt{[email protected]}} |
| 84 |
% |
| 85 |
% \maketitle |
| 86 |
% |
| 87 |
% \section{Introduction} |
| 88 |
% |
| 89 |
% The purpose of this package is to decrease the number of keystrokes needed to |
| 90 |
% typeset small amounts of aligned material (matrices, arrays, etc.). For |
| 91 |
% instance, it is inconvenient to type (using the \textsf{amsmath} package) |
| 92 |
% \begin{verbatim} |
| 93 |
% \[ \begin{matrix} |
| 94 |
% 1 & 12 & -3 \\ |
| 95 |
% 24 & -2 & 2 \\ |
| 96 |
% 0 & 0 & 1 |
| 97 |
% \end{matrix} \]\end{verbatim} |
| 98 |
% in a document where several hundred such |
| 99 |
% matrices must be typeset. Of course |
| 100 |
% one can always define a macro |\mat| which puts its argument inside a |
| 101 |
% |matrix| environment, but it is still necessary to type the align character |
| 102 |
% |&| and the end-of-row control sequence |\\| many times for each matrix. |
| 103 |
% |
| 104 |
% This package provides a facility for typesetting matrices, and using other |
| 105 |
% alignment environments and macros, with spaces as the |
| 106 |
% alignment delimiter and semicolons (by default) as the end-of-row indicator. So |
| 107 |
% the above matrix could be produced using the command: |
| 108 |
% \begin{verbatim} |
| 109 |
% \[ \spalignmat{1 12 -3; 24 -2 2; 0 0 1} \]\end{verbatim} |
| 110 |
% This package also contains utility macros for typesetting augmented matrices, |
| 111 |
% vectors, arrays, and more, and is easily extendable to other situations that use |
| 112 |
% alignments. |
| 113 |
% |
| 114 |
% \section{Usage} |
| 115 |
% |
| 116 |
% In~\S\ref{sec:alignment} the simplified alignment format used by arguments to |
| 117 |
% macros in this package is described. In~\S\ref{sec:utility} the utility |
| 118 |
% macros provided by the \textsf{spalign} package are presented; these are the |
| 119 |
% macros that most users will need. Section~\ref{sec:options} contains the |
| 120 |
% package options. The macros designed to allow the user to adapt the |
| 121 |
% \textsf{spalign} package to other situations are presented |
| 122 |
% in~\S\ref{sec:general}. |
| 123 |
% |
| 124 |
% \subsection{Alignment format}\label{sec:alignment} |
| 125 |
% The core functionality of the \textsf{spalign} package is to convert spaces to |
| 126 |
% alignment characters `|&|' and semicolons to end-of-row control sequences |
| 127 |
% `|\\|'. This process is called \textit{retokenization}. The retokenization |
| 128 |
% procedure is designed to work more or less how one would expect. |
| 129 |
% Retokenization of a string \meta{text} proceeds as follows. |
| 130 |
% \begin{enumerate} |
| 131 |
% \item Spaces at the beginning and end of \meta{text} are ignored, as are |
| 132 |
% spaces at the beginning and end of a line, and spaces before and after a |
| 133 |
% semicolon or a comma. |
| 134 |
% \item Multiple spaces are combined into one space. |
| 135 |
% \item Spaces between non-space characters are converted to |&| (really, to |
| 136 |
% the contents of |\spalignaligntab|). |
| 137 |
% \item Commas (really, the contents of |\spalignseparator|) are also converted to |
| 138 |
% |&|. |
| 139 |
% \item Semicolons (really, the contents of |\spalignendofrow|) are |
| 140 |
% converted to |\\| (really, to the contents of |\spalignendline|). |
| 141 |
% \item Text in braces |{...}| is treated as a unit: the contents are not |
| 142 |
% retokenized, and the braces are preserved. |
| 143 |
% \end{enumerate} |
| 144 |
% These rules are best understood by example. |
| 145 |
% |
| 146 |
% \medskip\noindent\textbf{Example.} The command |
| 147 |
% \begin{verbatim} |
| 148 |
% \[ \spalignmat{ 1 |
| 149 |
% -2 3 ; 4 |
| 150 |
% 55 2^3; |
| 151 |
% \frac{1 1}2 {1 |
| 152 |
% 3} {1 0 1} } \]\end{verbatim} |
| 153 |
% produces |
| 154 |
% \[\spalignmat{ 1 |
| 155 |
% -2 3 ; 4 |
| 156 |
% 55 2^3 ; |
| 157 |
% \frac{1 1}2 {1 |
| 158 |
% 3} {1 0 1} }. |
| 159 |
% \] |
| 160 |
% |
| 161 |
% \medskip\noindent\textbf{Example.} The command |
| 162 |
% \begin{verbatim} |
| 163 |
% \[ \spalignmat{ \cos\theta, \sin\theta; |
| 164 |
% -\sin\theta, \cos\theta} \]\end{verbatim} |
| 165 |
% produces |
| 166 |
% \[\spalignmat{ \cos\theta, \sin\theta; |
| 167 |
% -\sin\theta, \cos\theta}. |
| 168 |
% \] |
| 169 |
% Here the commas are necessary: \TeX\ will not tokenize spaces following a |
| 170 |
% command sequence, so |
| 171 |
% \begin{verbatim} |
| 172 |
% \[ \spalignmat{ \cos\theta \sin\theta; |
| 173 |
% -\sin\theta \cos\theta} \]\end{verbatim} |
| 174 |
% produces the (presumably unexpected) result |
| 175 |
% \[\spalignmat{ \cos\theta \sin\theta; |
| 176 |
% -\sin\theta \cos\theta}. |
| 177 |
% \] |
| 178 |
% Instead of commas, one could also type, for instance, |
| 179 |
% \begin{verbatim} |
| 180 |
% \[ \spalignmat{ \cos\theta{} \sin\theta; |
| 181 |
% -\sin{\theta} \cos\theta} \]\end{verbatim} |
| 182 |
% |
| 183 |
% \medskip\noindent\textbf{Example.} The fact that expressions between braces |
| 184 |
% |{...}| are not retokenized allows for arbitrarily complex entries in |
| 185 |
% \textsf{spalign} macros---although these macros are probably not terribly |
| 186 |
% useful in such cases. The \textsf{spalign} macros can even be nested: for |
| 187 |
% instance, |
| 188 |
% \begin{verbatim} |
| 189 |
% \[ \spalignmat{ \spalignmat{a b; c d} \spalignmat{a' b'; c' d'}; |
| 190 |
% \spalignmat{d e; f g} \spalignmat{d' e'; f' g'} } \] \end{verbatim} |
| 191 |
% produces |
| 192 |
% \[ \spalignmat{ \spalignmat{a b; c d} \spalignmat{a' b'; c' d'}; |
| 193 |
% \spalignmat{d e; f g} \spalignmat{d' e'; f' g'} }. \] |
| 194 |
% |
| 195 |
% |
| 196 |
% \subsection{Utility macros}\label{sec:utility} |
| 197 |
% Most math-mode utility macros use the |array| environment internally. This |
| 198 |
% can be the vanilla \LaTeX\ |array| environment, or the one from the |
| 199 |
% \textsf{array} package; it makes no difference. All math-mode utility macros |
| 200 |
% have an un-starred version and a starred version. The un-starred version |
| 201 |
% produces arrays with the delimiters defined by the package options |
| 202 |
% (see~\ref{sec:options}), and the starred version omits the delimiters (and the |
| 203 |
% glue between the delimiters and the array). |
| 204 |
% |
| 205 |
% As the only purpose of this package is to save keystrokes, the user may want |
| 206 |
% to put |
| 207 |
% \begin{verbatim} |
| 208 |
% \let\mat=\spalignmat |
| 209 |
% \let\amat=\spalignaugmat |
| 210 |
% \let\vec=\spalignvector\end{verbatim} |
| 211 |
% or something similar after |\usepackage{spalign}|. Note that |\mat*| will now |
| 212 |
% also be synonymous with |\spalignmat*|, etc. |
| 213 |
% |
| 214 |
% \bigskip |
| 215 |
% \noindent |
| 216 |
% \DescribeMacro{\spalignarray} |
| 217 |
% \DescribeMacro{\spalignarray*} |
| 218 |
% \usage\spalignarray{\marg{alignment specifier}\marg{text}} |
| 219 |
% Produces a (potentially delimited) |array| environment, passing it |
| 220 |
% \meta{alignment specifier}, after retokenizing \meta{text}. This is exactly |
| 221 |
% like the matrix environments below, except that it is possible to specify the |
| 222 |
% alignment of each column separately, add vertical bars, etc. |
| 223 |
% For example, |
| 224 |
% \begin{verbatim} |
| 225 |
% \[ \spalignarray{l|c|r}{1 1 1; 100 100 100} \]\end{verbatim} |
| 226 |
% produces |
| 227 |
% \[ \DeleteShortVerb{\|} |
| 228 |
% \spalignarray{l|c|r}{1 1 1; 100 100 100}. |
| 229 |
% \MakeShortVerb{\|} |
| 230 |
% \] |
| 231 |
% Note that |\spalignarray*| simply produces an |array| environment surrounding |
| 232 |
% the retokenized \meta{text}. |
| 233 |
% |
| 234 |
% \bigskip |
| 235 |
% \noindent |
| 236 |
% \DescribeMacro{\spalignmat} |
| 237 |
% \DescribeMacro{\spalignmat*} |
| 238 |
% \usage\spalignmat{\oarg{column alignment}\marg{text}} |
| 239 |
% Produces a matrix whose columns are aligned according to the |
| 240 |
% \meta{column alignment}, after retokenizing \meta{text}. The \meta{column alignment} |
| 241 |
% is an array environment alignment specifier for a single column (usually |l|, |
| 242 |
% |c|, or |r|), which is used for each column. The default is |c|. For example, |
| 243 |
% \begin{verbatim} |
| 244 |
% \[ \spalignmat[l]{1 1 1; 100 100 100} \]\end{verbatim} |
| 245 |
% produces |
| 246 |
% \[ \spalignmat[l]{1 1 1; 100 100 100}. \] |
| 247 |
% |
| 248 |
% \bigskip |
| 249 |
% \noindent |
| 250 |
% \DescribeMacro{\spalignvector} |
| 251 |
% \DescribeMacro{\spalignvector*} |
| 252 |
% \usage\spalignvector{\oarg{column alignment}\marg{text}} |
| 253 |
% Produces a matrix with one column. Spaces, commas, and semicolons are all |
| 254 |
% retokenized to the end-of-row control sequence `|\\|'. The |
| 255 |
% \meta{column alignment} is interpreted as in |\spalignmat|; the default is |
| 256 |
% |c|. For example, |
| 257 |
% \begin{verbatim} |
| 258 |
% \[ \spalignvector[r]{1 100 1000} \]\end{verbatim} |
| 259 |
% produces |
| 260 |
% \[ \spalignvector[r]{1 100 1000}. \] |
| 261 |
% |
| 262 |
% \bigskip |
| 263 |
% \noindent |
| 264 |
% \DescribeMacro{\spalignaugmatn} |
| 265 |
% \DescribeMacro{\spalignaugmatn*} |
| 266 |
% \usage\spalignaugmatn{\oarg{column alignment}\marg{augmented columns}\marg{text}} |
| 267 |
% Produces a matrix with a vertical divider located \meta{augmented columns} from the |
| 268 |
% right side of the matrix. The \meta{column alignment} is interpreted as in |
| 269 |
% |\spalignmat|; the default is |r|. For example, |
| 270 |
% \begin{verbatim} |
| 271 |
% \[ \spalignaugmatn[c]{3}{1 2 3 4; 10 20 30 40} \]\end{verbatim} |
| 272 |
% produces |
| 273 |
% \[ \spalignaugmatn[c]{3}{1 2 3 4; 10 20 30 40}. \] |
| 274 |
% |
| 275 |
% \bigskip |
| 276 |
% \noindent |
| 277 |
% \DescribeMacro{\spalignaugmat} |
| 278 |
% \DescribeMacro{\spalignaugmat*} |
| 279 |
% \usage\spalignaugmat{\oarg{column alignment}\marg{text}} |
| 280 |
% This is the same as |\spalignaugmatn|, with \meta{augmented columns} equal to |
| 281 |
% $1$. For example, |
| 282 |
% \begin{verbatim} |
| 283 |
% \[ \spalignaugmat{1 2 3 4; 10 20 30 40} \]\end{verbatim} |
| 284 |
% produces |
| 285 |
% \[ \spalignaugmat{1 2 3 4; 10 20 30 40}. \] |
| 286 |
% |
| 287 |
% \bigskip |
| 288 |
% \noindent |
| 289 |
% \DescribeMacro{\spalignaugmathalf} |
| 290 |
% \DescribeMacro{\spalignaugmathalf*} |
| 291 |
% \usage\spalignaugmathalf{\oarg{column alignment}\marg{text}} |
| 292 |
% This is the same as |\spalignaugmatn|, with \meta{augmented columns} equal to |
| 293 |
% the largest integer less or equal to half of the total number of columns |
| 294 |
% parsed from \meta{text}. For example, |
| 295 |
% \begin{verbatim} |
| 296 |
% \[ \spalignaugmathalf[l]{1 2 3 4; 10 20 30 40} \]\end{verbatim} |
| 297 |
% produces |
| 298 |
% \[ \spalignaugmathalf[l]{1 2 3 4; 10 20 30 40}. \] |
| 299 |
% |
| 300 |
% \bigskip |
| 301 |
% \noindent |
| 302 |
% \DescribeMacro{\spalignsys} |
| 303 |
% \DescribeMacro{\spalignsys*} |
| 304 |
% \usage\spalignsys{\marg{text}} |
| 305 |
% Typesets systems of simple equations so that binary operators and relations |
| 306 |
% are aligned vertically, and variables are right-justified. This macro assumes |
| 307 |
% that variables are in odd columns and that binary operators and relations are |
| 308 |
% in even columns. For example, |
| 309 |
% \begin{verbatim} |
| 310 |
% \[ \spalignsys{2x + y = 4; x - 3y = -17} \]\end{verbatim} |
| 311 |
% produces |
| 312 |
% \[ \spalignsys{2x + y = 4; x - 3y = -17\rlap.} \] |
| 313 |
% Within \marg{text} the macro |\+| is defined to be an empty box with the size |
| 314 |
% and spacing of a binary operator, the macro |\=| is defined to be an empty box |
| 315 |
% with the size and spacing of a relation, and the macro |\.| is defined to be |
| 316 |
% empty. (Ordinarily, the latter two macros produce th\=ese acc\.ents, but they |
| 317 |
% cannot be used in math mode.) This allows one to deal with empty columns in |
| 318 |
% an easy-to-read way: for example, |
| 319 |
% \begin{verbatim} |
| 320 |
% \[ \spalignsys{ |
| 321 |
% 2x \+ \. - 3z = 1; |
| 322 |
% \. \+ 4y + z = -4} |
| 323 |
% \]\end{verbatim} |
| 324 |
% produces |
| 325 |
% \[ \spalignsys{ |
| 326 |
% 2x \+ \. - 3z = 1; |
| 327 |
% \. \+ 4y + z = -4\rlap.} |
| 328 |
% \] |
| 329 |
% As with the matrix macros, delimiters can be changed with the package options. |
| 330 |
% |
| 331 |
% \bigskip |
| 332 |
% \noindent |
| 333 |
% \DescribeMacro{\spaligntabular} |
| 334 |
% \usage\spaligntabular{\marg{alignment specifier}\marg{text}} |
| 335 |
% Produces an (undelimited) |tabular| environment, passing it |
| 336 |
% \meta{alignment specifier}, after retokenizing \meta{text}. This macro may be |
| 337 |
% used outside of math mode, and therefore is undelimited. |
| 338 |
% For example, |
| 339 |
% \begin{verbatim} |
| 340 |
% \spaligntabular{lrc}{a b c; aa bb cc}\end{verbatim} |
| 341 |
% produces |
| 342 |
% \[ \spaligntabular{lcr}{a b c; aa bb cc} \] |
| 343 |
% |
| 344 |
% \subsection{Package options}\label{sec:options} |
| 345 |
% The following package options can be specified as key-value pairs when the |
| 346 |
% package is loaded, as in |
| 347 |
% \begin{verbatim} |
| 348 |
% \usepackage[sep={,},endofrow=;]{spalign} \end{verbatim} |
| 349 |
% They can also be set directly with macros, which are described as well. |
| 350 |
% |
| 351 |
% \bigskip |
| 352 |
% \noindent |
| 353 |
% \DescribeMacro{delims} |
| 354 |
% \DescribeMacro{\spaligndelims} |
| 355 |
% \poption Use: Specifies the delimiters used by all matrix macros.\endopt |
| 356 |
% \poption Format: Must contain exactly two delimiter tokens, the first for the left |
| 357 |
% delimiter, the second for the right.\endopt |
| 358 |
% \poption Default: |delims=()|\endopt |
| 359 |
% \poption Macro: |\spaligndelims|\marg{left-delim}\marg{right-delim}\endopt |
| 360 |
% \smallskip |
| 361 |
% It is easier to specify |\{\}| as delimiters using the macro form. |
| 362 |
% For example, |
| 363 |
% \begin{verbatim} |
| 364 |
% \[ \spaligndelims\vert\vert \spalignmat{a b; c d} \]\end{verbatim} |
| 365 |
% produces |
| 366 |
% \[ \spaligndelims\vert\vert \spalignmat{a b; c d}. \] |
| 367 |
% |
| 368 |
% \bigskip |
| 369 |
% \noindent |
| 370 |
% \DescribeMacro{sysdelims} |
| 371 |
% \DescribeMacro{\spalignsysdelims} |
| 372 |
% These function the same way as |delims| and |\spaligndelims|, except they |
| 373 |
% apply only to the |\spalignsys| macro. The default is |\spalignsysdelims\{.|, |
| 374 |
% i.e., left brace and no right delimiter. |
| 375 |
% |
| 376 |
% \bigskip |
| 377 |
% \noindent |
| 378 |
% \DescribeMacro{matdelimskip} |
| 379 |
% \DescribeMacro{\spalignmatdelimskip} |
| 380 |
% \poption Use: Specifies the glue to insert between delimiters and the internal |
| 381 |
% |array| environment in (un-starred) math-mode matrix macros, i.e., all |
| 382 |
% math-mode utility macros except |\spalignvector|.\endopt |
| 383 |
% \poption Format: Should either be empty, or expand to a legal |\hskip|, |
| 384 |
% |\mskip|, or |\kern| command. (Really any sequence of tokens can be |
| 385 |
% specified; they will dutifully be inserted between the delimiters and the |
| 386 |
% |array|, but this behavior may change in future versions.)\endopt |
| 387 |
% \poption Default: |matdelimskip=\,|\endopt |
| 388 |
% \poption Macro: |\def\spalignmatdelimskip|\marg{skip}\endopt |
| 389 |
% \smallskip |
| 390 |
% Actually, an additional skip of |\hskip-\arraycolsep| is always added; the |
| 391 |
% effect is that if |matdimskip| is empty, then there is no extra space between |
| 392 |
% the outer columns of the |array| and the delimiters. This is how the |
| 393 |
% \textsf{amsmath} package's |matrix| environment is defined. It is this |
| 394 |
% package author's opinion that matrices look better when a thin space is added |
| 395 |
% between the outer columns of the |array| and the delimiters. To keep the |
| 396 |
% original array spacing inside the delimiters, specify |
| 397 |
% |matdelimskip=\hskip\arraycolsep|. |
| 398 |
% |
| 399 |
% Here are four matrices typeset, respectively, with |matdelimskip| |
| 400 |
% set to |{}|, |\,|, |\;|, and |{\hskip\arraycolsep}|. |
| 401 |
% \[ \def\spalignmatdelimskip{} \spalignmat{10 20; 30 40} \quad |
| 402 |
% \def\spalignmatdelimskip{\,} \spalignmat{10 20; 30 40} \quad |
| 403 |
% \def\spalignmatdelimskip{\;} \spalignmat{10 20; 30 40} \quad |
| 404 |
% \def\spalignmatdelimskip{\hskip\arraycolsep} \spalignmat{10 20; 30 40} |
| 405 |
% \] |
| 406 |
% |
| 407 |
% \bigskip |
| 408 |
% \noindent |
| 409 |
% \DescribeMacro{vecdelimskip} |
| 410 |
% \DescribeMacro{\spalignvecdelimskip} |
| 411 |
% These function the same way as |matdelimskip| and |\spalignmatdelimskip|, |
| 412 |
% except they apply only to the |\spalignvector| macro. The default is no extra |
| 413 |
% skip in vectors, i.e., |vecdelimskip={}|. |
| 414 |
% |
| 415 |
% \bigskip |
| 416 |
% \noindent |
| 417 |
% \DescribeMacro{sysdelimskip} |
| 418 |
% \DescribeMacro{\spalignsysdelimskip} |
| 419 |
% These function the same way as |matdelimskip| and |\spalignmatdelimskip|, |
| 420 |
% except they apply only to the |\spalignsys| macro. The default is |
| 421 |
% |sysdelimskip=\,|. |
| 422 |
% |
| 423 |
% Actually there is a slight difference: the |\halign| primitive used in the |
| 424 |
% |\spalignsys| macro does not put glue on the outsides of the columns, so that |
| 425 |
% it is unnecessary to subtract any |\arraycolsep| glue. |
| 426 |
% |
| 427 |
% \bigskip |
| 428 |
% \noindent |
| 429 |
% \DescribeMacro{systabspace} |
| 430 |
% \DescribeMacro{\spalignsystabspace} |
| 431 |
% \poption Use: Specifies the amount of glue between columns in |
| 432 |
% |\spsysalign|.\endopt |
| 433 |
% \poption Format: Must be a legal glue specification.\endopt |
| 434 |
% \poption Default: |systabspace=1pt|\endopt |
| 435 |
% \poption Macro: |\spalignsystabspace=|\meta{glue}\endopt |
| 436 |
% \smallskip |
| 437 |
% Equations with operators, relations, and variables all aligned look a |
| 438 |
% better with a bit of extra spacing between these three. Setting |systabspace| |
| 439 |
% to zero will cause the equations to use their natural spacing, subject to |
| 440 |
% the alignment. For example, the following systems of equations were typeset, |
| 441 |
% respectively, with |systabspace| set to |0pt|, |1pt|, and |5pt|. |
| 442 |
% \[ |
| 443 |
% \spalignsystabspace=0pt \spalignsys{2x + y = 4; x - 3y = -17} \qquad |
| 444 |
% \spalignsystabspace=1pt \spalignsys{2x + y = 4; x - 3y = -17} \qquad |
| 445 |
% \spalignsystabspace=5pt \spalignsys{2x + y = 4; x - 3y = -17\rlap.} |
| 446 |
% \] |
| 447 |
% |
| 448 |
% \bigskip |
| 449 |
% \noindent |
| 450 |
% \DescribeMacro{endofrow} |
| 451 |
% \DescribeMacro{\spalignendofrow} |
| 452 |
% \poption Use: Specifies the token to convert into the end-of-row |
| 453 |
% control sequence |\\| (really into the contents of |\spalignendline|) during |
| 454 |
% retokenization.\endopt |
| 455 |
% \poption Format: Must consist of a single token.\endopt |
| 456 |
% \poption Default: |endofrow=;|\endopt |
| 457 |
% \poption Macro: |\def\spalignendofrow|\marg{token}\endopt |
| 458 |
% \smallskip |
| 459 |
% For example, |
| 460 |
% \begin{verbatim} |
| 461 |
% \[ \def\spalignendofrow{|} |
| 462 |
% \spalignmat{1;2 3;4 | 5;6 7;8} \]\end{verbatim} |
| 463 |
% produces |
| 464 |
% \[ \def\spalignendofrow{|} \spalignmat{1;2 3;4 | 5;6 7;8}. \] |
| 465 |
% |
| 466 |
% \bigskip |
| 467 |
% \noindent |
| 468 |
% \DescribeMacro{separator} |
| 469 |
% \DescribeMacro{\spalignseparator} |
| 470 |
% \poption Use: Specifies a token (in addition to space tokens) to convert into |
| 471 |
% the alignment character `|&|' (really the contents of |\spalignseparator|) |
| 472 |
% during retokenization.\endopt |
| 473 |
% \poption Format: Must consist of a single token.\endopt |
| 474 |
% \poption Default: |separator={,}|\endopt |
| 475 |
% \poption Macro: |\def\spalignseparator|\marg{token}\endopt |
| 476 |
% \smallskip |
| 477 |
% For example, |
| 478 |
% \begin{verbatim} |
| 479 |
% \[ \def\spalignseparator{|} |
| 480 |
% \spalignmat{(1,2)|(3,4);(5,6)|(7,8)} \]\end{verbatim} |
| 481 |
% produces |
| 482 |
% \[ \def\spalignseparator{|} \spalignmat{(1,2)|(3,4);(5,6)|(7,8)}. \] |
| 483 |
% |
| 484 |
% \bigskip |
| 485 |
% The following commands can be redefined to affect the behavior of the package, |
| 486 |
% but cannot be specified as key-value pairs when the package is loaded. |
| 487 |
% |
| 488 |
% \bigskip |
| 489 |
% \noindent |
| 490 |
% \DescribeMacro{\spalignendline} |
| 491 |
% \poption Use: The end-of-row token is replaced by the top-level expansion of |
| 492 |
% this macro during retokenization.\endopt |
| 493 |
% \poption Format: May contain any tokens.\endopt |
| 494 |
% \poption Default: |\def\spalignendline{\\}|\endopt |
| 495 |
% \smallskip |
| 496 |
% This is useful, for instance, when using \textsf{spalign} in conjunction with |
| 497 |
% plain \TeX-style alignment macros that use |\cr| as the end-of-row token. |
| 498 |
% See the documentation for |\spalignrun| in~\S\ref{sec:general} and the |
| 499 |
% implementation of |\spalignsys| for examples. |
| 500 |
% |
| 501 |
% \bigskip |
| 502 |
% \noindent |
| 503 |
% \DescribeMacro{\spalignaligntab} |
| 504 |
% \poption Use: Spaces and the contents of |\spalignseparator| are replaced by the |
| 505 |
% top-level expansion of this macro during retokenization.\endopt |
| 506 |
% \poption Format: May contain any tokens.\endopt |
| 507 |
% \poption Default: |\def\spalignaligntab{&}|\endopt |
| 508 |
% \smallskip |
| 509 |
% This is useful, for instance, in one-column alignments. For example, |
| 510 |
% \begin{verbatim} |
| 511 |
% \[ \def\spalignaligntab{\\} \spalignmat{12 1 2 13} \]\end{verbatim} |
| 512 |
% produces |
| 513 |
% \[ \def\spalignaligntab{\\} \spalignmat{12 1 2 13}. \] |
| 514 |
% The |\spalignvector| macro is defined in this way. |
| 515 |
% |
| 516 |
% |
| 517 |
% \subsection{General macros}\label{sec:general} |
| 518 |
% The following macros are meant to make it easy to make new utility macros |
| 519 |
% in different situations using \textsf{spalign}. |
| 520 |
% |
| 521 |
% \bigskip |
| 522 |
% \noindent |
| 523 |
% \DescribeMacro{\spalignretokenize} |
| 524 |
% \usage\spalignretokenize{\marg{text}} |
| 525 |
% Applies the retokenization procedure to \meta{text}, and expands to the |
| 526 |
% retokenized version. For instance, |\spalignretokenize{1 2; 3 4}| expands to |
| 527 |
% |1&2\\3&4|. |
| 528 |
% For example, using the |split| environment from the |
| 529 |
% \textsf{amsmath} package, |
| 530 |
% \begin{verbatim} |
| 531 |
% \[ \begin{split} |
| 532 |
% \spalignretokenize{f_0 =1; f_1 =1; f_{n+2} =f_n+f_{n+1}} |
| 533 |
% \end{split} \]\end{verbatim} |
| 534 |
% produces |
| 535 |
% \[ \begin{split} |
| 536 |
% \spalignretokenize{f_0 =1; f_1 =1; f_{n+2} =f_n+f_{n+1}}. |
| 537 |
% \end{split} \] |
| 538 |
% |
| 539 |
% \bigskip |
| 540 |
% \noindent |
| 541 |
% \DescribeMacro{\spalignrun} |
| 542 |
% \usage\spalignrun{\marg{tokens}\marg{text}} |
| 543 |
% Applies the retokenization procedure to \meta{text}, saving the result into |
| 544 |
% the token register |\spaligntoks| (see below). Then executes \meta{tokens}, |
| 545 |
% which presumably makes reference to |\spaligntoks|. For example, |
| 546 |
% \begin{verbatim} |
| 547 |
% \[ \def\spalignendline{\cr} |
| 548 |
% \spalignrun{\bordermatrix{\the\spaligntoks}} |
| 549 |
% {, x y; u 1 2; v 3 4} \]\end{verbatim} |
| 550 |
% produces |
| 551 |
% \[ \def\spalignendline{\cr} |
| 552 |
% \spalignrun{\bordermatrix{\the\spaligntoks}} |
| 553 |
% {, x y; u 1 2; v 3 4}. \] |
| 554 |
% |
| 555 |
% \bigskip |
| 556 |
% \noindent |
| 557 |
% \DescribeMacro{\spalignenv} |
| 558 |
% \usage\spalignenv{\marg{before-tokens}\marg{after-tokens}\marg{text}} |
| 559 |
% Convenience macro that expands to |
| 560 |
% \[ \hbox{|\spalignrun{|\meta{before-tokens} |
| 561 |
% |\the\spaligntoks| \meta{after-tokens}|}{|\meta{text}|}|.} \] |
| 562 |
% For example, using the |align*| environment from the |
| 563 |
% \textsf{amsmath} package, |
| 564 |
% \begin{verbatim} |
| 565 |
% \spalignenv{\begin{align*}}{\end{align*}}% |
| 566 |
% {x =y x' =y'; z =w z' =w'.}\end{verbatim} |
| 567 |
% produces |
| 568 |
% \spalignenv{\begin{align*}}{\end{align*}}% |
| 569 |
% {x =y x' =y'; z =w z' =w'.} |
| 570 |
% |
| 571 |
% \bigskip |
| 572 |
% The \meta{tokens} arguments of |\spalignrun| and |\spalignenv| have access to |
| 573 |
% the following registers, which are defined locally in a group inside |
| 574 |
% |\spalignrun| and |\spalignenv|. |
| 575 |
% |
| 576 |
% \bigskip |
| 577 |
% \noindent |
| 578 |
% \DescribeMacro{\spaligntoks} |
| 579 |
% A tokens register that contains the result of the retokenizing procedure. |
| 580 |
% |
| 581 |
% \bigskip |
| 582 |
% \noindent |
| 583 |
% \DescribeMacro{\spalignmaxcols} |
| 584 |
% A count register that contains the maximum number of columns in any given row, |
| 585 |
% as parsed by the retokenizer. This is used in |\spalignmat| and in the |
| 586 |
% |\spalignaugmat| family of macros. |
| 587 |
|
| 588 |
% \StopEventually{} |
| 589 |
% |
| 590 |
% \section{Implementation} |
| 591 |
% |
| 592 |
% \subsection{Options processing} |
| 593 |
% |
| 594 |
% \begin{macrocode} |
| 595 |
\makeatletter |
| 596 |
|
| 597 |
\RequirePackage{kvoptions} |
| 598 |
% \end{macrocode} |
| 599 |
% With the following options, the key |foo| gets stored as |\spalign@foo|. |
| 600 |
% \smallskip |
| 601 |
% \begin{macrocode} |
| 602 |
\SetupKeyvalOptions{family=spalign,prefix=spalign@} |
| 603 |
|
| 604 |
\DeclareStringOption[()]{delims} |
| 605 |
\DeclareStringOption[\{.]{sysdelims} |
| 606 |
\DeclareStringOption[\,]{matdelimskip} |
| 607 |
\DeclareStringOption[]{vecdelimskip} |
| 608 |
\DeclareStringOption[\,]{sysdelimskip} |
| 609 |
\DeclareStringOption[1pt]{systabspace} |
| 610 |
\DeclareStringOption[;]{endofrow} |
| 611 |
\DeclareStringOption[,]{separator} |
| 612 |
|
| 613 |
\ProcessLocalKeyvalOptions* |
| 614 |
|
| 615 |
\def\spaligndelims#1#2{% |
| 616 |
\def\spalign@leftdelim{#1}\def\spalign@rightdelim{#2}} |
| 617 |
\expandafter\spaligndelims\spalign@delims |
| 618 |
\def\spalignsysdelims#1#2{% |
| 619 |
\def\spalign@sysleftdelim{#1}\def\spalign@sysrightdelim{#2}} |
| 620 |
\expandafter\spalignsysdelims\spalign@sysdelims |
| 621 |
\let\spalignmatdelimskip=\spalign@matdelimskip |
| 622 |
\let\spalignvecdelimskip=\spalign@vecdelimskip |
| 623 |
\let\spalignsysdelimskip=\spalign@sysdelimskip |
| 624 |
\newdimen\spalignsystabspace |
| 625 |
\spalignsystabspace=\spalign@systabspace |
| 626 |
\let\spalignendofrow=\spalign@endofrow |
| 627 |
\let\spalignseparator=\spalign@separator |
| 628 |
\def\spalignendline{\\} |
| 629 |
\def\spalignaligntab{&} |
| 630 |
% \end{macrocode} |
| 631 |
% |
| 632 |
% \bigskip |
| 633 |
% \subsection{Main retokenizing code} |
| 634 |
% The retokenizer processes the input token list one ``item'' at a time, |
| 635 |
% performing replacements as necessary, and saving the resulting tokens in |
| 636 |
% |\spaligntoks|. The difficulties arise because of the special treatment that |
| 637 |
% \TeX\ gives to spaces and braces. First let's review the rules of the game: |
| 638 |
% \begin{enumerate}\itemsep=1\jot\parskip=0pt |
| 639 |
% \item Spaces after a control sequence are not tokenized. |
| 640 |
% \item Multiple spaces are tokenized into a single space. |
| 641 |
% \item Space tokens between a control sequence and its argument are ignored: |
| 642 |
% undelimited spaces are never interpreted as a macro argument. |
| 643 |
% \item Braces around a macro argument are stripped. |
| 644 |
% \item The |\futurelet|\meta{control sequence}\meta{token1}\meta{token2} syntax |
| 645 |
% will |\let| the specified \meta{control sequence} be \meta{token2}, nomatter |
| 646 |
% what kind of token \meta{token2} is. |
| 647 |
% \end{enumerate} |
| 648 |
% Consider then a macro |\retokenize#1{...}| and the token sequence |
| 649 |
% \[ \hbox{|\retokenize |\tvs|{\bf a}b|} \] |
| 650 |
% The |\retokenize| macro needs to recognize both the space (as it should be |
| 651 |
% replaced by an align token, if other non-space tokens have just been processed), |
| 652 |
% and the braces (so |{\bf a}b| is not replaced by |\bf ab|). However, the |
| 653 |
% argument to |\retokenize| will be |\bf a|; the space and braces are ignored. |
| 654 |
% |
| 655 |
% The solution, of course, is judicious use of |\futurelet|. (One could |
| 656 |
% conceivably use |\let| and |\futurelet| exclusively, except then one would |
| 657 |
% have the opposite problem: we \emph{want} the retokenizer to swallow whole |
| 658 |
% arguments in braces.) This is still somewhat tricky: |
| 659 |
% \[ \hbox{|\futurelet\next\retokenize |\tvs|{\bf a}b|} \] |
| 660 |
% will expand |\retokenize| with the argument set to |\bf a| and |\next| |
| 661 |
% behaving like \tvs, but the braces will still disappear. Hence one has to use |
| 662 |
% the |\futurelet| in a macro which does not take any arguments. |
| 663 |
% |
| 664 |
% Here is the outline of the solution to the problem used in this package. The |
| 665 |
% |\spalign@gobble@spaces| macro has the effect of deleting the subsequent |
| 666 |
% sequence of space tokens and replacing them with the token |
| 667 |
% |\spalign@parsetoks|. It sets |\spalign@saw@spacetrue| if it ate at least |
| 668 |
% one space token, and in any case it sets |\spalign@nexttok| to the following |
| 669 |
% token. The |\spalign@parsetoks| macro takes one argument. When it is |
| 670 |
% executed, |\spalign@nexttok| represents a non-space token. Now |
| 671 |
% |\spalign@parsetoks| knows if the token beginning its argument is a brace, and |
| 672 |
% it knows if there was a space preceding the brace as well, using |
| 673 |
% |\ifspalign@saw@space|. It can proceed to parse its argument in a |
| 674 |
% straightforward manner, eventually expanding back into |
| 675 |
% |\splign@gobble@spaces|, unless it sees |\spalign@end|, which signifies the |
| 676 |
% end of input. |
| 677 |
% |
| 678 |
% See Appendix~D in the {\TeX}book for a discussion of these kinds of tricks. |
| 679 |
% |
| 680 |
% \begin{macro}{\spalign@makespace} |
| 681 |
% This macro expands to a single space token. It is mildly tricky to |
| 682 |
% construct, since \TeX\ allows one optional space token after |\let\foo| or |
| 683 |
% |\let\foo=|, but multiple spaces are combined into one space token. Here is |
| 684 |
% one trick for producing two consecutive space tokens. See Exercise~24.6 in |
| 685 |
% the {\TeX}book. |
| 686 |
% \begin{macrocode} |
| 687 |
\begingroup |
| 688 |
\def\\{\global\let\spalign@space= } \\ % |
| 689 |
\endgroup |
| 690 |
% \end{macrocode} |
| 691 |
% \end{macro} |
| 692 |
% \begin{macro}{\spalign@end} |
| 693 |
% Sentinel macro that is used to mark the end-of-input for |
| 694 |
% |\spalign@parsetoks|. Defining it recursively like this has the |
| 695 |
% disadvantage that \LaTeX\ will hang if it tries to expand |\spalign@end|. |
| 696 |
% On the other hand, one can test whether a token is |\spalign@end| using |
| 697 |
% |\ifx| without using some other sentinel expansion of the macro, and the |
| 698 |
% only reason |\spalign@end| would be executed is if there is a bug in this |
| 699 |
% package. |
| 700 |
% \begin{macrocode} |
| 701 |
\def\spalign@end{\spalign@end} |
| 702 |
% \end{macrocode} |
| 703 |
% \end{macro} |
| 704 |
% \begin{macro}{\spalign@bgroup} |
| 705 |
% This is just used to test whether a token is a literal |\bgroup| (as opposed |
| 706 |
% to an actual open brace |{|) using |\ifx|. |
| 707 |
% \begin{macrocode} |
| 708 |
\def\spalign@bgroup{\bgroup} |
| 709 |
% \end{macrocode} |
| 710 |
% \end{macro} |
| 711 |
% \begin{macro}{\spalign@curcols} |
| 712 |
% This counter keeps track of how many columns are in the current row. |
| 713 |
% \begin{macrocode} |
| 714 |
\newcount\spalign@curcols |
| 715 |
% \end{macrocode} |
| 716 |
% \end{macro} |
| 717 |
% \begin{macro}{\ifspalign@ignorespaces} |
| 718 |
% Boolean variable which keeps track of whether spaces should be ignored in |
| 719 |
% the current state, like after an end-of-row token. |
| 720 |
% \begin{macrocode} |
| 721 |
\newif\ifspalign@ignorespaces |
| 722 |
% \end{macrocode} |
| 723 |
% \end{macro} |
| 724 |
% \begin{macro}{\ifspalign@saw@space} |
| 725 |
% Boolean variable which is true if the argument to |\spalign@parsetoks| was |
| 726 |
% preceded by at least one space token. |
| 727 |
% \begin{macrocode} |
| 728 |
\newif\ifspalign@saw@space |
| 729 |
% \end{macrocode} |
| 730 |
% \end{macro} |
| 731 |
% \begin{macro}{\spalign@gobble@spaces} |
| 732 |
% This macro has the effect of deleting any subsequent space tokens, replacing |
| 733 |
% them with |\spalign@parsetoks|. It sets |\spalign@nexttok| to the next |
| 734 |
% (non-space) token, and sets |\spalign@saw@spacetrue| if it ate at least one |
| 735 |
% space. The boolean variable |\ifspalign@saw@space| should be false before |
| 736 |
% |\spalign@gobble@spaces| is first executed. |
| 737 |
% |
| 738 |
% The macro |\spalign@gobble@next| has the effect of replacing the following |
| 739 |
% token with |\spalign@gobble@spaces|. |
| 740 |
% \begin{macrocode} |
| 741 |
\def\spalign@gobble@next{% |
| 742 |
\afterassignment\spalign@gobble@spaces\let\spalign@atoken= } |
| 743 |
\def\spalign@check@space{% |
| 744 |
\ifx\spalign@nexttok\spalign@space% |
| 745 |
\spalign@saw@spacetrue% |
| 746 |
\let\spalign@next=\spalign@gobble@next% |
| 747 |
\else% |
| 748 |
% \end{macrocode} |
| 749 |
% Don't set |\spalign@saw@spacefalse| here: eventually we will run |
| 750 |
% into a non-space token. |
| 751 |
% \begin{macrocode} |
| 752 |
\let\spalign@next=\spalign@parsetoks% |
| 753 |
\fi% |
| 754 |
\spalign@next% |
| 755 |
} |
| 756 |
\def\spalign@gobble@spaces{% |
| 757 |
\futurelet\spalign@nexttok\spalign@check@space} |
| 758 |
% \end{macrocode} |
| 759 |
% \end{macro} |
| 760 |
% \begin{macro}{\spalign@append} |
| 761 |
% Convenience macro that appends its argument, unexpanded, onto the token |
| 762 |
% register |\spaligntoks|. |
| 763 |
% \begin{macrocode} |
| 764 |
\def\spalign@append#1{% |
| 765 |
\begingroup% |
| 766 |
\toks255={#1}% |
| 767 |
% \end{macrocode} |
| 768 |
% The |\edef| and |\xdef| macros do not expand tokens obtained by expanding a |
| 769 |
% token register. |
| 770 |
% \begin{macrocode} |
| 771 |
\xdef\spalign@settok{% |
| 772 |
\spaligntoks={\the\spaligntoks\the\toks255}}% |
| 773 |
\endgroup% |
| 774 |
\spalign@settok% |
| 775 |
} |
| 776 |
% \end{macrocode} |
| 777 |
% \end{macro} |
| 778 |
% \begin{macro}{\spalign@addcol} |
| 779 |
% Convenience macro to take care of housekeeping when an align column has been |
| 780 |
% parsed but the row is not complete. Note that it appends the top-level |
| 781 |
% expansion of |\spaligntab| to |\spaligntoks|, not the token |\spaligntab| |
| 782 |
% itself. |
| 783 |
% \begin{macrocode} |
| 784 |
\def\spalign@addcol{% |
| 785 |
\expandafter\spalign@append\expandafter{\spalignaligntab}% |
| 786 |
\advance\spalign@curcols by 1 % |
| 787 |
} |
| 788 |
% \end{macrocode} |
| 789 |
% \end{macro} |
| 790 |
% \begin{macro}{\spalign@endrow} |
| 791 |
% Convenience macro to take care of housekeeping when a row has been ended, |
| 792 |
% either by an end-of-row token or an end-of-input token. |
| 793 |
% \begin{macrocode} |
| 794 |
\def\spalign@endrow{% |
| 795 |
\advance\spalign@curcols by 1 % |
| 796 |
\ifnum\spalign@curcols>\spalignmaxcols% |
| 797 |
\spalignmaxcols=\spalign@curcols% |
| 798 |
\fi% |
| 799 |
\spalign@curcols=0% |
| 800 |
\spalign@ignorespacestrue% |
| 801 |
} |
| 802 |
% \end{macrocode} |
| 803 |
% \end{macro} |
| 804 |
% \begin{macro}{\spalign@normaltok} |
| 805 |
% Convenience macro to take care of housekeeping when a non-special token (not |
| 806 |
% a space, end-of-row token, separator token, or end-of-input token) has been |
| 807 |
% parsed. The boolean variable |\ifspalign@saw@space| will be true if the token |
| 808 |
% preceding the non-special token was a space. |
| 809 |
% \begin{macrocode} |
| 810 |
\def\spalign@normaltok{% |
| 811 |
\ifspalign@saw@space% |
| 812 |
\ifspalign@ignorespaces% |
| 813 |
\else% |
| 814 |
\spalign@addcol% |
| 815 |
\fi% |
| 816 |
\fi% |
| 817 |
\spalign@ignorespacesfalse% |
| 818 |
} |
| 819 |
% \end{macrocode} |
| 820 |
% \end{macro} |
| 821 |
% \begin{macro}{\spalign@parsetoks} |
| 822 |
% This is the main retokenizing routine. It is only called by |
| 823 |
% |\spalign@gobble@spaces|. When it is expanded, |\spalign@nexttok| is the |
| 824 |
% token that immediately follows the |\spalign@parsetoks| token itself; this |
| 825 |
% is not a space. The value of the boolean variable |\ifspalign@saw@space| is |
| 826 |
% true if there was a space before the argument of |\spalign@parsetoks| in the |
| 827 |
% token list. |
| 828 |
% \begin{macrocode} |
| 829 |
\def\spalign@parsetoks#1{% |
| 830 |
\let\spalign@next=\spalign@gobble@spaces% |
| 831 |
% \end{macrocode} |
| 832 |
% This is for |\ifx| comparisons: |
| 833 |
% \begin{macrocode} |
| 834 |
\def\spalign@arg{#1}% |
| 835 |
% \end{macrocode} |
| 836 |
% Anything in braces is passed through untouched: |
| 837 |
% \begin{macrocode} |
| 838 |
\ifx\spalign@nexttok\bgroup% |
| 839 |
\spalign@normaltok% |
| 840 |
\ifx\spalign@arg\spalign@bgroup% |
| 841 |
% \end{macrocode} |
| 842 |
% The argument is a literal |\bgroup|, not a brace. |
| 843 |
% \begin{macrocode} |
| 844 |
\spalign@append{#1}% |
| 845 |
\else% |
| 846 |
% \end{macrocode} |
| 847 |
% Re-wrap the argument in braces and append. |
| 848 |
% \begin{macrocode} |
| 849 |
\spalign@append{{#1}}% |
| 850 |
\fi% |
| 851 |
\else% |
| 852 |
% \end{macrocode} |
| 853 |
% The argument is not wrapped in braces. |
| 854 |
% \begin{macrocode} |
| 855 |
\ifx\spalign@arg\spalignendofrow% |
| 856 |
% \end{macrocode} |
| 857 |
% End-of-row token. Append the top-level expansion of |\spalignendline|. |
| 858 |
% (Ignore previous spaces.) |
| 859 |
% \begin{macrocode} |
| 860 |
\expandafter\spalign@append\expandafter{\spalignendline}% |
| 861 |
\spalign@endrow% |
| 862 |
\else% |
| 863 |
\ifx\spalign@arg\spalignseparator% |
| 864 |
% \end{macrocode} |
| 865 |
% Separator token. (Ignore previous spaces.) |
| 866 |
% \begin{macrocode} |
| 867 |
\spalign@addcol% |
| 868 |
\spalign@ignorespacestrue% |
| 869 |
\else% |
| 870 |
\ifx\spalign@arg\spalign@end% |
| 871 |
% \end{macrocode} |
| 872 |
% End-of-input token. End the current row to record |\spalign@maxcols|. |
| 873 |
% (Ignore previous spaces.) |
| 874 |
% \begin{macrocode} |
| 875 |
\let\spalign@next=\relax% |
| 876 |
\spalign@endrow% |
| 877 |
\else% |
| 878 |
% \end{macrocode} |
| 879 |
% Non-special token. |
| 880 |
% \begin{macrocode} |
| 881 |
\spalign@normaltok% |
| 882 |
\spalign@append{#1}% |
| 883 |
\fi% |
| 884 |
\fi% |
| 885 |
\fi% |
| 886 |
\fi% |
| 887 |
% \end{macrocode} |
| 888 |
% Reset |\ifspalign@saw@space| for the next |\spalign@gobble@spaces|. |
| 889 |
% \begin{macrocode} |
| 890 |
\spalign@saw@spacefalse% |
| 891 |
\spalign@next% |
| 892 |
} |
| 893 |
% \end{macrocode} |
| 894 |
% \end{macro} |
| 895 |
% \begin{macro}{\spalign@process} |
| 896 |
% This is a wrapper for |\spalign@parsetoks|. It initializes registers and |
| 897 |
% boolean variables, then starts the parsing routine with |
| 898 |
% |\spalign@gobble@spaces|. It should be run in a local group. It stops |
| 899 |
% processing at |\spalign@end|. It fills |\spalignmaxcols| and |\spaligntoks| |
| 900 |
% with the results of the retokenization. It replaces everything in the token |
| 901 |
% list up through |\spalign@end| with |\relax|. |
| 902 |
% \begin{macrocode} |
| 903 |
\def\spalign@process{% |
| 904 |
\spaligntoks={}% |
| 905 |
\spalignmaxcols=0% |
| 906 |
\spalign@curcols=0% |
| 907 |
\spalign@ignorespacestrue% |
| 908 |
\spalign@saw@spacefalse% |
| 909 |
\spalign@gobble@spaces% |
| 910 |
} |
| 911 |
% \end{macrocode} |
| 912 |
% \end{macro} |
| 913 |
% |
| 914 |
% \subsection{Poor man's starred commands} |
| 915 |
% There are several excellent packages that allow for flexible command |
| 916 |
% specifications. However, the needs of this package are minimal as regards |
| 917 |
% starred commands, so in order to reduce dependencies, this package contains |
| 918 |
% its own simple implimentation. |
| 919 |
% |
| 920 |
% \begin{macro}{\ifspalign@star} |
| 921 |
% Boolean variable which records whether or not the current command has a star |
| 922 |
% on it. |
| 923 |
% \begin{macrocode} |
| 924 |
\newif\ifspalign@star |
| 925 |
% \end{macrocode} |
| 926 |
% \end{macro} |
| 927 |
% \begin{macro}{\spalign@def@star} |
| 928 |
% Used like |\def|, the command |\spalign@def@star\foo{...}| defines a macro |
| 929 |
% |\foo| which can be called as |\foo| or |\foo*|. In the body of |\foo|, |
| 930 |
% the boolean variable |\ifspalign@star| indicates whether the command was |
| 931 |
% called with a star. |
| 932 |
% |
| 933 |
% This command actually defines three commands: |\foo|, |\foo@x|, and |
| 934 |
% |\foo@star|. The first calls the second with |\spalign@nexttok| defined via |
| 935 |
% |\futurelet| to the token following |\foo|. The command |\foo@x| checks |
| 936 |
% whether or not the next token is a star, and if it is, it is removed from |
| 937 |
% the token list. It then sets |\ifspalign@star| appropriately and calls |
| 938 |
% |\foo@star|, which contains the original command definition. Of course, |
| 939 |
% |\foo@star| can then be redefined, for instance using |\newcommand|, to take |
| 940 |
% advantage of \LaTeX's optional argument parsing. |
| 941 |
% \begin{macrocode} |
| 942 |
\def\spalign@gobble@one#1{} |
| 943 |
|
| 944 |
\def\spalign@def@star#1{% |
| 945 |
% \end{macrocode} |
| 946 |
% If |#1| is |\foo|, then |\spalign@cmd| expands to |\foo|, |
| 947 |
% |\spalign@cmd@x| expands to |\foo@x|, and |\spalign@cmd@star| expands to |
| 948 |
% |\foo@star|. |
| 949 |
% \begin{macrocode} |
| 950 |
\def\spalign@cmd{#1}% |
| 951 |
\edef\spalign@cmd@x{% |
| 952 |
\csname\expandafter\spalign@gobble@one\string#1@x\endcsname}% |
| 953 |
\edef\spalign@cmd@star{% |
| 954 |
\csname\expandafter\spalign@gobble@one\string#1@star\endcsname}% |
| 955 |
% \end{macrocode} |
| 956 |
% Make |\foo@x| unexpandable. (The |\csname...\endcsname| construction |
| 957 |
% already does this, but only if |\foo@x| was previously undefined.) This |
| 958 |
% makes it easier to define |\foo|. |
| 959 |
% \begin{macrocode} |
| 960 |
\expandafter\let\spalign@cmd@x=\relax |
| 961 |
\expandafter\edef\spalign@cmd{% |
| 962 |
\futurelet\noexpand\spalign@nexttok\spalign@cmd@x}% |
| 963 |
% \end{macrocode} |
| 964 |
% I don't know a less annoying but still short way of defining a token list |
| 965 |
% where only one token in the middle is to be expanded, and that one token only |
| 966 |
% once (not recursively). |
| 967 |
% \begin{macrocode} |
| 968 |
\def\spalign@mkcmd##1{% |
| 969 |
\expandafter\def\spalign@cmd@x{% |
| 970 |
\ifx\spalign@nexttok*% |
| 971 |
\spalign@startrue% |
| 972 |
\let\spalign@next=\spalign@gobble@one% |
| 973 |
\else% |
| 974 |
\spalign@starfalse% |
| 975 |
\def\spalign@next{}% |
| 976 |
\fi% |
| 977 |
% \end{macrocode} |
| 978 |
% Expanding |\spalign@gobble@one| before |##1| (which is |\foo@star|) eats the |
| 979 |
% star before parsing the arguments for |\foo@star|. |
| 980 |
% \begin{macrocode} |
| 981 |
\expandafter##1\spalign@next% |
| 982 |
}% |
| 983 |
}% |
| 984 |
\expandafter\spalign@mkcmd\spalign@cmd@star% |
| 985 |
\expandafter\def\spalign@cmd@star% |
| 986 |
} |
| 987 |
% \end{macrocode} |
| 988 |
% \end{macro} |
| 989 |
% |
| 990 |
% \subsection{General macros} |
| 991 |
% |
| 992 |
% Here are the definitions of the macros presented in~\S\ref{sec:general}. |
| 993 |
% \begin{macrocode} |
| 994 |
\newtoks\spaligntoks |
| 995 |
\newcount\spalignmaxcols |
| 996 |
% \end{macrocode} |
| 997 |
% \begin{macro}{\spalignrun} |
| 998 |
% Calls |\spalign@process| on |#2|, then inserts |#1|, in a group. |
| 999 |
% Presumably |#1| will refer to |\spaligntoks| and/or |\spalignmaxcols|. |
| 1000 |
% \begin{macrocode} |
| 1001 |
\def\spalignrun#1#2{% |
| 1002 |
\begingroup% |
| 1003 |
\spalign@process#2\spalign@end% |
| 1004 |
%\showthe\spaligntoks% For debugging |
| 1005 |
#1% |
| 1006 |
\endgroup% |
| 1007 |
} |
| 1008 |
% \end{macrocode} |
| 1009 |
% \end{macro} |
| 1010 |
% \begin{macro}{\spalignenv} |
| 1011 |
% This effectively calls |\spalign@process| on |#3|, then puts the resulting |
| 1012 |
% token list between |#1| and |#2|. Both |#1| and |#2| have access to |
| 1013 |
% |\spaligntoks| and |\spalignmaxcols|. |
| 1014 |
% \begin{macrocode} |
| 1015 |
\def\spalignenv#1#2{% |
| 1016 |
\spalignrun{% |
| 1017 |
#1% |
| 1018 |
\the\spaligntoks% |
| 1019 |
#2% |
| 1020 |
}% |
| 1021 |
} |
| 1022 |
% \end{macrocode} |
| 1023 |
% \end{macro} |
| 1024 |
% \begin{macro}{\spalignretokenize} |
| 1025 |
% This calls |\spalign@process| on |#1|, then expands to |\the\spaligntoks|. |
| 1026 |
% \begin{macrocode} |
| 1027 |
\def\spalignretokenize#1{% |
| 1028 |
\begingroup% |
| 1029 |
\spalign@process#1\spalign@end% |
| 1030 |
\expandafter\endgroup\the\spaligntoks% |
| 1031 |
} |
| 1032 |
% \end{macrocode} |
| 1033 |
% \end{macro} |
| 1034 |
% \begin{macro}{\spaligntabular} |
| 1035 |
% Tabular utility macro. |
| 1036 |
% \begin{macrocode} |
| 1037 |
\def\spaligntabular#1#2{% |
| 1038 |
\begin{tabular}{#1}\spalignretokenize{#2}\end{tabular}} |
| 1039 |
% \end{macrocode} |
| 1040 |
% \end{macro} |
| 1041 |
% \begin{macro}{\spalign@maybedelim} |
| 1042 |
% This is like |\spalignenv|, but it adds delimiters and the glue specified in |
| 1043 |
% |#3| before |#1| and after |#2|, if |\ifspalign@star| is false. |
| 1044 |
% \begin{macrocode} |
| 1045 |
\def\spalign@maybedelim#1#2#3{% |
| 1046 |
\spalignenv% |
| 1047 |
{\ifspalign@star\else\left\spalign@leftdelim#3\fi#1}% |
| 1048 |
{#2\ifspalign@star\else#3\right\spalign@rightdelim\fi}% |
| 1049 |
} |
| 1050 |
% \end{macrocode} |
| 1051 |
% \end{macro} |
| 1052 |
% \begin{macro}{\spalignarray} |
| 1053 |
% Array utility macro with delimiters. |
| 1054 |
% \begin{macrocode} |
| 1055 |
\spalign@def@star\spalignarray#1{% |
| 1056 |
\spalign@maybedelim% |
| 1057 |
{\begin{array}{#1}}% |
| 1058 |
{\end{array}}% |
| 1059 |
{\hskip-\arraycolsep\spalignmatdelimskip}% |
| 1060 |
} |
| 1061 |
% \end{macrocode} |
| 1062 |
% \end{macro} |
| 1063 |
% \begin{macro}{\spalignvector} |
| 1064 |
% Vector utility macro: an array with one column, with |\spaligntab| set to |
| 1065 |
% |\\| so that spaces produce new rows. |
| 1066 |
% \begin{macrocode} |
| 1067 |
\spalign@def@star\spalignvector{} |
| 1068 |
\renewcommand\spalignvector@star[2][c]{% |
| 1069 |
\begingroup% |
| 1070 |
\def\spalignaligntab{\\}% |
| 1071 |
\spalign@maybedelim% |
| 1072 |
{\begin{array}{#1}}% |
| 1073 |
{\end{array}}% |
| 1074 |
% \end{macrocode} |
| 1075 |
% Note the use of |\spalignvecdelimskip| here. |
| 1076 |
% \begin{macrocode} |
| 1077 |
{\hskip-\arraycolsep\spalignvecdelimskip}% |
| 1078 |
{#2}% |
| 1079 |
\endgroup% |
| 1080 |
} |
| 1081 |
% \end{macrocode} |
| 1082 |
% \end{macro} |
| 1083 |
% \begin{macro}{\spalign@repeat} |
| 1084 |
% Sets |\spalign@repeated| to |#1|, repeated |#2| times. Used for |
| 1085 |
% auto-constructing |array| alignment specifications from |\spalignmaxcols|. |
| 1086 |
% \begin{macrocode} |
| 1087 |
\def\spalign@repeat#1#2{% |
| 1088 |
\begingroup% |
| 1089 |
\count255=0 % |
| 1090 |
\toks255={}% |
| 1091 |
\loop\ifnum\count255<#2% |
| 1092 |
\edef\spalign@settok{\toks255={\the\toks255 #1}}% |
| 1093 |
\spalign@settok% |
| 1094 |
\advance\count255 by 1 % |
| 1095 |
\repeat% |
| 1096 |
\xdef\spalign@repeated{\the\toks255}% |
| 1097 |
\endgroup |
| 1098 |
} |
| 1099 |
% \end{macrocode} |
| 1100 |
% \end{macro} |
| 1101 |
% \begin{macro}{\spalignmat} |
| 1102 |
% Matrix utility macro. Uses |\spalignmaxcols| and |\spalign@repeat| to |
| 1103 |
% construct the |array| align specification. |
| 1104 |
% \begin{macrocode} |
| 1105 |
\spalign@def@star\spalignmat{} |
| 1106 |
\renewcommand\spalignmat@star[1][c]{% |
| 1107 |
\spalign@maybedelim{% |
| 1108 |
\spalign@repeat{#1}{\spalignmaxcols}% |
| 1109 |
\edef\spalign@barray{\noexpand\begin{array}{% |
| 1110 |
\spalign@repeated}}% |
| 1111 |
\spalign@barray% |
| 1112 |
}{\end{array}% |
| 1113 |
}{\hskip-\arraycolsep\spalignmatdelimskip}% |
| 1114 |
} |
| 1115 |
% \end{macrocode} |
| 1116 |
% \end{macro} |
| 1117 |
% \begin{macro}{\spalignaugmatn} |
| 1118 |
% Augmented matrix with |#2| columns on the right of the vertical bar. Uses |
| 1119 |
% |\spalignmaxcols| and |\spalign@repeat| to construct the |array| align |
| 1120 |
% specification. |
| 1121 |
% \begin{macrocode} |
| 1122 |
\spalign@def@star\spalignaugmatn{} |
| 1123 |
\renewcommand\spalignaugmatn@star[2][r]{% |
| 1124 |
\spalign@maybedelim{% |
| 1125 |
\advance\spalignmaxcols by -#2 % |
| 1126 |
\spalign@repeat{#1}{\spalignmaxcols}% |
| 1127 |
\let\spalign@repeated@one=\spalign@repeated% |
| 1128 |
\spalign@repeat{#1}{#2}% |
| 1129 |
\let\spalign@repeated@two=\spalign@repeated% |
| 1130 |
\edef\spalign@barray{\noexpand\begin{array}{% |
| 1131 |
\spalign@repeated@one|\spalign@repeated@two}}% |
| 1132 |
\spalign@barray% |
| 1133 |
}{\end{array}% |
| 1134 |
}{\hskip-\arraycolsep\spalignmatdelimskip}% |
| 1135 |
}% |
| 1136 |
% \end{macrocode} |
| 1137 |
% \end{macro} |
| 1138 |
% \begin{macro}{\spalignaugmat} |
| 1139 |
% Augmented matrix with one column on the right of the vertical bar. |
| 1140 |
% \begin{macrocode} |
| 1141 |
\spalign@def@star\spalignaugmat{} |
| 1142 |
\renewcommand\spalignaugmat@star[1][r]{% |
| 1143 |
\spalignaugmatn@star[#1]{1}% |
| 1144 |
}% |
| 1145 |
% \end{macrocode} |
| 1146 |
% \end{macro} |
| 1147 |
% \begin{macro}{\spalignaugmathalf} |
| 1148 |
% Augmented matrix with (ceiling of) half the columns on the right. |
| 1149 |
% \begin{macrocode} |
| 1150 |
\spalign@def@star\spalignaugmathalf{} |
| 1151 |
\renewcommand\spalignaugmathalf@star[1][r]{% |
| 1152 |
\spalign@maybedelim{% |
| 1153 |
\count255=\spalignmaxcols% |
| 1154 |
\divide\spalignmaxcols by 2 % |
| 1155 |
\advance\count255 by -\spalignmaxcols% |
| 1156 |
\spalign@repeat{#1}{\spalignmaxcols}% |
| 1157 |
\let\spalign@repeated@one=\spalign@repeated% |
| 1158 |
\spalignmaxcols=\count255% |
| 1159 |
\spalign@repeat{#1}{\spalignmaxcols}% |
| 1160 |
\let\spalign@repeated@two=\spalign@repeated% |
| 1161 |
\edef\spalign@barray{\noexpand\begin{array}{% |
| 1162 |
\spalign@repeated@one|\spalign@repeated@two}}% |
| 1163 |
\spalign@barray% |
| 1164 |
}{\end{array}% |
| 1165 |
}{\hskip-\arraycolsep\spalignmatdelimskip}% |
| 1166 |
}% |
| 1167 |
% \end{macrocode} |
| 1168 |
% \end{macro} |
| 1169 |
% \begin{macro}{\spalignsys} |
| 1170 |
% System of equations with aligned operators and variables. |
| 1171 |
% \begin{macrocode} |
| 1172 |
\spalign@def@star\spalignsys#1{% |
| 1173 |
\ifspalign@star\else% |
| 1174 |
\left\spalign@sysleftdelim\spalignsysdelimskip% |
| 1175 |
\fi% |
| 1176 |
\vcenter{% |
| 1177 |
\def\spalignendline{\cr}% |
| 1178 |
\openup1pt% |
| 1179 |
\tabskip=0pt% |
| 1180 |
\def\+{\mathbin{\phantom{+}}}% |
| 1181 |
\def\={\mathrel{\phantom{=}}}% |
| 1182 |
\def\.{}% |
| 1183 |
\halign{% |
| 1184 |
% \end{macrocode} |
| 1185 |
% Adding |{}| to each side of the align argument in the even columns causes |
| 1186 |
% binary operators ($+$, $-$, $\ldots$) and relations ($=$, $<$, $\ldots$) to |
| 1187 |
% use their natural spacing. Assuming the even columns contain only binary |
| 1188 |
% operators (resp.\ only relations), these columns will all be the same width. |
| 1189 |
% The |\hfil| in the odd columns right-justifies. There should be no spaces in |
| 1190 |
% the templates. |
| 1191 |
% \begin{macrocode} |
| 1192 |
\tabskip=\spalignsystabspace% |
| 1193 |
&$\hfil##$&${}##{}$\cr% |
| 1194 |
\spalignretokenize{#1}\crcr% |
| 1195 |
}% |
| 1196 |
% \end{macrocode} |
| 1197 |
% It seems that one can't specify tabskip glue for after the last column when |
| 1198 |
% there are repeated templates. |
| 1199 |
% \begin{macrocode} |
| 1200 |
}\hskip-\spalignsystabspace% |
| 1201 |
\ifspalign@star\else% |
| 1202 |
\spalignsysdelimskip\right\spalign@sysrightdelim% |
| 1203 |
\fi% |
| 1204 |
} |
| 1205 |
% \end{macrocode} |
| 1206 |
% \end{macro} |
| 1207 |
% \begin{macrocode} |
| 1208 |
\makeatother |
| 1209 |
% \end{macrocode} |
| 1210 |
% \Finale |
| 1211 |
\endinput |