Debian Codegraph @

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

raw
1 % \iffalse
2 %<*driver>
3 \ProvidesFile{polytable.drv}
4 \documentclass{ltxdoc}
5 \usepackage[T1]{fontenc}
6 \usepackage[utf8]{inputenc}
7 \usepackage{lmodern}
8 \usepackage[info]{polytable}
9 \begin{document}
10 \DocInput{polytable.dtx}
11 \end{document}
12 %</driver>
13 %
14 % Copyright (C) 2003--2020 by Andres Loeh
15 %
16 % \fi
17 %
18 % \ProvidesFile{polytable.dtx}
19 % [2020/07/14 v0.8.6 `polytable' package (Andres Loeh)]
20 % \GetFileInfo{polytable.dtx}
21 %
22 % \title{The \textsf{polytable} package}
23 % \author{Andres L\"oh\\ \texttt{[email protected]}}
24 % \date{\filedate}
25 %
26 % \maketitle
27 %
28 % \begin{abstract}
29 % This package implements a variant of tabular/tabbing-like environments
30 % where columns can be given a name and entries can flexibly
31 % be placed between arbitrary columns. Complex alignment-based
32 % layouts, for example for program code, are possible.
33 % \end{abstract}
34 %
35 % \changes{v0.1}{2002/11/17}{Works.}
36 % \changes{v0.2}{2003/04/03}{Added pboxed. Added debug option.}
37 % \changes{v0.3}{2003/05/26}{Greatly enhanced pboxed.}
38 % \changes{v0.4}{2003/06/11}{Optional argument for ptable and parray.}
39 % \changes{v0.4.1}{2003/07/13}{Saving and restoring enhancements.}
40 % \changes{v0.4.2}{2003/07/16}{Fixed bug that caused infinite reruns.}
41 % \changes{v0.4.3}{2003/07/20}{Added some missing relaxes.}
42 % \changes{v0.5}{2003/07/22}{Documentation improved. To be put on CTAN.}
43 % \changes{v0.6}{2003/07/24}{Depends on lazylist instead of lambda now.}
44 % \changes{v0.7}{2003/11/20}{Check for undefined columns. Save counters.}
45 % \changes{v0.7.1}{2003/12/26}{Counters have not been restored correctly.}
46 % \changes{v0.7.2}{2004/02/27}{Fix for conflict with amsmath package.}
47 % \changes{v0.7.3}{2004/06/21}{Balance last line in pboxed environment.}
48 % \changes{v0.8}{2004/06/24}{New environments pmboxed, ptboxed.
49 % Removed original ptabular and parray.
50 % Interface overhaul.}
51 % \changes{v0.8.1}{2004/11/11}{Fixed some problems with the new
52 % interface, added the external (disk) queue.}
53 % \changes{v0.8.2}{2005/04/26}{New interface command.
54 % Minor beautifications.}
55 % \changes{v0.8.3}{2009/04/28}{Make sorting algorithm stable.}
56 % \changes{v0.8.4}{2009/11/01}{Make ptb output more stable.}
57 % \changes{v0.8.5}{2013/07/18}{Work around lazylist name clash
58 % with unicode-math.}
59 % \changes{v0.8.6}{2020/07/14}{Fix bug with column command redefinitions.}
60 %
61 % \section{Introduction}
62 %
63 % This package implements a variant of tabular-like environments.
64 % We will call these environments the |poly|-environments to
65 % distinguish them from the standard ones as provided by the
66 % \LaTeX\ kernel or the \textsf{array} package.
67 %
68 % Other than in standard tables, each column has a name. For
69 % instance, the commands\\
70 % |\column{foo}{l}|\\
71 % |\column{bar}{r}|\\
72 % -- when used within a |poly|-environment -- define a column
73 % with name |foo| that is left-aligned, and a column with name
74 % |bar| that is right-aligned.
75 %
76 % Once a couple of columns have been defined, the text is specified
77 % in a series of |\fromto| commands. Instead of specifying text
78 % per column in order, separating columns with |&|, we give the
79 % name of the column where the content should start, and the name
80 % of the column before which the content should stop.
81 % To typeset the text ``I'm aligned!'' in the column |foo|,
82 % we could thus use the command\\
83 % |\fromto{foo}{bar}{I'm aligned}|\\
84 % Several |\fromto|-commands can be used to typeset a complete
85 % line of the table. A new line can be started with |\\|.
86 %
87 % The strength of this approach is that it implicitly handles
88 % cases where different lines have different alignment properties.
89 % Not all column names have to occur in all lines.
90 %
91 % \section{A complete example}
92 %
93 % Figure~\ref{tab:example}
94 % is an example that is designed to show the
95 % capabilities of this package. In particular, it is \emph{not}
96 % supposed to look beautiful.
97 %
98 % \begin{figure}[htb]
99 % \centering
100 % \begin{ptboxed}
101 % \defaultcolumn{l|}\column{.}{|l|}
102 % \> left
103 % \=3 first of three \> second of three \> third of three
104 % \=r right \\
105 % \defaultcolumn{r|}\> left \=2 middle 1/2 \> middle 2/2 \=r right \\
106 % \> left \=3 middle 1/3 \> middle 2/3 \> middle 3/3 \=r right \\
107 % \> left
108 % \=2 first of two middle columns \> second of two middle columns
109 % \=r right \\
110 % \end{ptboxed}
111 % \caption{Example table}%
112 % \label{tab:example}
113 % \end{figure}
114 %
115 % The example table consists of four lines. All lines have
116 % some text on the left and on the right, but the middle part
117 % follows two different patterns: the first and the third line
118 % have three middle columnss that should be aligned, the second
119 % and the fourth line have two (right-aligned) middle columns
120 % that should be aligned, but otherwise independent of the three
121 % middle columns in the other lines.
122 %
123 % Vertical bars are used to clarify where one column ends and
124 % the next column starts in a particular line. Note that the first
125 % and the third line are completely aligned. Likewise, the
126 % second and the fourth line are. However, the fact that the
127 % bar after the text ``middle 1/2'' ends up between the two
128 % bars delimiting the column with ``second of three'' in it
129 % is just determined by the length of the text ``first of two
130 % middle columns'' in the last line. This text fragment
131 % is wider than the first of the three middle columns, but not
132 % wider than the first two of the three middle columns.
133 %
134 % Let's have a look at the input for the example table:
135 % \begin{verbatim}
136 % \begin{ptboxed}
137 % \defaultcolumn{l|}\column{.}{|l|}
138 % \> left
139 % \=3 first of three \> second of three \> third of three
140 % \=r right \\
141 % \defaultcolumn{r|}\> left \=2 middle 1/2 \> middle 2/2 \=r right \\
142 % \> left \=3 middle 1/3 \> middle 2/3 \> middle 3/3 \=r right \\
143 % \> left
144 % \=2 first of two middle columns \> second of two middle columns
145 % \=r right \\
146 % \end{ptboxed}
147 % \end{verbatim}
148 % First, columns are declared, including the vertical lines.
149 % Note that there is a final column |end| being declared
150 % that is only used as the end column in the |\fromto| statements.
151 % A future version of this package
152 % will probably get rid of the need to define
153 % such a column.
154 % After the column definitions, the lines are typeset by a
155 % series of |\fromto| commands, separated by |\\|.
156 % Note that the first and third column do not use |m12|, |m22|.
157 % Similarly, the second and fourth column do not use
158 % |m13|, |m23|, and |m33|.
159 %
160 % So far, one could achieve the same with an ordinary |tabular|
161 % environment. The table would have 6 columns. One left and right,
162 % the other four for the middle: the first and third line would
163 % use the first of the four columns, then place the second entry
164 % in a |\multicolumn| of length 2, and then use the fourth column
165 % for the third entry. Likewise, the other lines would place both
166 % their entries in a |\multicolumn| of length 2. In fact, this
167 % procedure is very similar to the way the |ptabular| environment
168 % is implemented.
169 %
170 % The problem is, though, that we need the information that
171 % the first of the two middle columns ends somewhere in the middle
172 % of the second of the three columns, as observed above. If we
173 % slightly modify the texts to be displayed in the middle columns,
174 % this situation changes. Figure~\ref{tab:variation} shows
175 % two variants of the example table. The input is the same,
176 % only that the texts contained in some columns have slightly
177 % changed. As you can see, the separator between the first and
178 % second middle column in the second and fourth lines of the
179 % tables now once ends up within the first, once within the
180 % third of the three middle columns of the other lines.
181 %
182 % \begin{figure}[htb]
183 % \centering
184 % \begin{ptboxed}
185 % \column{left}{|l|}
186 % \column{right}{l|}
187 % \column{m13}{l|}
188 % \column{m23}{l|}
189 % \column{m33}{l|}
190 % \column{m12}{r|}
191 % \column{m22}{r|}
192 % \column{end}{l}
193 % \fromto{left}{m13}{left}
194 % \fromto{m13}{m23}{first of three}
195 % \fromto{m23}{m33}{second of three}
196 % \fromto{m33}{right}{third of three}
197 % \fromto{right}{end}{right}
198 % \\
199 % \fromto{left}{m12}{left}
200 % \fromto{m12}{m22}{middle 1/2}
201 % \fromto{m22}{right}{middle 2/2}
202 % \fromto{right}{end}{right}
203 % \\
204 % \fromto{left}{m13}{left}
205 % \fromto{m13}{m23}{middle 1/3}
206 % \fromto{m23}{m33}{middle 2/3}
207 % \fromto{m33}{right}{middle 3/3}
208 % \fromto{right}{end}{right}
209 % \\
210 % \fromto{left}{m12}{left}
211 % \fromto{m12}{m22}{first of two}
212 % \fromto{m22}{right}{second of two}
213 % \fromto{right}{end}{right}
214 % \end{ptboxed}
215 % \par\bigskip
216 % \begin{ptboxed}
217 % \column{left}{|l|}
218 % \column{right}{l|}
219 % \column{m13}{l|}
220 % \column{m23}{l|}
221 % \column{m33}{l|}
222 % \column{m12}{r|}
223 % \column{m22}{r|}
224 % \column{end}{l}
225 % \fromto{left}{m13}{left}
226 % \fromto{m13}{m23}{first of three}
227 % \fromto{m23}{m33}{second of three}
228 % \fromto{m33}{right}{third of three}
229 % \fromto{right}{end}{right}
230 % \\
231 % \fromto{left}{m12}{left}
232 % \fromto{m12}{m22}{middle 1/2}
233 % \fromto{m22}{right}{middle 2/2}
234 % \fromto{right}{end}{right}
235 % \\
236 % \fromto{left}{m13}{left}
237 % \fromto{m13}{m23}{middle 1/3}
238 % \fromto{m23}{m33}{middle 2/3}
239 % \fromto{m33}{right}{middle 3/3}
240 % \fromto{right}{end}{right}
241 % \\
242 % \fromto{left}{m12}{left}
243 % \fromto{m12}{m22}{the first of two middle columns}
244 % \fromto{m22}{right}{2/2}
245 % \fromto{right}{end}{right}
246 % \end{ptboxed}
247 % \caption{Variants of the example table}%
248 % \label{tab:variation}
249 % \end{figure}
250 %
251 % If one wants the general case using the |\multicolumn|
252 % approach, one thus has to measure the widths of the entries
253 % of the columns to compute their relative position. In essence,
254 % this is what the package does for you.
255 %
256 % \section{Haskell code example}
257 %
258 % I have written this package mainly for one purpose: to be able
259 % to beautifully align Haskell source code. Haskell is a functional
260 % programming language where definitions are often grouped into
261 % several declarations. I've seen programmers exhibit symmetric
262 % structures in different lines by adding spaces in their source
263 % code files in such a way that corresponding parts in different
264 % definitions line up. On the other hand, as Haskell allows
265 % user-defined infix operators, some programmers like their symbols
266 % to be typeset as \LaTeX\ symbols, not as typewriter code.
267 % But using \LaTeX\ symbols and a beautiful proportional font
268 % usually destroys the carefully crafted layout and alignment.
269 %
270 % With lhs2\TeX, there is now a preprocessor available that preserves
271 % the source code's internal alignment by mapping the output onto
272 % \textsf{polytable}'s environments.
273 % Figure~\ref{tab:haskell} is an example of how
274 % the output of lhs2\TeX\ might look like.
275 %
276 % \newcommand{\HsVar}[1]{\mathop{\mathit{\strut #1}}}
277 % \newcommand{\HsCon}[1]{\mathop{\mathsf{\strut #1}}}
278 % \newcommand{\HsKey}[1]{\mathop{\mathbf{\strut #1}}}
279 % \newcommand{\map}{\HsVar{map}}
280 % \newcommand{\round}{\HsVar{round}}
281 % \newcommand{\TA}{\HsVar{a}}
282 % \newcommand{\TB}{\HsVar{b}}
283 % \newcommand{\TF}{\HsVar{f}}
284 % \newcommand{\TX}{\HsVar{x}}
285 % \newcommand{\TY}{\HsVar{y}}
286 % \newcommand{\TN}{\HsVar{n}}
287 % \newcommand{\TM}{\HsVar{m}}
288 % \newcommand{\TR}{\HsVar{r}}
289 % \newcommand{\TXS}{\HsVar{xs}}
290 % \newcommand{\RealFrac}{\mathop{\mathsf{RealFrac}}}
291 % \newcommand{\Integral}{\mathop{\mathsf{Integral}}}
292 % \newcommand{\LET}{\mathop{\mathbf{let}}}
293 % \newcommand{\IN}{\mathop{\mathbf{in}}}
294 % \newcommand{\IF}{\mathop{\mathbf{if}}}
295 % \newcommand{\THEN}{\mathop{\mathbf{then}}}
296 % \newcommand{\ELSE}{\mathop{\mathbf{else}}}
297 % \newcommand{\OF}{\mathop{\mathbf{of}}}
298 % \newcommand{\CASE}{\mathop{\mathbf{case}}}
299 % \newcommand{\CLASS}{\mathop{\mathbf{class}}}
300 % \newcommand{\WHERE}{\mathop{\mathbf{where}}}
301 % \newcommand{\Eq}{\mathop{\mathsf{Eq}}}
302 % \newcommand{\Ord}{\mathop{\mathsf{Ord}}}
303 %
304 % \setlength{\arraycolsep}{.5ex}
305 %
306 % \begin{figure}
307 % \[
308 % \begin{pmboxed}
309 % \column{left}{l}
310 % \column{body}{l}
311 % \column{end}{l}
312 % \column{rel}{c}
313 % \column{rhs}{l}
314 % \>[left] \CLASS\ (\Eq\TA)\Rightarrow\Ord\TA\WHERE \\
315 % \>[left] \qquad\
316 % \>[body] \HsVar{compare}
317 % \>[rel] ::
318 % \>[rhs] \TA\rightarrow\TA\rightarrow\HsCon{Ordering} \\
319 % \>[left] \qquad\
320 % \>[body] (<),(\leq),(\geq),(>)
321 % \>[rel] ::
322 % \>[rhs] \TA\rightarrow\TA\rightarrow\HsCon{Bool}
323 % \\
324 % \>[left] \qquad\
325 % \>[body] \HsVar{max},\HsVar{min}
326 % \>[rel] ::
327 % \>[rhs] \TA\rightarrow\TA\rightarrow\HsCon{Bool}
328 % \\
329 % \\
330 % \>[body] \mbox{--- Minimal complete definition:
331 % $(\leq)$ or $\HsVar{compare}$}
332 % \\
333 % \>[body] \mbox{--- using $\HsVar{compare}$ can be more efficient
334 % for complex types}
335 % \\
336 % \column{cgrd}{l}
337 % \>[body] \HsVar{compare}\TX\TY
338 % \>[cgrd] \vert\ \TX\equiv\TY
339 % \>[rel] =
340 % \>[rhs] \HsCon{EQ}
341 % \\
342 % \>[cgrd] \vert\ \TX\leq\TY
343 % \>[rel] =
344 % \>[rhs] \HsCon{LT}
345 % \\
346 % \>[cgrd] \vert\ \HsVar{otherwise}
347 % \>[rel] =
348 % \>[rhs] \HsCon{GT}
349 % \\
350 % \>[body] \TX\leq\TY
351 % \>[rel] =
352 % \>[rhs] \HsVar{compare}\TX\TY\not\equiv\HsCon{GT}
353 % \\
354 % \>[body] \TX < \TY
355 % \>[rel] =
356 % \>[rhs] \HsVar{compare}\TX\TY\equiv\HsCon{LT}
357 % \\
358 % \>[body] \TX\geq\TY
359 % \>[rel] =
360 % \>[rhs] \HsVar{compare}\TX\TY\not\equiv\HsCon{LT}
361 % \\
362 % \>[body] \TX > \TY
363 % \>[rel] =
364 % \>[rhs] \HsVar{compare}\TX\TY\equiv\HsCon{GT}
365 % \\
366 % \>[body] \HsVar{max}\TX\TY
367 % \>[cgrd] \vert\ \TX\leq\TY
368 % \>[rel] =
369 % \>[rhs] \TY
370 % \\
371 % \>[cgrd] \vert\ \HsVar{otherwise}
372 % \>[rel] =
373 % \>[rhs] \TX
374 % \\
375 % \>[body] \HsVar{min}\TX\TY
376 % \>[cgrd] \vert\ \TX\leq\TY
377 % \>[rel] =
378 % \>[rhs] \TX
379 % \\
380 % \>[cgrd] \vert\ \HsVar{otherwise}
381 % \>[rel] =
382 % \>[rhs] \TY
383 % \end{pmboxed}
384 % \]
385 % \caption{Haskell code example}
386 % \label{tab:haskell}
387 % \end{figure}
388 %
389 % Of course, this could be useful for other programming languages
390 % as well. In fact, lhs2\TeX\ can be tweaked to process several
391 % experimental languages that are based on Haskell, but I can imagine
392 % that this package could generally prove useful to typeset program
393 % code.
394 %
395 % \section{Other applications}
396 %
397 % Although I have written this package for a specific purpose, I
398 % am very much interested to hear of other potential application
399 % areas. Please tell me if you found a use for this package and
400 % do not hesitate to ask for additional features that could convince
401 % you to use the package for something.
402 %
403 % \section{The \textsf{lazylist} package}
404 %
405 % Internally, this package makes use of Alan Jeffrey's excellent
406 % \textsf{lazylist} package, which provides an implementation of
407 % the lambda calculus using fully expandable control sequences.
408 % Unfortunately, |lazylist.sty| is not included in most common
409 % \TeX\ distributions, so you might need to fetch it from CTAN
410 % separately.
411 %
412 % \section{Reference}
413 %
414 % \subsection{Setup}
415 %
416 % New in v0.8: We allow to implicitly define columns by just
417 % using the names during table content specification, without
418 % having declared them formally using |\column| (see below).
419 %
420 % \DescribeMacro{\nodefaultcolumn}
421 % By default, though, this behaviour is turned off, because the
422 % use of a misspelled column is often an error. Thus, by default
423 % or after the command |\nodefaultcolumn| is used, all columns
424 % must be declared.
425 %
426 % \DescribeMacro{\defaultcolumn}
427 % Using a statement of the form
428 % |\defaultcolumn{|\meta{spec}|}|, implicit columns can
429 % be activated. All undefined columns that are encountered
430 % will be assumed to have format string \meta{spec}.
431 %
432 % \subsection{The environments}
433 %
434 % \DescribeEnv{pboxed}
435 % \DescribeEnv{ptboxed}
436 % \DescribeEnv{pmboxed}
437 % \DescribeEnv{ptabular}
438 % \DescribeEnv{parray}
439 % New in v0.8: There are now five environments that this package
440 % provides: in addition to the former |ptabular|, |parray|, and
441 % |pboxed|, there is now also |ptboxed| and |pmboxed|.
442 % The environment |pboxed|
443 % typesets the material in boxes of the calculated length, but in
444 % normal paragraph mode. The advantage is that
445 % there can be page breaks within the table.
446 % Note that you should start a new (probably nonindented)
447 % paragraph before beginning a |pboxed|. All lines that a |pboxed|
448 % produces are of equal length, so it should be possible to center
449 % or right-align the material.
450
451 % Both |ptboxed| and |pbmoxed| are like |pboxed|, but
452 % pre-wrapped in a |tabular| or |array| environment,
453 % respectively, and
454 % thus not page-breakable but less fragile (or should I just
455 % say: easier to use) than the unwrapped |pboxed|.
456 % With those, there
457 % is no need for |ptabular| and |parray| anymore --
458 % which were directly based on (and translated into) usual
459 % |tabular| and |array| environments as provided by the |array|
460 % package. The environments are still supported, to ensure
461 % compatibility, but they are mapped to |ptboxed| and |pmboxed|,
462 % respectively.
463 %
464 % The |pmboxed| and |parray| environments assume math mode, whereas
465 % |ptboxed| and |ptabular| assume text mode. The |pboxed| environment
466 % works in both text and math modes and adapts to whatever is
467 % currently active.
468 %
469 % I wrote in previous versions that one additional environment, namely
470 % |plongtable|, a poly-version of the |longtable| environment, was planned.
471 % Page-breaking of |pboxed| works fine, and I do not see a
472 % real need for a |plongtable| environment anymore. If you would like
473 % it anyway, feel free to mail me and inform me about the advantages
474 % it would have over |pboxed|.
475 %
476 % The interface is the same for all of the environments.
477 %
478 % \subsection{The interface}
479 %
480 % \begin{center}
481 % \textbf{(Note: this has changed significantly in v0.8!)}
482 % \end{center}
483 %
484 % In each of the environments, the following commands can be
485 % used. All other commands should be used with care if placed
486 % directly inside such an environment: the contents of a polytable
487 % are processed multiple times; hence, if your commands generate
488 % output, the output will end up multiple times in the final
489 % document, and destroy your layout.
490 %
491 % \DescribeMacro{\column}
492 % With |\column[|\meta{dimen}|]{|\meta{columnid}|}{|\meta{spec}|}|,
493 % a new column
494 % \meta{columnid} is specified. The name of the column can be
495 % any sequence of alphanumerical characters. The \meta{spec} is
496 % a format string for that particular column, and it can contain
497 % the same constructs that can be used in format strings of normal
498 % tables or arrays.
499 % However, it must only contain the description for \emph{one}
500 % column.
501 %
502 % As long as the save/restore feature (explained below) is not used,
503 % |\column| definitions are local to one table.
504 % One can define a column multiple times within one table:
505 % a warning will be produced,
506 % and the second format string will be used for the complete table.
507 %
508 % Columns must be defined before use when implicit columns using
509 % |\defaultcolumn| are disabled.
510 %
511 % A minimal horizontal position \meta{dimen} (relative to the
512 % beginning of the table) can be specified for a column, which
513 % defaults to 0pt.
514 %
515 % \DescribeMacro{\=}
516 % The command |\={|\meta{fromid}|}[|\meta{spec}|]| instructs the
517 % package to prepare for a new entry starting at column \meta{fromid}.
518 % Everything that follows the command, up to the next
519 % interface-command (except |\column|) or the end of the environment
520 % is interpreted as contents of the entry and can be arbitrary
521 % \LaTeX\ code that has balanced grouping. The column specifier
522 % \meta{spec} can be used to define a different formatting for this
523 % particular entry. If the specifier starts with an exclamation mark
524 % (|!|), it will be used as specifier for all entries of that column.
525 % The use of multiple exclamation-mark specifiers for the same
526 % column name gives precedence to the last one, but you should not
527 % rely on this behaviour as it may change in the future.
528 %
529 % Note that, contrary to normal \LaTeX\ behaviour, the second argument
530 % is the optional argument. Therefore, if an entry starts with an
531 % opening bracket |([)|,
532 % and the optional argument is omitted, a |\relax| should be inserted
533 % between command and opening bracket, as otherwise the entry would
534 % be treated as the optional argument.
535 %
536 % \DescribeMacro{\>}
537 % The command |\>[|\meta{fromid}|][|\meta{spec}|]| is a variant of
538 % |\=| where both arguments are optional. If no column name is given
539 % then the current column name, postfixed by a dot (|.|), is assumed
540 % as column name. At the beginning of a line, a single dot (|.|)
541 % will be assumed as a name. The \meta{spec} argument has the same
542 % behaviour as for |\=|. Note that if the specifier is given, the
543 % column name must be given as well, but may be left empty if the
544 % default choice is desired. For instance, |\>[][r]|, will move to
545 % the next column, which will be typeset right-aligned.
546 %
547 % \DescribeMacro{\<}
548 % The command |\<[|\meta{fromid}|]| ends the current entry at the
549 % boundary specified by \meta{fromid}. This macro can be used instead
550 % of |\>| or |\=| if an entry should be ended without immediately
551 % starting a new one. Differences in behaviour occur if \meta{fromid}
552 % is associated with a non-trivial column format string.
553 % TODO: Improve this explanation.
554 %
555 % \DescribeMacro{\fromto}
556 % The call |\fromto{|\meta{fromid}|}{|\meta{toid}|}{|\meta{text}|}|
557 % will typeset \meta{text} in the current line, starting at column
558 % \meta{fromid} and ending before column \meta{toid}, using the
559 % format string specified for \meta{fromid}.
560 %
561 % A line of a table usually consists of multiple |\fromto| statements.
562 % Each statement's starting column should be either the same as the
563 % end column of the previous statement, or it will be assumed that
564 % the start column is located somewhere to the right of the previous
565 % end column. The user is responsible to not introduce cycles in the
566 % (partial) order of columns. If such a cycle is specified, the
567 % current algorithm will loop, causing a |dimension too large| error
568 % ultimately. TODO: catch this error.
569 %
570 % \DescribeMacro{\\}
571 % \DescribeMacro{\nextline}
572 % The command |\\| (or, now deprecated, |\nextline|) ends one line
573 % and begins the next.
574 % There is no need to end the last line. One can pass an optional
575 % argument, as in |\\[|\meta{dimen}|]|, that will add
576 % \meta{dimen} extra space between the lines.
577 %
578 % \subsection{A warning}
579 %
580 % The contents of the table are processed multiple times because
581 % the widths of the entries are measured. Global assignments that
582 % modify registers and similar things can thus result in unexpected
583 % behaviour.
584 % New in v0.7: \LaTeX\ counters (i.e., counters defined by
585 % |\newcounter|) are protected now. They will be reset after
586 % each of the trial runs.
587 %
588 % \subsection{Saving column width information}
589 %
590 % Sometimes, one might want to reuse not only the same column, but
591 % exactly the same alignment as in a previous table. An example
592 % would be a fragment of program code, which has been broken into
593 % several pieces, with documentation paragraphs added in between.
594 %
595 % \DescribeMacro{\savecolumns}
596 % \DescribeMacro{\restorecolumns}
597 % With |\savecolumns[|\meta{setid}|]|, one can save the information
598 % of the current table for later reuse. The name |setid| can be an
599 % arbitrary sequence of alphanumeric characters. It does \emph{not} share
600 % the same namespace as the column names. The argument is optional;
601 % if it is omitted, a default name is assumed. Later, one can restore
602 % the information (multiple times, if needed) in other tables, by
603 % issuing a |\restorecolumns[|\meta{setid}|]|.
604 %
605 % This feature requires to pass information backwards in the general
606 % case, as column widths in later environments using one specific
607 % column set might influence the layout of earlier environments.
608 % Therefore, information is written into the |.aux| file, and
609 % sometimes, a warning is given that a rerun is needed. Multiple
610 % reruns might be required to get all the widths right.
611 %
612 % I have tried very hard to avoid producing rerun warnings infinitely
613 % except if there are really cyclic dependencies between columns.
614 % Still, if it happens or something seems to be broken, it often
615 % is a good idea to remove the |.aux| file and start over. Be sure
616 % to report it as a bug, though.
617 %
618 % Figure~\ref{tab:saverestore} is an example of the Haskell code
619 % example with several comments inserted. The source of this file
620 % shows how to typeset the example.
621 % \begin{figure}
622 % \setlength{\parindent}{0pt}%
623 % \setlength{\parskip}{1ex}%
624 % \hrule
625 % We introduce a new type class $\Ord$ for objects that admit
626 % an ordering. It is based on the $\Eq$ class:
627 %
628 % \(
629 % \begin{pboxed}
630 % \savecolumns
631 % \column{left}{>{\quad}l}
632 % \column{end}{l}
633 % \fromto{left}{end}{\CLASS\ (\Eq\TA)\Rightarrow\Ord\TA\WHERE}
634 % \end{pboxed}
635 % \)
636 %
637 % The next three lines give the type signatures for all
638 % the methods of the class.
639 %
640 % \(
641 % \begin{pboxed}
642 % \restorecolumns
643 % \column{body}{l}
644 % \column{rel}{c}
645 % \column{rhs}{l}
646 % \fromto{left}{body}{\qquad\ }
647 % \fromto{body}{rel}{\HsVar{compare}}
648 % \fromto{rel}{rhs}{::}
649 % \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Ordering}}
650 % \\
651 % \fromto{left}{body}{\qquad\ }
652 % \fromto{body}{rel}{(<),(\leq),(\geq),(>)}
653 % \fromto{rel}{rhs}{::}
654 % \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Bool}}
655 % \\
656 % \fromto{left}{body}{\qquad\ }
657 % \fromto{body}{rel}{\HsVar{max},\HsVar{min}}
658 % \fromto{rel}{rhs}{::}
659 % \fromto{rhs}{end}{\TA\rightarrow\TA\rightarrow\HsCon{Bool}}
660 % \\[1ex]
661 % \fromto{body}{end}{\mbox{--- Minimal complete definition:
662 % $(\leq)$ or $\HsVar{compare}$}}
663 % \\
664 % \fromto{body}{end}{\mbox{--- using $\HsVar{compare}$ can be more efficient
665 % for complex types}}
666 % \end{pboxed}
667 % \)
668 %
669 % As the comment above says, it is sufficient to define either
670 % $(\leq)$ or $\HsVar{compare}$ to get a complete instance. All
671 % of the class methods have default definitions. First, we can
672 % define $\HsVar{compare}$ in terms of $(\leq)$. The result type
673 % of $\HsVar{compare}$ is an $\HsCon{Ordering}$, a type consisting
674 % of only three values: $\HsCon{EQ}$ for ``equality'',
675 % $\HsCon{LT}$ for ``less than'', and $\HsCon{GT}$ for ``greater
676 % than''.
677 %
678 % \(
679 % \begin{pboxed}
680 % \restorecolumns
681 % \column{cgrd}{l}
682 % \fromto{body}{cgrd}{\HsVar{compare}\TX\TY}
683 % \fromto{cgrd}{rel}{\vert\ \TX\equiv\TY}
684 % \fromto{rel}{rhs}{=}
685 % \fromto{rhs}{end}{\HsCon{EQ}}
686 % \\
687 % \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
688 % \fromto{rel}{rhs}{=}
689 % \fromto{rhs}{end}{\HsCon{LT}}
690 % \\
691 % \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
692 % \fromto{rel}{rhs}{=}
693 % \fromto{rhs}{end}{\HsCon{GT}}
694 % \end{pboxed}
695 % \)
696 %
697 % All the other comparison operators can be defined
698 % in terms of $\HsCon{compare}$:
699 %
700 % \(
701 % \begin{pboxed}
702 % \restorecolumns
703 % \fromto{body}{rel}{\TX\leq\TY}
704 % \fromto{rel}{rhs}{=}
705 % \fromto{rhs}{end}{\HsVar{compare}\TX\TY\not\equiv\HsCon{GT}}
706 % \\
707 % \fromto{body}{rel}{\TX < \TY}
708 % \fromto{rel}{rhs}{=}
709 % \fromto{rhs}{end}{\HsVar{compare}\TX\TY\equiv\HsCon{LT}}
710 % \\
711 % \fromto{body}{rel}{\TX\geq\TY}
712 % \fromto{rel}{rhs}{=}
713 % \fromto{rhs}{end}{\HsVar{compare}\TX\TY\not\equiv\HsCon{LT}}
714 % \\
715 % \fromto{body}{rel}{\TX > \TY}
716 % \fromto{rel}{rhs}{=}
717 % \fromto{rhs}{end}{\HsVar{compare}\TX\TY\equiv\HsCon{GT}}
718 % \end{pboxed}
719 % \)
720 %
721 % Finally, there are default definitions for
722 % $\HsVar{max}$ and $\HsVar{min}$ in terms of $(\leq)$.
723 %
724 % \(
725 % \begin{pboxed}
726 % \restorecolumns
727 % \fromto{body}{cgrd}{\HsVar{max}\TX\TY}
728 % \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
729 % \fromto{rel}{rhs}{=}
730 % \fromto{rhs}{end}{\TY}
731 % \\
732 % \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
733 % \fromto{rel}{rhs}{=}
734 % \fromto{rhs}{end}{\TX}
735 % \\
736 % \fromto{body}{cgrd}{\HsVar{min}\TX\TY}
737 % \fromto{cgrd}{rel}{\vert\ \TX\leq\TY}
738 % \fromto{rel}{rhs}{=}
739 % \fromto{rhs}{end}{\TX}
740 % \\
741 % \fromto{cgrd}{rel}{\vert\ \HsVar{otherwise}}
742 % \fromto{rel}{rhs}{=}
743 % \fromto{rhs}{end}{\TY}
744 % \end{pboxed}
745 % \)
746 % \hrule
747 % \caption{Commented Haskell code example}
748 % \label{tab:saverestore}
749 % \end{figure}
750 %
751 % \section{The Code}
752 % \begin{macrocode}
753 %<*package>
754 \NeedsTeXFormat{LaTeX2e}
755 \ProvidesPackage{polytable}%
756 [2020/07/14 v0.8.6 `polytable' package (Andres Loeh)]
757 % \end{macrocode}
758 % New in v0.7.2:
759 % The \textsf{amsmath} package clashes with \textsf{lazylist}:
760 % both define the command |\And|. Although it would certainly
761 % be better to find another name in \textsf{lazylist}, we
762 % take precautions for now. (Note that this will still fail
763 % if \textsf{lazylist} is already loaded -- but then it's not
764 % our problem \dots
765 % \begin{macrocode}
766 \let\PT@original@And\And
767 \let\PT@original@Not\Not
768 \RequirePackage{lazylist}
769 \let\PT@And\And
770 \let\PT@Not\Not
771 \def\PT@prelazylist
772 {\let\And\PT@And
773 \let\Not\PT@Not}
774 \def\PT@postlazylist
775 {\let\And\PT@original@And
776 \let\Not\PT@original@Not}
777 \PT@postlazylist
778 \RequirePackage{array}
779 % \end{macrocode}
780 %
781 % The option debug will cause (a considerable amount of)
782 % debugging output to be printed. The option info is a
783 % compromise, printing some status information for each table,
784 % but no real debugging information. The option silent, on
785 % the other hand, will prevent certain warnings from being
786 % printed.
787 % \begin{macrocode}
788 \DeclareOption{debug} {\AtEndOfPackage\PT@debug}
789 \DeclareOption{info} {\AtEndOfPackage\PT@info}
790 \DeclareOption{silent}{\AtEndOfPackage\PT@silent}
791 % \end{macrocode}
792 %
793 % First, we declare a couple of registers that we will need later.
794 % \begin{macrocode}
795 \newdimen\PT@colwidth
796 \newcount\PT@cols
797 \newcount\PT@table
798 \newtoks\PT@toks
799 \newif\ifPT@changed
800 \newread\PT@in
801 \newwrite\PT@out
802 % \end{macrocode}
803 % In |\PT@allcols|, we will store the list of all columns, as a
804 % list as provided by the \textsf{lazylist} package. We initialise
805 % it to the empty list, which is represented by |\Nil|. In v0.9,
806 % we will have a second list that only contains the public columns.
807 % \begin{macrocode}
808 \def\PT@allcols{\Nil}
809 %\def\PT@allpubliccols{\Nil}
810 \let\PT@infromto\empty
811 % \end{macrocode}
812 % These are flags and truth values. TODO: Reduce and simplify.
813 % \begin{macrocode}
814 \let\PT@currentwidths\empty
815 \def\PT@false{0}
816 \def\PT@true{1}
817 \let\PT@inrestore\PT@false
818 % \end{macrocode}
819 %
820 % \begin{macro}{\defaultcolumn}
821 % \begin{macro}{\nodefaultcolumn}
822 % \begin{macro}{\PT@defaultcolumnspec}
823 % The macro |\PT@defaultcolumnspec| contains, if defined, the
824 % current default specifier that is assumed for undefined columns.
825 % The other two commands can be used to set the specifier.
826 % \begin{macrocode}
827 \newcommand{\defaultcolumn}[1]{\gdef\PT@defaultcolumnspec{#1}}
828 \newcommand{\nodefaultcolumn}{\global\let\PT@defaultcolumnspec\undefined}
829 \DeclareOption{defaultcolumns}{\defaultcolumn{l}}
830 % \end{macrocode}
831 % \end{macro}
832 % \end{macro}
833 % \end{macro}
834 %
835 % \begin{macro}{\memorytables}
836 % \begin{macro}{\disktables}
837 % \begin{macro}{\PT@add}
838 % \begin{macro}{\PT@split}
839 % \begin{macro}{\PT@prearewrite}
840 % \begin{macro}{\PT@preareread}
841 % \begin{macro}{\PT@finalize}
842 % These macros steer where the end-column queue is stored, which
843 % can be either in memory or on disk. The default is on disk,
844 % because that's more reliable for large tables. There is a package
845 % option |memory| to make |\memorytables| the default.
846 % \begin{macrocode}
847 \newcommand{\memorytables}{%
848 \let\PT@preparewrite\@gobble
849 \let\PT@add \PT@addmem
850 \let\PT@prepareread \PT@preparereadmem
851 \let\PT@split \PT@splitmem
852 \let\PT@finalize \relax
853 }
854 \newcommand{\disktables}{%
855 \let\PT@preparewrite\PT@preparewritefile
856 \let\PT@add \PT@addfile
857 \let\PT@prepareread \PT@preparereadfile
858 \let\PT@split \PT@splitfile
859 \let\PT@finalize \PT@finalizefile
860 }
861 \DeclareOption{memory}{\AtEndOfPackage\memorytables}
862 \ProcessOptions
863 % \end{macrocode}
864 % \end{macro}
865 % \end{macro}
866 % \end{macro}
867 % \end{macro}
868 % \end{macro}
869 % \end{macro}
870 % \end{macro}
871 %
872 % \begin{macro}{\PT@debug}
873 % \begin{macro}{\PT@info}
874 % \begin{macro}{\PT@debug@}
875 % \begin{macro}{\PT@typeout@}
876 % \begin{macro}{\PT@silent}
877 % \begin{macro}{\PT@warning}
878 % Similar to the \textsf{tabularx} package, we add macros to
879 % print debugging information to the log. Depending on package
880 % options, we can set or unset them.
881 % \begin{macrocode}
882 \newcommand*{\PT@debug}
883 {\def\PT@debug@ ##1{\typeout{(polytable) ##1}}
884 \PT@info}
885 \newcommand*{\PT@info}
886 {\def\PT@typeout@ ##1{\typeout{(polytable) ##1}}}
887 \let\PT@debug@\@gobble
888 \let\PT@typeout@\@gobble
889 \def\PT@warning{\PackageWarning{polytable}}%
890 \def\PT@silent
891 {\let\PT@typeout@\@gobble\let\PT@warning\@gobble}
892 % \end{macrocode}
893 % \end{macro}
894 % \end{macro}
895 % \end{macro}
896 % \end{macro}
897 % \end{macro}
898 % \end{macro}
899 %
900 % \begin{macro}{\PT@aligndim}
901 % \begin{macro}{\PT@aligncol}
902 % The first is (almost) stolen from the \textsf{tabularx}-package, to nicely
903 % align dimensions in the log file. TODO: fix some issues.
904 % The other command is for column names.
905 % \begin{macrocode}
906 \def\PT@aligndim#1#2#3\@@{%
907 \ifnum#1=0
908 \if #2p%
909 \PT@[email protected]\space\space\space\space\space\@@
910 \else
911 \PT@aligndim@#1#2#3\space\space\space\space\space\space\space\space\@@
912 \fi
913 \else
914 \PT@aligndim@#1#2#3\space\space\space\space\space\space\space\space\@@
915 \fi}
916
917 \def\PT@aligndim@#1.#2#3#4#5#6#7#8#9\@@{%
918 \ifnum#1<10 \space\fi
919 \ifnum#1<100 \space\fi
920 \ifnum#1<\@m\space\fi
921 \ifnum#1<\@M\space\fi
922 #1.#2#3#4#5#6#7#8\space\space}
923
924 \def\PT@aligncol#1{%
925 \PT@aligncol@#1\space\space\space\space\space\space\space\space\@@}
926
927 \def\PT@aligncol@#1#2#3#4#5#6#7#8#9\@@{%
928 #1#2#3#4#5#6#7#8\space\space}
929 % \end{macrocode}
930 % \end{macro}
931 % \end{macro}
932 %
933 % \begin{macro}{\PT@rerun}
934 % This macro can be called at a position where we know that
935 % we have to rerun \LaTeX\ to get the column widths right. It
936 % issues a warning at the end of the document.
937 % \begin{macrocode}
938 \def\PT@rerun
939 {\PT@typeout@{We have to rerun LaTeX ...}%
940 \AtEndDocument
941 {\PackageWarning{polytable}%
942 {Column widths have changed. Rerun LaTeX.\@gobbletwo}}%
943 \global\let\PT@rerun\relax}
944 % \end{macrocode}
945 % \end{macro}
946 %
947 % \begin{macro}{\PT@currentcol}
948 % \begin{macro}{\PT@currentcolumn}
949 % Both macros are used during typesetting to store the current
950 % column. The differences are subtle. TODO: remove
951 % one of the two, if possible. The |\PT@currentcol| variant
952 % contains the internal name, whereas the |\PT@currentcolumn|
953 % variant contains the external name.
954 % \end{macro}
955 % \end{macro}
956 %
957 % \subsection{Macro definition tools}
958 %
959 % \begin{macro}{\PT@listopmacro}
960 % \begin{macro}{\PT@consmacro}
961 % \begin{macro}{\PT@appendmacro}
962 % This assumes that |#2| is a list macro and |#3| is a new list element.
963 % The macro |#2| should, after the call, expand to the list with the
964 % new element |#1|ed. Because we don't know the number of tokens in |#3|,
965 % we use a temporary macro |\PT@temp| (which is used frequently throughout
966 % the package).
967 % \begin{macrocode}
968 \def\PT@listopmacro #1#2#3% #1 #3 to the list #2
969 {\def\PT@temp{#1{#3}}%
970 \expandafter\expandafter\expandafter
971 \def\expandafter\expandafter\expandafter
972 #2\expandafter\expandafter\expandafter
973 {\expandafter\PT@temp\expandafter{#2}}}
974
975 \def\PT@consmacro{\PT@listopmacro\Cons}
976 \def\PT@appendmacro{\PT@listopmacro\Cat}
977 % \end{macrocode}
978 % \end{macro}
979 % \end{macro}
980 % \end{macro}
981 % The follwing macro can be used to add something to
982 % the end of a control structure.
983 % \begin{macrocode}
984 \def\PT@gaddendmacro #1#2% add #2 to the end of #1
985 {\PT@expanded{\gdef #1}{#1#2}}
986 % \end{macrocode}
987 % \begin{macro}{\PT@expanded}
988 % This macro expands its second argument once before passing
989 % it to the first argument. It is like |\expandafter|, but
990 % works on grouped arguments instead of tokens.
991 % \begin{macrocode}
992 \def\PT@expanded #1#2%
993 {\expandafter\Twiddle\expandafter\Identity\expandafter{#2}{#1}}
994 % \end{macrocode}
995 % \end{macro}
996 % \begin{macro}{\PT@enamedef}
997 % This is much like |\@namedef|, but it expands |#2| once.
998 % \begin{macrocode}
999 \def\PT@enamedef #1% sets name #1 to the expansion of #2
1000 {\PT@expanded{\@namedef{#1}}}
1001 % \end{macrocode}
1002 % \end{macro}
1003 %
1004 % \begin{macro}{\PT@addoptargtomacro}
1005 % \begin{macro}{\PT@addargtomacro}
1006 % \begin{macrocode}
1007 \def\PT@addoptargtomacro
1008 {\PT@add@argtomacro\PT@makeoptarg}
1009 \def\PT@addargtomacro
1010 {\PT@add@argtomacro\PT@makearg}
1011
1012 \def\PT@add@argtomacro#1#2#3%
1013 {\PT@expanded{\PT@expanded{\gdef\PT@temp}}{\csname #3\endcsname}%
1014 #1%
1015 \PT@expanded{\PT@gaddendmacro{#2}}{\PT@temp}}
1016
1017 \def\PT@makeoptarg%
1018 {\PT@expanded{\def\PT@temp}{\expandafter[\PT@temp]}}
1019 \def\PT@makearg%
1020 {\PT@expanded{\def\PT@temp}{\expandafter{\PT@temp}}}
1021 % \end{macrocode}
1022 % \end{macro}
1023 %
1024 % \begin{macro}{\PT@gobbleoptional}
1025 % Gobbles one optional argument. Ignores spaces.
1026 % \begin{macrocode}
1027 \newcommand*{\PT@gobbleoptional}[1][]{\ignorespaces}
1028 % \end{macrocode}
1029 % \end{macro}
1030 % \end{macro}
1031 %
1032 % \begin{macro}{\PT@addmem}
1033 % \begin{macro}{\PT@splitmem}
1034 % \begin{macro}{\PT@addfile}
1035 % \begin{macro}{\PT@splitfile}
1036 % \begin{macro}{\PT@preparereadmem}
1037 % \begin{macro}{\PT@preparereadfile}
1038 % \begin{macro}{\PT@preparewrite}
1039 % \begin{macro}{\PT@finalizefile}
1040 % \begin{macro}{\PT@queuefilename}
1041 % The following macros handle a simple queue of names.
1042 % With |\PT@addmem|, a name is added to the end of the queue. Using
1043 % |\PT@splitmem|, the first element of the queue is bound to another
1044 % command. As a replacement, we also define |\PT@addfile| and
1045 % |\PT@splitfile|, that implement the queue in a file.
1046 % \begin{macrocode}
1047 \def\PT@addmem#1#2{\PT@gaddendmacro #2{\PT@elt{#1}}}
1048 \def\PT@splitmem#1#2{#1\PT@nil{#2}{#1}}
1049
1050 \def\PT@elt#1#2\PT@nil#3#4{\gdef #3{#1}\gdef #4{#2}}
1051
1052 \def\PT@queuefilename{\jobname.ptb}
1053
1054 % the \empty at the end consumes the newline space
1055 \def\PT@addfile#1#2{%
1056 \immediate\write #2{\string\def\string\PTtemp{#1}\string\empty}}
1057
1058 \def\PT@splitfile#1#2{%
1059 \ifeof #1%
1060 \let #2=\empty
1061 \else
1062 \read #1 to#2%
1063 %\show #2%
1064 #2% hack, because it essentially ignores #2
1065 \PT@expanded{\def #2}{\PTtemp}%
1066 %\show #2%
1067 \fi}
1068
1069 %\def\strip#1{\def #1{\expandafter\@strip #1\@dummy}}
1070 %\def\@strip#1\@dummy{#1}
1071
1072 \def\PT@preparereadmem#1#2{%
1073 \global\let #1=#2}
1074
1075 \def\PT@preparewritefile#1{%
1076 \immediate\openout\PT@out\PT@queuefilename\relax
1077 \let #1\PT@out}
1078
1079 \def\PT@preparereadfile#1#2{%
1080 \immediate\closeout\PT@out
1081 \openin\PT@in\PT@queuefilename\relax
1082 \let #1\PT@in}
1083
1084 \def\PT@finalizefile{%
1085 \closein\PT@in}
1086
1087 \disktables
1088 % \end{macrocode}
1089 % \end{macro}
1090 % \end{macro}
1091 % \end{macro}
1092 % \end{macro}
1093 % \end{macro}
1094 % \end{macro}
1095 % \end{macro}
1096 % \end{macro}
1097 % \end{macro}
1098 %
1099 % \subsection{The environment}
1100 %
1101 % The general idea is to first scan the contents of the environment
1102 % and store them in a token register. In a few test runs, the positions
1103 % of the column borders are determined. After that, the table is
1104 % typeset by producing boxes of appropriate size.
1105 %
1106 % \begin{macro}{\beginpolytable}
1107 % This macro starts the environment. It should, however, not
1108 % be called directly, but rather in a \LaTeX\ environment.
1109 % We initialize the
1110 % token register to the empty string and then start scanning.
1111 % \begin{macrocode}
1112 \newcommand*{\beginpolytable}%
1113 % \end{macrocode}
1114 % We save the current enclosing \LaTeX\ environment in
1115 % |\PT@environment|. This will be the |\end| we will be
1116 % looking for, and this will be the environment we manually
1117 % close in the end.
1118 % \begin{macrocode}
1119 {\edef\PT@environment{\@currenvir}%
1120 \begingroup
1121 % new in v0.7: save counters
1122 \PT@savecounters
1123 \PT@toks{}% initialise token register
1124 \PT@scantoend}
1125 % \end{macrocode}
1126 % \end{macro}
1127 % \begin{macro}{\endpolytable}
1128 % This is just defined for convenience.
1129 % \begin{macrocode}
1130 \let\endpolytable=\relax
1131 % \end{macrocode}
1132 % \end{macro}
1133 % \begin{macro}{\PT@scantoend}
1134 % Whenenver an |\end| is encountered, we check if it really
1135 % ends the current environment. We store the tokens we have
1136 % read. Once we have found the end of the environment, we
1137 % initialize the column queue and column reference,
1138 % |\PT@columnqueue| and |\PT@columnreference|. The new interface
1139 % commands build the queue during the first test run, containing
1140 % the end columns of specific entries of the table. Later, the
1141 % queue is copied to be the reference, and in the typesetting run,
1142 % the information is used to compute the correct box widths.
1143 % We start with an empty queue, and set the reference to |\undefined|,
1144 % because it is not yet needed. TODO: queue and reference must be
1145 % global variables at the moment; try to change that.
1146 % \begin{macrocode}
1147 \newcommand{\PT@scantoend}% LaTeX check
1148 \long\def\PT@scantoend #1\end #2%
1149 {\PT@toks\expandafter{\the\PT@toks #1}%
1150 \def\PT@temp{#2}%
1151 \ifx\PT@temp\PT@environment
1152 \global\let\PT@columnqueue \empty
1153 \global\let\PT@columnreference \undefined
1154 \PT@preparewrite\PT@columnqueue
1155 \expandafter\PT@getwidths
1156 \else
1157 \PT@toks\expandafter{\the\PT@toks\end{#2}}%
1158 \expandafter\PT@scantoend
1159 \fi}
1160 % \end{macrocode}
1161 % \end{macro}
1162 % \begin{macro}{\PT@getwidths}
1163 % Here, we make as many test runs as are necessary to
1164 % determine the correct column widths.
1165 % \begin{macrocode}
1166 \def\PT@getwidths
1167 % \end{macrocode}
1168 % We let the |\column| command initialize a column
1169 % in the first run.
1170 % \begin{macrocode}
1171 {\let\column \PT@firstrun@column
1172 % \end{macrocode}
1173 % There is the possibility to save or restore columns.
1174 % This is new in v0.4.
1175 % \begin{macrocode}
1176 \let\savecolumns \PT@savewidths
1177 \let\restorecolumns \PT@restorewidths
1178 % \end{macrocode}
1179 % We \emph{always} define a pseudo-column |@begin@|.
1180 % This denotes the begin of a row. We also define a
1181 % pseudo-column |@end@| denoting the end of a row (as
1182 % of v0.8; and I'm not sure if |@begin@| is still
1183 % needed).
1184 % \begin{macrocode}
1185 \column{@begin@}{@{}l@{}}%
1186 \column{@end@}{}%
1187 \PT@cols=0\relax%
1188 % \end{macrocode}
1189 % The two other commands that are allowed inside of the
1190 % environment, namely |\fromto| and |\\| are
1191 % initialized. The |\fromto| command may increase the
1192 % current widths of some columns, if necessary, whereas
1193 % |\\| just resets the counter that keeps track
1194 % of the ``current'' column, to |0|. The command
1195 % |\nextline| is an old name for |\\|.
1196 % \begin{macrocode}
1197 \let\fromto \PT@fromto
1198 \let\PT@processentry \PT@checkwidth
1199 \let\PT@scanbegin \PT@scanbeginfree
1200 \let\\= \PT@resetcolumn
1201 \let\nextline \PT@resetcolumn
1202 \let\>= \PT@fromopt
1203 \let\== \PT@from
1204 \let\<= \PT@toopt
1205 \global\PT@changedfalse % nothing has changed so far
1206 \PT@resetcolumn % we are at the beginning of a line
1207 % \end{macrocode}
1208 % Now we are ready for a test run.
1209 % \begin{macrocode}
1210 \the\PT@toks
1211 \@ifundefined{PT@scanning}%
1212 {}{\PT@resetcolumn\relax}%
1213 % \end{macrocode}
1214 % After the first run, we print extra information.
1215 % We use the contents of the macro |\column| to check
1216 % whether we are in the first run, because it will be
1217 % reset below for all other runs to do nothing.
1218 % \begin{macrocode}
1219 \ifx\column\PT@otherrun@column
1220 \else
1221 % we are in first run, print extra info
1222 \PT@prelazylist
1223 \PT@typeout@{\PT@environment: \the\PT@cols\space columns, %
1224 \PT@Print\PT@allcols}%
1225 \PT@postlazylist
1226 \fi
1227 % \end{macrocode}
1228 % The columns are initialised after the first run.
1229 % Therefore, we make sure that the |\column| command
1230 % won't do much in the other runs. Also, saving and
1231 % restoring columns is no longer needed.
1232 %
1233 % Note that redefining just |\column| or just
1234 % |\PT@firstrun@column| is not enough. In the former
1235 % case, on the next trial run, |\PT@getwidths| will
1236 % restore the old definition. In the latter case, if
1237 % there is no additional trial run, the typesetting
1238 % run will be performed with the wrong definition.
1239 % TODO: I think similar issues exist with saving/restoring?
1240 % \begin{macrocode}
1241 \let\column \PT@otherrun@column
1242 \let\PT@firstrun@column \PT@otherrun@column
1243 \let\savecolumns \PT@gobbleoptional
1244 \let\restorecolumns \PT@gobbleoptional
1245 \let\PT@savewidths \PT@gobbleoptional
1246 \let\PT@restorewidths \PT@gobbleoptional
1247 % \end{macrocode}
1248 % New in v0.7.1: restore counters after each trial run.
1249 % \begin{macrocode}
1250 \PT@restorecounters
1251 % \end{macrocode}
1252 % If some column widths have indeed changed in the
1253 % test run, this will be indicated by the flag
1254 % |\ifPT@changed|. Depending on this flag, we will
1255 % either loop and rerun, or we will continue in
1256 % |\PT@sortcols|.
1257 % \begin{macrocode}
1258 \ifPT@changed
1259 % we need to rerun if something has changed
1260 \PT@typeout@{There were changes; another trial run needed.}%
1261 \expandafter\PT@getwidths
1262 \else
1263 % we are done and can do the sorting
1264 \PT@typeout@{There were no changes; reached fixpoint.}%
1265 \expandafter\PT@sortcols
1266 \fi}
1267 % \end{macrocode}
1268 % \end{macro}
1269 %
1270 % \begin{macro}{\PT@savecounters}
1271 % Save all \LaTeX\ counters so that they can be restored after a
1272 % trial run.
1273 % \begin{macrocode}
1274 \def\PT@savecounters
1275 {\begingroup
1276 \def\@elt ##1%
1277 {\global\csname c@##1\endcsname\the\csname c@##1\endcsname}%
1278 \xdef\PT@restorecounters{\cl@@ckpt}%
1279 \endgroup}
1280 % \end{macrocode}
1281 % \end{macro}
1282 %
1283 % \begin{macro}{\PT@sortcols}
1284 % The column borders are sorted by their horizontal
1285 % position on the page (width). The they get numbered
1286 % consecutively. After that, we are well prepared to
1287 % typeset the table.
1288 % \begin{macrocode}
1289 \def\PT@sortcols
1290 % \end{macrocode}
1291 % First, we sort the list. To make sure that the
1292 % computation is only executed once, we save the
1293 % sorted list by means of an |\edef|. Sorting happens
1294 % with \textsf{lazylist}'s |\Insertsort| which expects
1295 % an order and a list. As order, we provide |\PT@ltwidth|,
1296 % which compares the widths of the columns.
1297 % To prevent expansion of the list structure, given by
1298 % |\Cons| and |\Nil|, we fold the list with the
1299 % |\noexpand|ed versions of the list constructors.
1300 % \begin{macrocode}
1301 {\PT@prelazylist
1302 \edef\PT@sortedlist
1303 {\Foldr{\noexpand\Cons}{\noexpand\Nil}%
1304 {\Insertsort\PT@ltmax\PT@allcols}}%
1305 \PT@typeout@{Sorted columns:}%
1306 \PT@PrintWidth\PT@sortedlist
1307 \PT@postlazylist
1308 % \end{macrocode}
1309 % Now, each column is assigned a number, starting from
1310 % zero.
1311 % \begin{macrocode}
1312 \PT@cols=0\relax%
1313 \PT@prelazylist
1314 \PT@Execute{\Map\PT@numbercol\PT@sortedlist}%
1315 \PT@postlazylist
1316 \edef\PT@lastcol@{\PT@StripColumn\PT@lastcol}%
1317 \PT@typeout@{Numbered successfully, %
1318 last column is \PT@lastcol@}%
1319 % \end{macrocode}
1320 % Now is a good time to save table information, if needed later.
1321 % We will also compare our computed information with the restored
1322 % maximum widths.
1323 % \begin{macrocode}
1324 \ifx\PT@currentwidths\empty
1325 \else
1326 \PT@typeout@{Saving table information for \PT@currentwidths .}%
1327 \PT@expanded\PT@saveinformation\PT@currentwidths
1328 \fi
1329 % \end{macrocode}
1330 % Finally, we can typeset the table.
1331 % \begin{macrocode}
1332 \PT@typeset}
1333 % \end{macrocode}
1334 % \end{macro}
1335 % \begin{macro}{\PT@typeset}
1336 % We redefine |\fromto| and |\\| to their
1337 % final meaning in the typesetting process. The
1338 % |\fromto| statements will be replaced by appropriate
1339 % calls to |\multicolumn|, whereas the |\\| will
1340 % again reset the counter for the current column, but
1341 % also call the table environment's newline macro.
1342 % Again, |\nextline| is allowed as an old name for |\\|.
1343 % \begin{macrocode}
1344 \def\PT@typeset
1345 {\PT@typeout@{Typesetting the table ...}%
1346 \let\PT@processentry \PT@placeinbox
1347 \let\PT@scanbegin \PT@scanbeginwidth
1348 \let\\= \PT@resetandcr
1349 \let\nextline \PT@resetandcr
1350 \PT@prepareread\PT@columnreference\PT@columnqueue
1351 % \end{macrocode}
1352 % The environment that will be opened soon, can, if
1353 % if happens to be |tabular| or |array|, redefines |\\|
1354 % once more, and will redefine it to |\@arraycr|. To
1355 % prevent this, we also set |\@arraycr| to our command.
1356 % \begin{macrocode}
1357 \let\@arraycr \PT@resetandcr
1358 % \end{macrocode}
1359 % The array environments keep each line in a group; therefore
1360 % |\PT@resetcolumn|, when executed during the linebreak, will
1361 % not affect the current column counters. By having
1362 % |\PT@resetcolumn| before entering the environment, we ensure
1363 % that the group reset will have the correct effect anyway.
1364 % \begin{macrocode}
1365 \PT@resetcolumn % we are at the beginning of a line
1366 % \end{macrocode}
1367 % Now we start the tabular environment with the
1368 % computed preamble. We redefine the |\\| to whatever
1369 % the environment dictates.
1370 % \begin{macrocode}
1371 \PT@begin%
1372 % \end{macrocode}
1373 % Run, and this time, typeset, the contents.
1374 % \begin{macrocode}
1375 \the\PT@toks
1376 \PT@fill% new in 0.7.3: balance the last line
1377 % \end{macrocode}
1378 % End the array, close the group, close the environment.
1379 % We are done!
1380 % \begin{macrocode}
1381 \PT@finalize% finalize the queue (possibly close file)
1382 \PT@end
1383 \endgroup
1384 \PT@typeout@{Finished.}%
1385 \expandafter\end\expandafter{\PT@environment}}%
1386 % \end{macrocode}
1387 % \end{macro}
1388 %
1389 % \subsection{New interface}
1390 %
1391 % From v0.8 on, we offer a more convenient user interface
1392 % for the programming of the columns.
1393 %
1394 % \begin{macro}{\PT@from}
1395 % \begin{macro}{\PT@fromopt}
1396 % \begin{macro}{\PT@toopt}
1397 % The macro |\PT@from| is bound to |\=| in a polytable environment,
1398 % and used to move to the named column specified by its argument. The
1399 % previous column is closed. The variant |\PT@fromopt| is bound to
1400 % |\>| and takes an optional argument instead of a mandatory, which defaults
1401 % to the current column name followed by a single dot |.|. We use an
1402 % empty default which is then redefined to make it easier for the
1403 % user to use the default definition (TODO: explain better).
1404 % Otherwise, it is like |\PT@from|.
1405 %
1406 % The macro |\PT@toopt| is bound to |\<|. Where |\PT@from| ends an
1407 % entry if one is active, and starts a new one, the |\PT@toopt|
1408 % variant only ends the currently active entry.
1409 % \begin{macrocode}
1410 \newcommand{\PT@from}[1]%
1411 {\PT@checkendentry{#1}\PT@dofrom{#1}}
1412
1413 \newcommand{\PT@fromopt}[1][]%
1414 {\def\PT@temp{#1}%
1415 \ifx\PT@temp\empty
1416 % set default column name
1417 \def\PT@temp{\PT@currentcolumn .}%
1418 \fi
1419 \PT@expanded\PT@from\PT@temp}
1420
1421 \newcommand{\PT@toopt}[1][]%
1422 {\def\PT@temp{#1}%
1423 \ifx\PT@temp\empty
1424 % set default column name
1425 \def\PT@temp{\PT@currentcolumn .}%
1426 \fi
1427 \PT@expanded\PT@checkendentry\PT@temp
1428 \let\PT@scanning\undefined}
1429 % \end{macrocode}
1430 % \end{macro}
1431 % \end{macro}
1432 % \end{macro}
1433 %
1434 % \begin{macro}{\PT@dofrom}
1435 % \begin{macrocode}
1436 \newcommand*{\PT@dofrom}[1]%
1437 {\edef\PT@currentcolumn{#1}%
1438 \let\PT@scanning\PT@currentcolumn
1439 \let\PT@currentpreamble\relax% necessary for preparescan
1440 \@ifnextchar[%]
1441 {\PT@expanded\PT@dospecfrom\PT@currentcolumn}%
1442 {\PT@expanded\PT@dodofrom \PT@currentcolumn}}
1443
1444 \newcommand*{\PT@dospecfrom}{}% LaTeX check
1445 \def\PT@dospecfrom #1[#2]%
1446 {\PT@checkglobalfrom #2\PT@nil{#1}%
1447 \PT@dodofrom{#1}}
1448
1449 \newcommand*{\PT@checkglobalfrom}{}% LaTeX check
1450 \def\PT@checkglobalfrom
1451 {\@ifnextchar!\PT@getglobalfrom\PT@ignorefrom}
1452
1453 \newcommand*{\PT@getglobalfrom}{}% LaTeX check
1454 \def\PT@getglobalfrom!#1\PT@nil#2%
1455 {\column{#2}{#1}}
1456
1457 \newcommand*{\PT@ignorefrom}{}% LaTeX check
1458 \def\PT@ignorefrom #1\PT@nil#2%
1459 {\def\PT@currentpreamble{#1}}
1460
1461 \newcommand*{\PT@dodofrom}[1]%
1462 {\@ifundefined{PT@columnreference}%
1463 {% trial run
1464 \ifx\column\PT@otherruncolumn
1465 \else
1466 % first run
1467 \let\PT@storeendcolumn\PT@add
1468 \fi
1469 \def\PT@temp{@end@}}%
1470 {% final run
1471 \PT@split\PT@columnreference\PT@temp
1472 %\PT@typeout@{splitted: \PT@temp}
1473 }%
1474 \PT@expanded{\PT@expanded\PT@preparescan\PT@currentcolumn}\PT@temp
1475 \PT@scanbegin}
1476
1477 \let\PT@storeendcolumn\@gobbletwo
1478 % \end{macrocode}
1479 % Here, |\PT@scanbegin| will scan free or using the width, depending
1480 % on the run we are in.
1481 % \end{macro}
1482 %
1483 % \begin{macro}{\PT@fromto}
1484 % This is the implementation for the old explicit |\fromto|
1485 % macro. It takes the start and end columns, and the contents.
1486 % It can be used for all runs.
1487 % \begin{macrocode}
1488 \newcommand*{\PT@fromto}[3]%
1489 % \end{macrocode}
1490 % We allow a |\fromto| to follow a new-style command, but we reset
1491 % the current column to undefined, so no text may immediately
1492 % follow a |\fromto| command.
1493 % \begin{macrocode}
1494 {\PT@checkendentry{#1}%
1495 \let\PT@scanning\undefined
1496 % \end{macrocode}
1497 % We check a switch to prevent nested |\fromto|s.
1498 % \begin{macrocode}
1499 \PT@infromto
1500 \def\PT@infromto{%
1501 \PackageError{polytable}{Nested fromto}{}}%
1502 % \end{macrocode}
1503 % Here, the real work is done:
1504 % \begin{macrocode}
1505 \let\PT@currentpreamble\relax% necessary for preparescan
1506 \PT@preparescan{#1}{#2}%
1507 \PT@scanbegin #3\PT@scanend% defines \@curfield
1508 \PT@processentry{#1}{#2}%
1509 % \end{macrocode}
1510 % The commands |\PT@scanbegin| and |\PT@processentry| will perform
1511 % different tasks depending on which run we are in.
1512 %
1513 % We ignore spaces after the |\fromto| command.
1514 % \begin{macrocode}
1515 \let\PT@infromto\empty
1516 \ignorespaces}
1517 % \end{macrocode}
1518 % \end{macro}
1519 %
1520 % \begin{macro}{\PT@checkendentry}
1521 % This macro checks if we are currently scanning an entry. If so
1522 % (this is detected by checking if |\PT@scanning| is defined),
1523 % we close the entry and handle it (in |\PT@endentry|)
1524 % before returning. The argument
1525 % is the name of the column from which this macro is called.
1526 % \begin{macrocode}
1527 \newcommand*{\PT@checkendentry}% takes one argument
1528 {\@ifundefined{PT@scanning}%
1529 {\let\PT@temp\@gobble}%
1530 {\let\PT@temp\PT@endentry}%
1531 \PT@temp}
1532 %\newcommand*{\PT@checkendentry}% takes one argument
1533 % {\@ifundefined{PT@post@preamble}%
1534 % {\let\PT@temp\PT@discardentry}%
1535 % {\let\PT@temp\PT@endentry}%
1536 % \PT@temp}
1537
1538 %\newcommand*{\PT@discardentry}[1]%
1539 % {\let\PT@postpreamble=\empty\PT@scanend}
1540
1541 \newcommand*{\PT@endentry}[1]%
1542 {\PT@scanend
1543 \edef\PT@temp{#1}%
1544 \PT@expanded\PT@storeendcolumn\PT@temp\PT@columnqueue
1545 \let\PT@storeendcolumn\@gobbletwo
1546 \PT@expanded{\PT@expanded\PT@processentry\PT@currentcolumn}\PT@temp}
1547 % \end{macrocode}
1548 % \end{macro}
1549 %
1550 % \subsection{The trial runs}
1551 %
1552 % For each column, we store information in macros that are
1553 % based on the column name. We store a column's type (i.e., its
1554 % contribution to the table's preamble), its current width
1555 % (i.e., its the horizontal position where the column will start
1556 % on the page), and later, its number, which will be used for
1557 % the |\multicolumn| calculations.
1558 %
1559 % \begin{macro}{\PT@firstrun@column}
1560 % During the first trial run, we initialise all the columns. We
1561 % store their type, as declared in the |\column| command inside
1562 % the environment, and we set their initial width to 0pt.
1563 % Furthermore, we add the column to the list of all available
1564 % columns, increase the column counter, and tell \TeX\ to ignore
1565 % spaces that might follow the |\column| command.
1566 % New in v0.4.1: We make a case distinction on an empty type
1567 % field to prevent warnings for columns that have been defined
1568 % via |\PT@setmaxwidth| -- see there for additional comments.
1569 % New in v0.4.2: We allow redefinition of width if explicitly
1570 % specified, i.e., not equal to 0pt.
1571 % \begin{macrocode}
1572 \newcommand\PT@firstrun@column[3][0pt]%
1573 {\@ifundefined{PT@col@#2.type}%
1574 {\PT@typeout@{Defining column \PT@aligncol{#2} at #1.}%
1575 \@namedef{PT@col@#2.type}{#3}%
1576 \@namedef{PT@col@#2.width}{#1}% initialize the width of the column
1577 % add the new column to the (sortable) list of all columns
1578 \PT@consmacro\PT@allcols{PT@col@#2}%
1579 \advance\PT@cols by 1\relax}%
1580 {\expandafter\ifx\csname PT@col@#2.type\endcsname\empty
1581 \relax % will be defined in a later table of the same set
1582 \else
1583 \begingroup
1584 \def\PT@temp{PT@col@#2}%
1585 \ifx\PT@temp\PT@endcol
1586 \relax % end column is always redefined
1587 \else
1588 \PT@warning{Redefining column #2}%
1589 \fi
1590 \endgroup
1591 \fi
1592 \@namedef{PT@col@#2.type}{#3}%
1593 \expandafter\ifdim#1>0pt\relax
1594 \PT@typeout@{Redefining column #2 at #1.}%
1595 \@namedef{PT@col@#2.width}{#1}%
1596 \fi
1597 }%
1598 % \end{macrocode}
1599 % For the case that we are saving and there is not yet information
1600 % from the |.aux| file, we define the |.max| and |.trusted| fields
1601 % if they are undefined. If information becomes available later, it
1602 % will overwrite these definitions.
1603 % \begin{macrocode}
1604 \@ifundefined{PT@col@#2.max}%
1605 {\@namedef{PT@col@#2.max}{#1}%
1606 \expandafter\let\csname PT@col@#2.trusted\endcsname\PT@true}{}%
1607 \ignorespaces}
1608 % \end{macrocode}
1609 % \end{macro}
1610 % \begin{macro}{\PT@otherrun@column}
1611 % In all but the first trial run, we do not need any additional
1612 % information about the columns any more, so we just gobble the
1613 % two arguments, but still ignore spaces.
1614 % \begin{macrocode}
1615 \newcommand\PT@otherrun@column[3][]%
1616 {\ignorespaces}
1617 % \end{macrocode}
1618 % \end{macro}
1619 % \begin{macro}{\PT@checkcoldefined}
1620 % This macro verifies that a certain column is defined and
1621 % produces an error message if it is not. New in v0.8: this
1622 % macro implicitly defines the column if we have a default
1623 % column specifier.
1624 % \begin{macrocode}
1625 \def\PT@checkcoldefined #1%
1626 {\@ifundefined{PT@col@#1.type}%
1627 {\@ifundefined{PT@defaultcolumnspec}%
1628 {\PackageError{polytable}{Undefined column #1}{}}
1629 {\PT@debug@{Implicitly defining column #1}%
1630 \PT@expanded{\column{#1}}{\PT@defaultcolumnspec}}}{}%
1631 % \end{macrocode}
1632 % We also have to define columns with empty specifiers. This
1633 % situation can occur if save/restoring columns that are defined
1634 % by default specifiers.
1635 % \begin{macrocode}
1636 \expandafter\ifx\csname PT@col@#1.type\endcsname\empty\relax
1637 \@ifundefined{PT@defaultcolumnspec}{}%
1638 {\PT@debug@{Implicitly defining column #1}%
1639 \PT@expanded{\column{#1}}{\PT@defaultcolumnspec}}%
1640 \fi}
1641 % \end{macrocode}
1642 % \end{macro}
1643 % \begin{macro}{\PT@checkwidth}
1644 % Most of the work during the trial runs is done here. We increase
1645 % the widths of certain columns, if necessary. Note that there
1646 % are two conditions that have to hold if |\fromto{A}{B}| is encountered:
1647 % \begin{itemize}
1648 % \item the width of |A| has to be at least the width of the current
1649 % (i.e., previous) column.
1650 % \item the width of |B| has to be at least the width of |A|, plus the
1651 % width of the entry.
1652 % \end{itemize}
1653 % \begin{macrocode}
1654 \def\PT@checkwidth #1#2%
1655 {\PT@checkcoldefined{#2}% first column should have been checked before
1656 % \end{macrocode}
1657 % Here, we check the first condition.
1658 % \begin{macrocode}
1659 \def\PT@temp{PT@col@#1}%
1660 \ifx\PT@currentcol\PT@temp
1661 \PT@debug@{No need to skip columns.}%
1662 \else
1663 \PT@colwidth=\expandafter\@nameuse\expandafter
1664 {\[email protected]}\relax
1665 \ifdim\PT@colwidth>\csname PT@col@#1.width\endcsname\relax
1666 % we need to change the width
1667 \PT@debug@{s \PT@aligncol{#1}: %
1668 old=\expandafter\expandafter\expandafter
1669 \PT@aligndim\csname PT@col@#1.width\endcsname\@@%
1670 new=\expandafter\PT@aligndim\the\PT@colwidth\@@}%
1671 \PT@changedtrue
1672 \PT@enamedef{PT@col@#1.width}{\the\PT@colwidth}%
1673 \fi
1674 % \end{macrocode}
1675 % The same for the untrusted |.max| values.
1676 % \begin{macrocode}
1677 \PT@colwidth=\expandafter\@nameuse\expandafter
1678 {\[email protected]}\relax
1679 \ifdim\PT@colwidth>\csname PT@col@#1.max\endcsname\relax
1680 % we need to change the width
1681 \PT@debug@{S \PT@aligncol{#1}: %
1682 old=\expandafter\expandafter\expandafter
1683 \PT@aligndim\csname PT@col@#1.max\endcsname\@@%
1684 new=\expandafter\PT@aligndim\the\PT@colwidth\@@}%
1685 \PT@changedtrue
1686 \PT@checkrerun
1687 \PT@enamedef{PT@col@#1.max}{\the\PT@colwidth}%
1688 \fi
1689 \ifnum\csname PT@col@#1.trusted\endcsname=\PT@false\relax
1690 \ifdim\PT@colwidth=\csname PT@col@#1.max\endcsname\relax
1691 \PT@debug@{#1=\the\PT@colwidth\space is now trusted}%
1692 \expandafter\let\csname PT@col@#1.trusted\endcsname\PT@true%
1693 \fi
1694 \fi
1695 \fi
1696 % \end{macrocode}
1697 % We assume that the current field is typeset in |\@curfield|; we
1698 % can thus measure the width of the box and then test the second condition.
1699 % \begin{macrocode}
1700 \PT@expanded{\def\PT@temp}{\the\wd\@curfield}%
1701 \global\PT@colwidth=\@nameuse{PT@col@#1.width}%
1702 \global\advance\PT@colwidth by \PT@temp\relax%
1703 \ifdim\PT@colwidth>\csname PT@col@#2.width\endcsname\relax
1704 % we need to change the width
1705 \PT@debug@{#2 (width \PT@temp) starts after #1 (at \csname PT@col@#1.width\endcsname)}%
1706 \PT@debug@{c \PT@aligncol{#2}: %
1707 old=\expandafter\expandafter\expandafter
1708 \PT@aligndim\csname PT@col@#2.width\endcsname\@@%
1709 new=\expandafter\PT@aligndim\the\PT@colwidth\@@}%
1710 \PT@changedtrue
1711 \PT@enamedef{PT@col@#2.width}{\the\PT@colwidth}%
1712 \fi
1713 % \end{macrocode}
1714 % And again, we have to do the same for the untrusted maximums.
1715 % \begin{macrocode}
1716 \global\PT@colwidth=\@nameuse{PT@col@#1.max}%
1717 \global\advance\PT@colwidth by \PT@temp\relax%
1718 \ifdim\PT@colwidth>\csname PT@col@#2.max\endcsname\relax
1719 % we need to change the width
1720 \PT@debug@{C \PT@aligncol{#2}: %
1721 old=\expandafter\expandafter\expandafter
1722 \PT@aligndim\csname PT@col@#2.max\endcsname\@@%
1723 new=\expandafter\PT@aligndim\the\PT@colwidth\@@}%
1724 \PT@changedtrue
1725 \PT@checkrerun
1726 \PT@enamedef{PT@col@#2.max}{\the\PT@colwidth}%
1727 \fi
1728 \ifnum\csname PT@col@#2.trusted\endcsname=\PT@false\relax
1729 \ifdim\PT@colwidth=\csname PT@col@#2.max\endcsname\relax
1730 \PT@debug@{#2=\the\PT@colwidth\space is now trusted}%
1731 \expandafter\let\csname PT@col@#2.trusted\endcsname\PT@true%
1732 \fi
1733 \fi
1734 % \end{macrocode}
1735 % Finally, we update the current column to |#2|.
1736 % \begin{macrocode}
1737 \def\PT@currentcol{PT@col@#2}}
1738 % \end{macrocode}
1739 % \end{macro}
1740 % \begin{macro}{\PT@checkrerun}
1741 % If we have changed something with the trusted widths, we have
1742 % to check whether we are in a situation where we are using previously
1743 % defined columns. If so, we have to rerun \LaTeX.
1744 % \begin{macrocode}
1745 \def\PT@checkrerun
1746 {\ifnum\PT@inrestore=\PT@true\relax
1747 \PT@rerun
1748 \fi}
1749 % \end{macrocode}
1750 % \end{macro}
1751 % \begin{macro}{\PT@resetcolumn}
1752 % If the end of a line is encountered, we stop scanning the current
1753 % entry, and reset the current column.
1754 % \begin{macrocode}
1755 \newcommand*{\PT@resetcolumn}[1][]%
1756 {\PT@checkendentry{@end@}%
1757 \let\PT@currentcolumn\empty%
1758 \let\PT@scanning\undefined
1759 \let\PT@currentcol\PT@nullcol
1760 % TODO: remove these lines if they don't work
1761 %\let\PT@pre@preamble\empty
1762 %\PT@scanbeginfree
1763 }
1764 % \end{macrocode}
1765 % \end{macro}
1766 % \begin{macro}{\PT@nullcol}
1767 % The name of the |@begin@| column as a macro, to be able to compare
1768 % to it with |\ifx|; dito for the |@end@| column.
1769 % \begin{macrocode}
1770 \def\PT@nullcol{PT@col@@begin@}
1771 \def\PT@endcol{PT@col@@end@}
1772 % \end{macrocode}
1773 % \end{macro}
1774 %
1775 % \subsection{Sorting and numbering the columns}
1776 %
1777 % Not much needs to be done here, all the work is done by the
1778 % macros supplied by the \textsf{lazylist} package.
1779 % We just provide a few additional commands to facilitate
1780 % their use.
1781 %
1782 % \begin{macro}{\PT@Execute}
1783 % \begin{macro}{\PT@Sequence}
1784 % With |\PT@Execute|, a list of commands (with sideeffects) can
1785 % be executed in sequence. Usually, first a command will be
1786 % mapped over a list, and then the resulting list will be
1787 % executed.
1788 % \begin{macrocode}
1789 \def\PT@Execute{\Foldr\PT@Sequence\empty}
1790 \def\PT@Sequence #1#2{#1#2}
1791 % \end{macrocode}
1792 % \end{macro}
1793 % \end{macro}
1794 % \begin{macro}{\PT@ShowColumn}
1795 % This is a debugging macro, that is used to output the list
1796 % of columns in a pretty way. The columns internally get
1797 % prefixes to their names, to prevent name conflicts with
1798 % normal commands. In the debug output, we gobble this prefix
1799 % again.
1800 % \begin{macrocode}
1801 \def\PT@ShowColumn #1#2%
1802 {\PT@ShowColumn@{#1}#2\PT@ShowColumn@}
1803 \def\PT@ShowColumn@ #1PT@col@#2\PT@ShowColumn@
1804 {#1{#2} }
1805 \def\PT@ShowColumnWidth #1%
1806 {\PT@typeout@{%
1807 \PT@ShowColumn\PT@aligncol{#1}:
1808 \expandafter\expandafter\expandafter
1809 \PT@aligndim\csname #1.max\endcsname\@@}}
1810 \def\PT@StripColumn #1%
1811 {\expandafter\PT@StripColumn@#1\PT@StripColumn@}
1812 \def\PT@StripColumn@ PT@col@#1\PT@StripColumn@
1813 {#1}
1814 % \end{macrocode}
1815 % \end{macro}
1816 % \begin{macro}{\PT@Print}
1817 % Prints a list of columns, using |\PT@ShowColumn|.
1818 % \begin{macrocode}
1819 \def\PT@Print#1{\PT@Execute{\Map{\PT@ShowColumn\Identity}#1}}
1820 \def\PT@PrintWidth#1{\PT@Execute{\Map\PT@ShowColumnWidth#1}}
1821 % \end{macrocode}
1822 % \end{macro}
1823 %
1824 % \begin{macro}{\PT@TeXif}
1825 % This is an improved version of \textsf{lazylist}'s |\TeXif|.
1826 % It does have an additional |\relax| to terminate the condition.
1827 % The |\relax| is gobbled again to keep it fully expandable.
1828 % \begin{macrocode}
1829 \def\PT@TeXif #1%
1830 {\expandafter\@gobble#1\relax
1831 \PT@gobblefalse
1832 \else\relax
1833 \gobbletrue
1834 \fi}
1835 \def\PT@gobblefalse\else\relax\gobbletrue\fi #1#2%
1836 {\fi #1}
1837 % \end{macrocode}
1838 % \end{macro}
1839 %
1840 % \begin{macro}{\PT@ltmax}
1841 % The order by which the columns are sorted is given by
1842 % the order on their (untrusted) widths.
1843 % \begin{macrocode}
1844 \def\PT@ltmax #1#2%
1845 {\Not{\PT@TeXif{\ifdim\csname #1.max\endcsname>\csname #2.max\endcsname}}}
1846 % \end{macrocode}
1847 % \end{macro}
1848 %
1849 % \begin{macro}{\PT@numbercol}
1850 % This assigns the next consecutive number to a column.
1851 % We also reassign |PT@lastcol| to remember the final column.
1852 % \begin{macrocode}
1853 \def\PT@numbercol #1%
1854 {%\PT@typeout@{numbering #1 as \the\PT@cols}%
1855 \PT@enamedef{#1.num}{\the\PT@cols}%
1856 \def\PT@lastcol{#1}%
1857 \advance\PT@cols by 1\relax}
1858 % \end{macrocode}
1859 % \end{macro}
1860 %
1861 % \subsection{Typesetting the table}
1862 %
1863 % Remember that there are three important macros that
1864 % occur in the body of the polytable: |\column|, |\fromto|,
1865 % and |\\|. The |\column| macro is only really used
1866 % in the very first trial run, so there is nothing new
1867 % we have to do here, but the other two have to be
1868 % redefined.
1869 %
1870 % \begin{macro}{\PT@resetandcr}
1871 % This is what |\\| does in the typesetting phase.
1872 % It resets the current column, but it also calls the surrounding
1873 % environment's newline macro |\PT@cr| \dots
1874 % If we are \emph{not} in the last column, we insert
1875 % an implicit |fromto|. This is needed for the boxed environment to
1876 % make each column equally wide. Otherwise, if the boxed environment
1877 % is typeset in a centered way, things will go wrong.
1878 % \begin{macrocode}
1879 \newcommand{\PT@resetandcr}%
1880 {\PT@expanded\PT@checkendentry\PT@lastcol@%
1881 \ifx\PT@currentcol\PT@lastcol
1882 \else
1883 \ifx\PT@currentcol\PT@nullcol
1884 \edef\PT@currentcol{\Head{\Tail\PT@sortedlist}}%
1885 \fi
1886 \edef\PT@currentcol@{\PT@StripColumn\PT@currentcol}%
1887 \PT@typeout@{adding implicit fromto at eol from \PT@currentcol@
1888 \space to \PT@lastcol@}%
1889 \PT@expanded{\PT@expanded\fromto\PT@currentcol@}\PT@lastcol@
1890 \fi
1891 \PT@typeout@{Next line ...}%
1892 \let\PT@scanning\undefined% needed for resetcolumn
1893 \PT@resetcolumn\PT@cr}
1894 % \end{macrocode}
1895 % \end{macro}
1896 %
1897 % \begin{macro}{\PT@fill}
1898 % This variant of |\PT@resetandcr| is used at the end of
1899 % the environment, to insert a blank box for the |pboxed|
1900 % environment to balance the widths of all lines. It does
1901 % not start a new line, and does nothing if the current
1902 % column is |@begin@|. TODO: extract commonalities with
1903 % |\PT@resetandcr| into a separate macro.
1904 % \begin{macrocode}
1905 \newcommand{\PT@fill}%
1906 {\PT@expanded\PT@checkendentry\PT@lastcol@%
1907 \ifx\PT@currentcol\PT@lastcol
1908 \else
1909 \ifx\PT@currentcol\PT@nullcol
1910 \else
1911 \edef\PT@currentcol@{\PT@StripColumn\PT@currentcol}%
1912 \PT@typeout@{adding implicit fromto from \PT@currentcol@
1913 \space to \PT@lastcol@}%
1914 \PT@expanded{\PT@expanded\fromto\PT@currentcol@}\PT@lastcol@
1915 \fi\fi}
1916 % \end{macrocode}
1917 % \end{macro}
1918 %
1919 % \begin{macro}{\PT@placeinbox}
1920 % This macro is the final-run replacement for |\PT@checkwidth|. We use
1921 % the precomputed width information to typeset the contents of the
1922 % table in aligned boxes. The arguments are the same as for
1923 % |\PT@checkwidth|, i.e., the start and the end columns, and the assumption
1924 % that the entry is contained in the box |\@curfield|.
1925 % \begin{macrocode}
1926 \def\PT@placeinbox#1#2%
1927 % \end{macrocode}
1928 % We start by computing the amount of whitespace that
1929 % must be inserted before the entry begins. We then insert that amount
1930 % of space.
1931 % \begin{macrocode}
1932 {\PT@colwidth=\@nameuse{PT@col@#1.max}%
1933 \advance\PT@colwidth by -\expandafter\csname\[email protected]\endcsname
1934 \leavevmode
1935 \edef\PT@temp{\PT@StripColumn\PT@currentcol}%
1936 \PT@typeout@{adding space of width %
1937 \expandafter\PT@aligndim\the\PT@colwidth\@@
1938 (\expandafter\PT@aligncol\expandafter{\PT@temp} %
1939 -> \PT@aligncol{#1})}%
1940 \hb@xt@\PT@colwidth{%
1941 {\@mkpream{@{}l@{}}\@addtopreamble\@empty}%
1942 \let\CT@row@color\relax% colortbl compatibility
1943 \let\@sharp\empty%
1944 %\show\@preamble
1945 \@preamble}%
1946 % \end{macrocode}
1947 % The important part is to use the pre-typeset box |\@curfield|.
1948 % This produces real output!
1949 % \begin{macrocode}
1950 \PT@typeout@{adding box \space\space of width %
1951 \expandafter\PT@aligndim\the\wd\@curfield\@@
1952 (\PT@aligncol{#1} -> \PT@aligncol{#2})}%
1953 \box\@curfield
1954 % \end{macrocode}
1955 % Finally, we have to reset the current column and
1956 % ignore spaces.
1957 % \begin{macrocode}
1958 \def\PT@currentcol{PT@col@#2}%
1959 \ignorespaces}%
1960 % \end{macrocode}
1961 % \end{macro}
1962 %
1963 % \begin{macro}{\PT@preparescan}
1964 % The macro |\PT@preparescan| sets the two macros |\PT@scanbegin|
1965 % and |\PT@scanend| in such a way that they scan the input between
1966 % those two macros and place it in a box. The width of the box is
1967 % determined from the given column names. The name |@end@| can be
1968 % used as a column name is a free scan (a scan without knowing the
1969 % real end column) is desired. To allow redefinition of the preamble,
1970 % we assume that |\PT@currentpreamble| is defined to |\relax| if
1971 % we want it set normally dugin |\PT@preparescan|.
1972 % \begin{macrocode}
1973 \def\PT@preparescan#1#2%
1974 % First, we check that both columns are defined. This will
1975 % actually define the columns if implicit column definitions are
1976 % enabled.
1977 % \begin{macrocode}
1978 {\PT@checkcoldefined{#1}%
1979 \PT@checkcoldefined{#2}%
1980 \PT@colwidth=\@nameuse{PT@col@#2.max}%
1981 \advance\PT@colwidth by -\@nameuse{PT@col@#1.max}\relax%
1982 \ifmmode
1983 \PT@debug@{*math mode*}%
1984 \let\d@llarbegin=$%$
1985 \let\d@llarend=$%$
1986 \let\col@sep=\arraycolsep
1987 \else
1988 \PT@debug@{*text mode*}%
1989 \let\d@llarbegin=\begingroup
1990 \let\d@llarend=\endgroup
1991 \let\col@sep=\tabcolsep
1992 \fi
1993 \ifx\PT@currentpreamble\relax
1994 \PT@expanded{\PT@expanded{\def\PT@currentpreamble}}%
1995 {\csname PT@col@#1.type\endcsname}%
1996 \fi
1997 % \end{macrocode}
1998 % Now, we make a preamble using the macro |\@mkpream| from the
1999 % \textsf{array} package. This macro takes a format string as
2000 % argument, and defines |\@preamble| as a result, where |\@sharp|
2001 % occurs in the positions of the column contents.
2002 % We perform the operation in a group to prevent certain redefinitions
2003 % from escaping. The |\@preamble| is set globally anyway.
2004 % \begin{macrocode}
2005 {\PT@expanded\@mkpream\PT@currentpreamble%
2006 \@addtopreamble\@empty}%
2007 \let\CT@row@color\relax% colortbl compatibility
2008 % \end{macrocode}
2009 % We split the preamble at the position of the |\@sharp|, using
2010 % some tricks to make sure that there really is precisely one
2011 % occurrence of |\@sharp| in the resulting preamble code, and
2012 % producing an error otherwise. The splitting defines
2013 % |\PT@pre@preamble| and |\PT@post@preamble|. With those and
2014 % the computed |\PT@colwidth|, the scan is successfully prepared.
2015 % \begin{macrocode}
2016 \expandafter\PT@splitpreamble\@preamble\@sharp\PT@nil}
2017 % \end{macrocode}
2018 %
2019 % We now define the splitting of the preamble.
2020 % \begin{macrocode}
2021 \def\PT@splitpreamble #1\@sharp #2\PT@nil{%
2022 \let\@sharp=\relax% needed for the following assignment
2023 \def\PT@terp{#2}%
2024 \ifx\PT@terp\empty%
2025 \PackageError{polytable}{Illegal preamble (no columns)}{}%
2026 \fi
2027 \PT@splitsplitpreamble{#1}#2\PT@nil}
2028
2029 \def\PT@splitsplitpreamble #1#2\@sharp #3\PT@nil{%
2030 \def\PT@temp{#3}%
2031 \ifx\PT@temp\empty%
2032 \else
2033 \PackageError{polytable}{Illegal preamble (multiple columns)}{}%
2034 \fi
2035 \def\PT@pre@preamble{#1}%
2036 \def\PT@post@preamble{#2}}%
2037 % \end{macrocode}
2038 %
2039 % Finally, we can define the scan environment, which depends on
2040 % all the other macros being defined correctly. The macro
2041 % |\PT@scanbegin| is not defined directly, but will be set
2042 % to |\PT@scanbeginfree| during the trial runs and to
2043 % |\PT@scanbeginwidth| during the final run.
2044 % \begin{macrocode}
2045 \def\PT@scanbeginwidth
2046 {\PT@scanbegin@{\hbox to \PT@colwidth}}
2047
2048 \def\PT@scanbeginfree
2049 {\PT@scanbegin@{\hbox}}
2050
2051 \def\PT@scanbegin@#1%
2052 {\setbox\@curfield #1%
2053 \bgroup
2054 \PT@pre@preamble\strut\ignorespaces}
2055
2056 \def\PT@scanend
2057 {\PT@post@preamble
2058 \egroup}
2059 % \end{macrocode}
2060 % \end{macro}
2061 %
2062 % \subsection{Saving and restoring column widths}
2063 %
2064 % Column width information can be saved under a name and thus
2065 % be reused in other tables. The idea is that the command
2066 % |\savecolumns| can be issued inside a polytable to save the
2067 % current column information, and |\restorecolumns| can be used
2068 % to make that information accessible in a later table. All tables
2069 % using the same information should have the same column widths,
2070 % which means that some information might need to be passed back.
2071 % Therefore, we need to write to an auxiliary file.
2072 %
2073 % Both |\savecolumns| and |\restorecolumns| are mapped to the internal
2074 % commands |\PT@savewidths| and |\PT@restorewidths|. Both take an
2075 % optional argument specifying a name for the column width
2076 % information. Thereby, multiple sets of such information can be
2077 % used simultaneously.
2078 %
2079 % One important thing to consider is that the widths read from the
2080 % auxiliary file must not be trusted. The user may have edited the
2081 % source file before the rerun, and therefore, the values read might
2082 % actually be too large (or too small, but this is less dangerous).
2083 %
2084 % The way we solve this problem is to distinguish two width values
2085 % per column: the trusted width, only using information from the
2086 % current run, and the untrusted width, incorportating information
2087 % from the |.aux| file. An untrusted width can become (conditionally)
2088 % trusted if it is reached in the computation with respect to an
2089 % earlier column. (Conditionally, because its trustworthiness still
2090 % depends on the earlier columns being trustworthy.) In the end, we
2091 % can check whether all untrusted widths are conditionally trusted.
2092 %
2093 % We write the final, the maximum widths, into the auxiliary file.
2094 % We perform the
2095 % write operation when we are sure that a specific set is
2096 % no longer used. This is the case when we save a new set under the
2097 % same name, or at the end of the document. The command
2098 % |\PT@verifywidths| takes care of this procedure.
2099 % This command will also check if a rerun is necessary, and issue
2100 % an appropriate warning if that should be the case.
2101 %
2102 % \begin{macro}{\PT@setmaxwidth}
2103 % First, we need a macro to help us interpreting the contents of
2104 % the |.aux| file. New v0.4.1: We need to define the restored
2105 % columns with the |\column| command, because otherwise we will
2106 % have problems in the case that later occurences of tables in
2107 % the document that belong to the same set, but define additional
2108 % columns. (Rerun warnings appear ad infinitum.)
2109 % In v0.4.2: columns with width 0.0 are now always trusted.
2110 % \begin{macrocode}
2111 \newcommand*{\PT@setmaxwidth}[3][\PT@false]% #2 column name, #3 maximum width
2112 {\@namedef{PT@col@#2.max}{#3}%
2113 \ifdim#3=0pt\relax
2114 \expandafter\let\csname PT@col@#2.trusted\endcsname=\PT@true%
2115 \else
2116 \expandafter\let\csname PT@col@#2.trusted\endcsname=#1%
2117 \fi
2118 \column{#2}{}}%
2119 % \end{macrocode}
2120 % \end{macro}
2121 %
2122 % \begin{macro}{\PT@loadtable}
2123 % Now, we can load table information that has been read from the
2124 % |.aux| file. Note that a |\csname| construct expands to |\relax|
2125 % if undefined.
2126 % \begin{macrocode}
2127 \def\PT@loadtable#1% #1 table id number
2128 {%\expandafter\show\csname PT@restore@\romannumeral #1\endcsname
2129 %\show\column
2130 \PT@typeout@
2131 {Calling \expandafter\string
2132 \csname PT@restore@\romannumeral #1\endcsname.}%
2133 \let\maxcolumn\PT@setmaxwidth
2134 %\expandafter\show\csname PT@load@\romannumeral #1\endcsname
2135 \csname PT@restore@\romannumeral #1\endcsname}
2136 % \end{macrocode}
2137 % \end{macro}
2138 %
2139 % \begin{macro}{\PT@loadtablebyname}
2140 % Often, we want to access table information by a column width set
2141 % name.
2142 % \begin{macrocode}
2143 \def\PT@loadtablebyname#1% #1 set name
2144 {\PT@typeout@{Loading table information for column width set #1.}%
2145 \PT@loadtable{\csname PT@widths@#1\endcsname}}%
2146 % \end{macrocode}
2147 % \end{macro}
2148 %
2149 % \begin{macro}{\PT@saveinformation}
2150 % In each table for which the widths get reused (i.e., in all tables
2151 % that use either |\savecolumns| or |\restorecolumns|, we have to store
2152 % all important information for further use.
2153 % \begin{macrocode}
2154 \def\PT@saveinformation#1% #1 set name
2155 {\PT@expanded{\def\PT@temp}{\csname PT@widths@#1\endcsname}%
2156 \PT@expanded{\def\PT@temp}%
2157 {\csname PT@restore@\romannumeral\PT@temp\endcsname}%
2158 \expandafter\gdef\PT@temp{}% start empty
2159 % this is: \PT@Execute{\Map{\PT@savecolumn{\PT@temp}}{\Reverse\PT@allcols}}
2160 \expandafter\PT@Execute\expandafter{\expandafter
2161 \Map\expandafter{\expandafter\PT@savecolumn
2162 \expandafter{\PT@temp}}{\Reverse\PT@allcols}}}
2163 % \end{macrocode}
2164 % \end{macro}
2165 %
2166 % \begin{macro}{\PT@savecolumn}
2167 % A single column is saved by this macro.
2168 % \begin{macrocode}
2169 \def\PT@savecolumn#1#2% #1 macro name, #2 column name
2170 {\PT@typeout@{saving column #2 in \string #1 ...}%
2171 \def\PT@temp{#2}%
2172 \ifx\PT@temp\PT@nullcol
2173 \PT@typeout@{skipping nullcol ...}%
2174 % This was a bug: end column cannot be skipped, because
2175 % it can change.
2176 % \else\ifx\PT@temp\PT@endcol
2177 % \PT@typeout@{skipping endcol ...}%
2178 \else
2179 \PT@typeout@{max=\csname #2.max\endcsname, %
2180 width=\csname #2.width\endcsname, %
2181 trusted=\csname #2.trusted\endcsname}%
2182 % we need the column command in here
2183 % we could do the same in \column, but then the location of
2184 % \save / \restore matters ...
2185 \PT@gaddendmacro{#1}{\maxcolumn}%
2186 \ifnum\csname #2.trusted\endcsname=\PT@true\relax
2187 \PT@gaddendmacro{#1}{[\PT@true]}%
2188 \fi
2189 \edef\PT@temp{\PT@StripColumn{#2}}%
2190 \PT@addargtomacro{#1}{PT@temp}%
2191 \PT@addargtomacro{#1}{#2.max}%
2192 \PT@gaddendmacro{#1}{\column}%
2193 \PT@addoptargtomacro{#1}{#2.width}%
2194 \edef\PT@temp{\PT@StripColumn{#2}}%
2195 \PT@addargtomacro{#1}{PT@temp}%
2196 \PT@addargtomacro{#1}{#2.type}%
2197 %\show#1%
2198 % \fi
2199 \fi
2200 }
2201 % \end{macrocode}
2202 % \end{macro}
2203 %
2204 % \begin{macro}{\PT@savewidths}
2205 % If we really want to save column width information, then the first
2206 % thing we should worry about is that there might already have been
2207 % a set with the name in question. Therefore, we will call
2208 % |\PT@verifywidths| for that set. In the case that there is no set
2209 % of this name yet, we will schedule the set for verification at the
2210 % end of document.
2211 % \begin{macrocode}
2212 \newcommand*{\PT@savewidths}[1][default@]
2213 {\PT@typeout@{Executing \string\savecolumns [#1].}%
2214 \def\PT@currentwidths{#1}%
2215 \PT@verifywidths{#1}%
2216 % \end{macrocode}
2217 % We now reserve a new unique number for this column width set
2218 % by increasing the |\PT@table| counter. We then associate
2219 % the given name (or |default@|) with the counter value and
2220 % restore the widths from the |.aux| file if they are present.
2221 % \begin{macrocode}
2222 \global\advance\PT@table by 1\relax
2223 \expandafter\xdef\csname PT@widths@#1\endcsname
2224 {\the\PT@table}%
2225 \PT@loadtable{\PT@table}%
2226 \ignorespaces}
2227 % \end{macrocode}
2228 % \end{macro}
2229 %
2230 % \begin{macro}{\PT@restorewidths}
2231 % Restoring information is quite simple. We just load all information
2232 % available.
2233 % \begin{macrocode}
2234 \newcommand*{\PT@restorewidths}[1][default@]
2235 {\PT@typeout@{Executing \string\restorecolumns [#1].}%
2236 \def\PT@currentwidths{#1}%
2237 \let\PT@inrestore\PT@true
2238 \PT@loadtablebyname{#1}%
2239 \ignorespaces}
2240 % \end{macrocode}
2241 % \end{macro}
2242 %
2243 % \begin{macro}{\PT@comparewidths}
2244 % \begin{macrocode}
2245 \def\PT@comparewidths#1% #1 full column name
2246 {\@ifundefined{#1.max}%
2247 {\PT@typeout@{computed width for #1 is fine ...}}%
2248 {\ifdim\csname #1.max\endcsname>\csname #1.width\endcsname\relax
2249 \PT@typeout@{Preferring saved width for \PT@StripColumn{#1}.}%
2250 \PT@changedtrue
2251 \PT@colwidth=\@nameuse{#1.max}\relax
2252 \PT@enamedef{#1.width}{\the\PT@colwidth}%
2253 \fi}}
2254 % \end{macrocode}
2255 % \end{macro}
2256 %
2257 % \begin{macro}{\PT@trustedmax}
2258 % \begin{macrocode}
2259 \def\PT@trustedmax#1%
2260 {\PT@TeXif{\ifnum\csname #1.trusted\endcsname=\PT@true}}
2261 % \end{macrocode}
2262 % \end{macro}
2263 % \begin{macro}{\PT@equalwidths}
2264 % \begin{macrocode}
2265 \def\PT@equalwidths#1% #1 full column name
2266 {\@ifundefined{#1.max}{}%
2267 {\ifdim\csname #1.max\endcsname=\csname #1.width\endcsname\relax
2268 \PT@typeout@{col #1 is okay ...}%
2269 \else
2270 \PT@rerun% a rerun is needed
2271 \fi}}
2272 % \end{macrocode}
2273 % \end{macro}
2274 %
2275 % \begin{macro}{\PT@verifywidths}
2276 % \begin{macrocode}
2277 \def\PT@verifywidths#1% #1 column width set name
2278 {\@ifundefined{PT@widths@#1}%
2279 {\PT@typeout@{Nothing to verify yet for set #1.}%
2280 \PT@typeout@{Scheduling set #1 for verification at end of document.}%
2281 \AtEndDocument{\PT@verifywidths{#1}}}%
2282 {\PT@typeout@{Verifying column width set #1.}%
2283 \PT@expanded\PT@verify@widths{\csname PT@widths@#1\endcsname}{#1}}}
2284
2285 \def\PT@verify@widths#1#2% #1 set id number, #2 set name
2286 {\@ifundefined{PT@restore@\romannumeral #1}{}%
2287 {\begingroup
2288 \let\column\PT@firstrun@column
2289 \PT@cols=0\relax%
2290 \def\PT@allcols{\Nil}%
2291 \PT@loadtablebyname{#2}%
2292 \PT@table=#1\relax
2293 % nullcolumn is not loaded, therefore:
2294 \@namedef{\PT@nullcol .width}{0pt}%
2295 % checking trust
2296 \PT@prelazylist
2297 \All{\PT@trustedmax}{\PT@allcols}%
2298 {\PT@typeout@{All maximum widths can be trusted -- writing .max!}%
2299 \PT@save@table{.max}}%
2300 {\PT@typeout@{Untrustworthy maximums widths -- writing .width!}%
2301 \PT@rerun
2302 \PT@save@table{.width}}%
2303 \PT@postlazylist
2304 \endgroup}%
2305 \PT@typeout@{Verification for #2 successful.}}
2306 % \end{macrocode}
2307 % \end{macro}
2308 %
2309 % \begin{macro}{\PT@save@table}
2310 % Here we prepare to write maximum column widths to the
2311 % |.aux| file.
2312 % \begin{macrocode}
2313 \def\PT@save@table#1%
2314 {\PT@typeout@{Saving column width information.}%
2315 \if@filesw
2316 \PT@prelazylist
2317 {\immediate\write\@auxout{%
2318 \gdef\expandafter\noexpand
2319 \csname PT@restore@\romannumeral\PT@table\endcsname
2320 {\PT@Execute{\Map{\PT@write@column{#1}}{\Reverse\PT@allcols}}}}}%
2321 \PT@postlazylist
2322 \fi}
2323 % \end{macrocode}
2324 % \end{macro}
2325 %
2326 % \begin{macro}{\PT@write@column}
2327 % We define the column command to write to the file.
2328 % \begin{macrocode}
2329 \def\PT@write@column #1#2%
2330 {\noexpand\maxcolumn^^J%
2331 {\PT@StripColumn{#2}}%
2332 {\@nameuse{#2#1}}}%
2333 % \end{macrocode}
2334 % \end{macro}
2335 %
2336 % \subsection{The user environments}
2337 %
2338 % It remains to define the environments to be
2339 % called by the user.
2340 % New in v0.8: we add the environments
2341 % |ptboxed| and |pmboxed| for text-mode and math-mode
2342 % boxed environments. In turn, we remove |ptabular| and
2343 % |parray|, and make the point to their new counterparts.
2344 % \begin{macrocode}
2345 \def\pboxed{%
2346 \let\PT@begin \empty
2347 \let\PT@end \empty
2348 % \end{macrocode}
2349 % The following assignment is a hack. If |pboxed| is called
2350 % from within another |tabular|- or |array|-environment, then
2351 % this sometimes does the right thing.
2352 % \begin{macrocode}
2353 \ifx\\\PT@arraycr
2354 \let\PT@cr \PT@normalcr
2355 \else
2356 \let\PT@cr \\%
2357 \fi
2358 \expandafter\beginpolytable\ignorespaces}
2359
2360 \let\endpboxed\endpolytable
2361
2362 \def\ptboxed{%
2363 \def\PT@begin {\tabular{@{}l@{}}}%
2364 \let\PT@end \endtabular
2365 \let\PT@cr \@arraycr
2366 \expandafter\beginpolytable\ignorespaces}
2367
2368 \let\endptboxed\endpolytable
2369
2370 \def\pmboxed{%
2371 \def\PT@begin {\array{@{}l@{}}}%
2372 \let\PT@end \endarray
2373 \let\PT@cr \@arraycr
2374 \expandafter\beginpolytable\ignorespaces}
2375
2376 \let\endpmboxed\endpolytable
2377
2378 \let\ptabular \ptboxed
2379 \let\endptabular \endptboxed
2380 \let\parray \pmboxed
2381 \let\endparray \endpmboxed
2382
2383 % \end{macrocode}
2384 %
2385 % That is all.
2386 % \begin{macrocode}
2387 %</package>
2388 % \end{macrocode}
2389 %
2390 % \Finale
2391 \endinput