summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tabularew/tabularew.dtx
blob: ddd249aa209c6afcd8ae633942d6a18bb0505548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
% \iffalse
%%
%% File `tabularew.dtx'.
%% Copyright (C) 2009 Diego Saba
%% This file may be distributed under the terms of the LPPL.
%%
%
%<*dtx>
          \ProvidesFile{tabularew.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{tabularew}
% \fi
%         \ProvidesFile{tabularew.dtx}
          [2009/05/23 v0.1 tabularew package (DPC)]
%
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{tabularew}
\EnableCrossrefs
\CodelineIndex
\GetFileInfo{tabularew.dtx}
\title{The \textsf{tabularew} package\thanks{This file
         has version number \fileversion, last
         revised \filedate.}}
\author{Diego Saba}
\date{\filedate}
\RecordChanges
\begin{document}
\maketitle
\DocInput{tabularew.dtx}
\PrintChanges
\end{document}
%</driver>
% \fi
% \CheckSum{458}
%
% \MakeShortVerb{\"}
% \def\<#1>{$\langle$#1\/$\rangle$}%
%
% \def\dataa{\ignorespaces
%   1&7,4&2,9&0,83&0,83&37,0&54,7&30,7&1,2\\
%   2&59,9&8,9&0,83&0,83&4,61&17,9&4,46&1,2\\
%   3&64,0&7,5&0,83&0,83&4,31&21,1&4,22&1,2\\
%   4&46,3&4,8&0,83&0,83&5,95&33,5&5,86&1,2\\
%   5&48,4&6,8&0,83&0,83&5,70&23,3&5,53&1,2
%   }
% \def\datab{\ignorespaces
%   1&7&,4&2&,9&0&,83&0&,83&37&,0&54&,7&30&,7&1&,2\\
%   2&59&,9&8&,9&0&,83&0&,83&4&,61&17&,9&4&,46&1&,2\\
%   3&64&,0&7&,5&0&,83&0&,83&4&,31&21&,1&4&,22&1&,2\\
%   4&46&,3&4&,8&0&,83&0&,83&5&,95&33&,5&5&,86&1&,2\\
%   5&48&,4&6&,8&0&,83&0&,83&5&,70&23&,3&5&,53&1&,2
%   }
%
% \begin{abstract}
%
% This article proposes an extended implementation of the \LaTeX\
% \textsf{tabular} environment. It adds a new command to gain access
% to a quantity, called here the \emph{excess width}, involved in
% the process of
% calculating the column widths. This allows to modify the default
% algorithm, that produces undesirable effects in some circumstances.
%
% The same extension can easily be added in the future to the similar
% \textsf{array} environment and to the star forms of both.
%
% This extension evaluates the whole table three times. It consumes
% more resources than the standard environment and cannot be made 100\%
% compatible, as it conceptually should.
%
% \end{abstract}
%
%
% \section{Introduction}
%
% This package extends the implementation of the \textsf{tabular}
% environment contained in the \textsf{array} package. More
% information can be found in \cite{bk:companion}.
%
% Let us start with an example. Suppose that you have to modify
% the following table, in order to align the decimal separators.
%
% \begin{center}
%   \begin{tabular}{*{9}{c}}
%     sez. &
%     $\sigma$ &
%     $\tau$ &
%     $\beta_{\mathrm{pl},\sigma}$ &
%     $\beta_{\mathrm{pl},\tau}$ &
%     $S_{\mathrm{pl},\sigma}$ &
%     $S_{\mathrm{pl},\tau}$ &
%     $S_{\mathrm{pl}}$ &
%     $S_{\mathrm{pl,am}}$ \\[-.2\normalbaselineskip]
%     &
%     \scriptsize [MPa] &
%     \scriptsize [MPa] \\[.2\normalbaselineskip]
%     \hline
%     \hbox{\vrule height .9\normalbaselineskip width0pt}\dataa
%   \end{tabular}
% \end{center}
%
% This can be done using a well known stratagem:
% each number can be split so that the integer part belongs to a column,
% and the separator sign and the fractional part belong to the following
% column. The labels must now span two columns.
%
% The code will change accordingly:
%
% \begin{center}
%   \begin{tabular}{m{.4\textwidth}@{}c@{}m{.5\textwidth}}
%     "\begin{tabular}{*{9}{c}}" \par
%     "  sez. &" \par
%     "  $\sigma$ &" \par
%     "  $\tau$ &" \par
%     "  "~\dots \par
%     "  \hline" \par
%     "  1 & 7,4 & 2,9 &"~\dots \par
%     "  "~\dots \par
%     "\end{tabular}"        
%     &
%     $\Rightarrow$
%     &
%     "\begin{tabular}{c*{8}{r@{}l}}" \par
%     "  sez. &" \par
%     "  \multicolumn{2}{c}{$\sigma$} &" \par
%     "  \multicolumn{2}{c}{$\tau$} &" \par
%     "  "~\dots \par
%     "  \hline" \par
%     "  1 & 7&,4 & 2&,9 &"~\dots \par
%     "  "~\dots \par
%     "\end{tabular}"
%   \end{tabular}
% \end{center}
%
% The result is probably unexpected:
%
% \begin{center}
%   \begin{tabular}{c*{8}{r@{}l}}
%     sez. &
%     \multicolumn{2}{c}{$\sigma$} &
%     \multicolumn{2}{c}{$\tau$} &
%     \multicolumn{2}{c}{$\beta_{\mathrm{pl},\sigma}$} &
%     \multicolumn{2}{c}{$\beta_{\mathrm{pl},\tau}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl},\sigma}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl},\tau}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl}}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl,am}}$} \\[-.2\normalbaselineskip]
%     &
%     \multicolumn{2}{c}{\scriptsize [MPa]} &
%     \multicolumn{2}{c}{\scriptsize [MPa]} \\[.2\normalbaselineskip]
%     \hline
%     \hbox{\vrule height .9\normalbaselineskip width0pt}\datab
%   \end{tabular}
% \end{center}
%
% Note that the last label is not centered on its data: it is aligned
% to the left and hangs to the right. The same is true of the third label.
% Actually, seven of the nine labels are wider than their data, though some
% of them by very little.
% When a |\multicolumn| is wider than the columns it spans, it is aligned
% to the left, disregarding the alignment directive.
%
% The intent was to center all the labels, like this:
% \newcolumntype{w}{>{\spew{.5}{+1}}}
% \begin{center}
%   \begin{tabularew}{c*{8}{wr@{}l}}
%     sez. &
%     \multicolumn{2}{c}{$\sigma$} &
%     \multicolumn{2}{c}{$\tau$} &
%     \multicolumn{2}{c}{$\beta_{\mathrm{pl},\sigma}$} &
%     \multicolumn{2}{c}{$\beta_{\mathrm{pl},\tau}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl},\sigma}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl},\tau}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl}}$} &
%     \multicolumn{2}{c}{$S_{\mathrm{pl,am}}$} \\[-.2\normalbaselineskip]
%     &
%     \multicolumn{2}{c}{\scriptsize [MPa]} &
%     \multicolumn{2}{c}{\scriptsize [MPa]} \\[.2\normalbaselineskip]
%     \hline
%     \hbox{\vrule height .9\normalbaselineskip width0pt}%
%     \datab
%   \end{tabularew}
% \end{center}
%
% To understand the reason of this behaviour, and the difficulty to
% ``correct'' it, the algorithm used by \LaTeX\ and by the underlying
% \TeX\ to compute the column widths must be examined in greater detail.
%
% The column widths are first calculated without
% considering
% the |\multicolumn| cells, unless they span just one column and so
% are ``fake'' |\multicolumn|s. Let us call the result of this first
% computation the \emph{intrinsic width} of a column. Then each
% |\multicolumn| in turn is taken into consideration. If it is
% wider than the columns it spans, comprehensive of the space
% between adjacent columns, the last column
% is widened to accommodate the |\multicolumn|.
% The order matters:
% first the |\multicolumn|s that end at the second column are
% considered, then those that end at the third column, and so on.
% Let us call the column widths so obtained the
% \emph{extrinsic widths}.
% The difference between the extrinsic width and the intrinsic width is
% the \emph{excess width}\footnote{Other names that I have considered are
% \emph{proper}, \emph{inherent}, \emph{implicit}, \emph{essential},
% \emph{innate} for \emph{intrinsic} and \emph{relational},
% \emph{explicit}, \emph{accidental}, \emph{acquired} for
% \emph{extrinsic}.}.
%
% The \textsf{tabular} environment relies on the \TeX\ primitive
% |\halign|, from which it inherits the algorithm. It is
% explained in a different and more formal way in
% The~\TeX book~\cite[page~235]{bk:texbook}, but the result is
% the same.
%
% This algorithm sometimes produces undesirable effects, as the
% example above shows and Donald Knuth
% himself points out~\cite[\textit{ibidem}]{bk:texbook}.
%
%
% \section{Features}
%
% The \textsf{tabularew} environment has the same syntax as the
% \textsf{tabular} environment, and should behave in the same
% way, except for the new features (and a new bunch of incompatibilities
% and bugs \dots).
% \begin{quote}
%   "\begin{tabularew}"\marg{pream}" " \ldots\ "\end{tabularew}"
% \end{quote}
%
% \DescribeMacro\GetExcessWidth
% In the body of the environment a new command is available.
% \begin{quote}
%   |\GetExcessWidth|\marg{column} \\
%   \<\textit{column}>\ $\longrightarrow$~%
%     \<optional sign>\ \<number>
% \end{quote}
% The \<\textit{column}> indication can be absolute or relative to
% the current position. It is relative if
% the first character is `\texttt{+}' or
% `\texttt{-}', otherwise it is absolute\footnote{I cannot see
% a useful application for the `\texttt{-}' form, but it was natural
% to include it.}.
% \DescribeMacro\ExcessWidth
% Its effect is to set the dimension register |\ExcessWidth| to
% the excess width of \<\textit{column}>.
%
% \DescribeMacro\spew
% The command\footnote{The name stands for \emph{add a \textsc{sp}ace
% proportional to the \textsc{e}xcess \textsc{w}idth}, but the effect
% seems a bit unfortunate.}
% \begin{quote}
%   "\spew"\marg{factor}\marg{column}
% \end{quote}
% is just a shorthand for the idiom
% \begin{quote}
%   "\GetExcessWidth"\marg{column}"\hspace{"\<\textit{factor}>"\ExcessWidth}"
% \end{quote}
%
% Since there is no way to access the widths directly during
% the evaluation of a table, a trick must be used.
% The table is evaluated three times. During the first pass
% every call to |\GetExcessWidth| sets |\ExcessWidth| to
% a null length and every call to
% |\multicolumn| generates an empty cell; thus the \emph{intrinsic widths}
% are collected.
% The second pass restores the normal behaviour of |\multicolumn|; the
% \emph{extrinsic widths} are collected and the \emph{excess widths}
% calculated.
% In the third and last pass every call to |\ExcessWidth| returns the
% requested measure.
%
% The column widths can change between the second and third pass,
% but the total width of the table will not,
% if |\spew| is used in the following way, that is the
% way I had in mind when this package was conceived.
%
% When there is a |\multicolumn| cell, the columns crossed by it
% form a group. It is sometimes
% useful to access the excess width of the \emph{last} column of the
% group from within the preceding columns \emph{of the same group}.
% Moreover, the sum of the spaces |\spew|ed in each of the preceding
% columns should not exceed the excess width of the last column; that is,
% the sum of all the \<\textit{factor}> arguments should not exceed one.
% In this way the net result is to redistribute the excess width in a
% flexible way among the group of columns.
%
% \DescribeMacro\CurrentColumn
% \DescribeMacro\NumberOfColumns
% The two counters |\CurrentColumn| and |\NumberOfColumns| need not
% normally be used explicitly, since |\GetExcessWidth| makes use
% of them behind the scenes. Nonetheless I decided to expose them too
% to the user. They should only be read and not assigned to.
%
%
% \section{Example}
%
% The ``correct'' table shown in the introduction was obtained by
% \begin{quote}
%   "\begin{tabularew}{c*{8}{>{\spew{.5}{+1}}r@{}l}}" \\
%   "  sez. &" \\
%   "  \multicolumn{2}{c}{$\sigma$} &" \\
%   "  \multicolumn{2}{c}{$\tau$} &" \\
%   "  "~\dots \\
%   "  \hline" \\
%   "  1 & 7&,4 & 2&,9 &"~\dots \\
%   "  "~\dots \\
%   "\end{tabularew}"
% \end{quote}
%
%
% \StopEventually{
% \begin{thebibliography}{1}
%    \bibitem{bk:companion} \textsc{M.~Goossens}, \textsc{F.~Mittelbach}
%       and \textsc{A.~Samarin}.
%       \newblock The \LaTeX{} Companion.
%       \newblock
%       Addison-Wesley, Reading, Massachusetts, 1994.
%    \bibitem{bk:texbook}  \textsc{D. E. Knuth}.
%       \newblock  The \TeX{}book (Computers \& Typesetting Volume A).
%       \newblock
%       Addison-Wesley, Reading, Massachusetts, 1986.
% \end{thebibliography}
% \PrintChanges
% }
%
%
% \clearpage
% \section{The Code}
%
% I have succeeded in writing this code, thanks to David Carlisle's
% \textsf{tabulary} environment and his well-commented code.
% I have boldly and shamelessly copied it, without even fully
% understanding it, and modified it to suite my needs.
%
% To simplify things, I have eliminated the |\verb| and \textsf{colortbl}
% support (at least for the moment).
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{array}
%    \end{macrocode}
%
%    \begin{macrocode}
\catcode`\Z=14
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareOption{debugshow}{\catcode`\Z=9\relax}
%    \end{macrocode}
%
%    \begin{macrocode}
\ProcessOptions
%    \end{macrocode}
%    
% \begin{macro}{\ExcessWidth}
% \begin{macro}{\CurrentColumn}
% \begin{macro}{\NumberOfColumns}
% Allocate the registers for the user interface.
%    \begin{macrocode}
\newdimen\ExcessWidth
\newcount\CurrentColumn
\newcount\NumberOfColumns
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\tabularew}
% \begin{macro}{\TEW@get@body}
% \begin{macro}{\TEW@find@end}
% \begin{macro}{\endtabularew}
% The \textsf{tabularew} environment uses the same mechanism of grabbing
% its body as \textsf{tabulary}, \textsf{tabularx}, and the AMS alignment
% environments.
% The use of "{\ifnum0=`}\fi" to begin a grouping and of "\ifnum0=`{\fi}"
% to end it is discussed in \textsf{tabularx}. See also
% The \TeX book \cite[page 385]{bk:texbook}.
% It is needed to allow the environment inside an alignment.
%    \begin{macrocode}
\def\tabularew{%
  \edef\TEW@{\@currenvir}%
  {\ifnum0=`}\fi
  \TEW@setup
  \toks@{}\TEW@get@body}
%    \end{macrocode}

%    \begin{macrocode}
\long\def\TEW@get@body#1\end
  {\toks@\expandafter{\the\toks@#1}\TEW@find@end}
%    \end{macrocode}

%    \begin{macrocode}
\def\TEW@find@end#1{%
  \def\@tempa{#1}%
  \ifx\@tempa\TEW@\def\@tempa{\end{#1}}\expandafter\@tempa
  \else\toks@\expandafter
    {\the\toks@\end{#1}}\expandafter\TEW@get@body\fi}
%    \end{macrocode}

%    \begin{macrocode}
\def\endtabularew{%
Z \message{^^J^^JEW Table - first pass^^J}%
  \TEW@firstpass
Z \message{\@spaces\@spaces\space - second pass^^J}%
  \TEW@secondpass
Z \message{\@spaces\@spaces\space - last pass^^J}%
  \TEW@lastpass
  \TEW@cleanup
  \ifnum0=`{\fi}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}

% \begin{macro}{\TEW@setup}
% Save locally all the things that \textsf{tabularew} will assign to
% globally. The values will be restored at the end by |\TEW@cleanup|.
%    \begin{macrocode}
\def\TEW@setup{%
  \edef\@restorecounters{%
    \global\NumberOfColumns\the\NumberOfColumns
    \global\CurrentColumn\the\NumberOfColumns\relax}%
%    \end{macrocode}
% The excess widths are stored as macro definitions because
% this way is easier, and probably more efficient, than dealing with
% an ``array'' of dimension registers.
%    \begin{macrocode}
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
    \advance\count@\@ne
    \expandafter\ifx\csname TEW@\the\count@\endcsname\relax
      \@tempswafalse
    \else
      \expandafter\let\csname TEW@S\the\count@
        \expandafter\endcsname\csname TEW@\the\count@\endcsname
    \fi}%
%    \end{macrocode}
% These will only change locally.
%    \begin{macrocode}
  \let\@arraycr\TEW@arraycr
  \let\multicolumn\TEW@multicolumn
  \ExcessWidth\z@
%    \end{macrocode}
% This will only exist locally. But I'm not sure of this choice.
%    \begin{macrocode}
  \let\spew\TEW@spew
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@cleanup}
%    \begin{macrocode}
\def\TEW@cleanup{%
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
    \advance\count@\@ne
    \expandafter\ifx\csname TEW@S\the\count@\endcsname\relax
      \@tempswafalse
    \else
      \global\expandafter\let\csname TEW@\the\count@
        \expandafter\endcsname\csname TEW@S\the\count@\endcsname
    \fi}%
  \@restorecounters
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@firstpass}
% Build a table that will never show up and
% is built in a special way with the purpose of taking measures.
% |\ExcessWidth| is null and |\multicolumn|s are empty.
% The \emph{intrinsic widths} are collected.
%    \begin{macrocode}
\def\TEW@firstpass{%
  \let\multicolumn\TEW@multicolumnempty
  \TEW@tabsample
  \let\multicolumn\TEW@multicolumn
  \let\@computation\TEW@firstcomp
  \TEW@measure
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@secondpass}
% Now |\multicolumn|s are honoured and
% the \emph{extrinsic widths} are collected.
%    \begin{macrocode}
\def\TEW@secondpass{%
  \TEW@tabsample
  \let\@computation\TEW@secondcomp
  \TEW@measure
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@lastpass}
% Just build the real table.
%    \begin{macrocode}
\def\TEW@lastpass{%
  \TEW@tabfinal
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@tabsample}
% Add a row at the end of the table that won't affect the
% column widths. This row will later be analyzed to collect
% the widths. The last row provided by the user can't serve
% this purpose because it could contain multicolumns or be
% hidden by an hline.
%    \begin{macrocode}
\def\TEW@tabsample{%
  \let\GetExcessWidth\@GetExcessWidthz@
  \let\@mkpream\TEW@mkpream
  \setbox\z@\hbox{%
  \gdef\@halignto{}%
  \col@sep\tabcolsep
  \let\d@llarbegin\begingroup\let\d@llarend\endgroup
    \expandafter\TEW@tabarray\the\toks@
      \crcr\omit
      {\count@\NumberOfColumns
        \xdef\TEW@save@row{}%
        \loop
          \advance\count@\m@ne
        \ifnum\count@>\z@
          \xdef\TEW@save@row{\TEW@save@row&\omit}%
        \repeat}%
      \TEW@save@row
      \endarray
    \global\setbox\@ne\lastbox
    }%
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@tabfinal}
% This mimics a regular \textsf{tabular}, the only difference
% being that |\CurrentColumn| is updated before evaluating
% the content of each cell and the command |\GetExcessWidth| is
% made available.
%    \begin{macrocode}
\def\TEW@tabfinal{%
  \leavevmode
  \let\GetExcessWidth\@GetExcessWidth
  \let\@mkpream\TEW@mkpream
  \gdef\@halignto{}%
  \col@sep\tabcolsep
  \let\d@llarbegin\begingroup\let\d@llarend\endgroup
  \expandafter\TEW@tabarray\the\toks@\endarray}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@tabarray}
% \begin{macro}{\TEW@array}
% Handle the optional position argument.
%    \begin{macrocode}
\def\TEW@tabarray{\@ifnextchar[{\TEW@array}{\@array[t]}}
%    \end{macrocode}

%    \begin{macrocode}
\def\TEW@array[#1]{\@array[t]}
%    \end{macrocode}
% \end{macro}
% \end{macro}

% \begin{macro}{\TEW@@mkpream}
% \begin{macro}{\TEW@@arraycr}
% \begin{macro}{\TEW@@multicolumn}
% Saved versions.
%    \begin{macrocode}
\let\TEW@@mkpream\@mkpream
\let\TEW@@arraycr\@arraycr
\let\TEW@@multicolumn\multicolumn
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}

% \begin{macro}{\TEW@mkpream}
% This is a one-shot customized version, that redifines itself
% to the regular version. It's not clear to me why this is
% needed. Maybe because the regular version is used to process
% the multicolumns' preambles?
%    \begin{macrocode}
\def\TEW@mkpream{%
  \global\NumberOfColumns\@ne
  \global\CurrentColumn\@ne
  \let\@addamp\TEW@addamp
  \global\let\@mkpream\TEW@@mkpream % needed!
  \TEW@@mkpream}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@arraycr}
%    \begin{macrocode}
\def\TEW@arraycr{%
  \global\CurrentColumn\@ne
  \TEW@@arraycr
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@multicolumn}
% For the multicolumn mechanism to work, the first token of the
% expansion must be |\omit|.
%    \begin{macrocode}
\long\def\TEW@multicolumn#1#2#3{%
  % Can't place anything before \omit
  \TEW@@multicolumn{#1}{#2}{\global\advance\CurrentColumn\@ne#3}%
  \global\advance\CurrentColumn#1%
  \global\advance\CurrentColumn\m@ne
  \ignorespaces}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@multicolumnempty}
% To behave exactly as explained in the introduction, the special case
% of a ``fake'' multicolumn should be dealt with. I don't think this
% would give any practical advantage, though.
%    \begin{macrocode}
\long\def\TEW@multicolumnempty#1#2#3{\multispan#1\relax}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@addamp}
% During the evaluation of |\@mkpream|, while processing the pramble,
% each `\texttt{\&}' increases |\NumberOfColumns|. After that, it will
% keep it's value. During the evaluation of |\@preamble|, while
% building a row, each `\texttt{\&}' increases |\CurrentColumn|.
% The latter is reset at the end of each row when
% |\@arraycr| is evaluated.
%    \begin{macrocode}
\def\TEW@addamp{%
  \if@firstamp\@firstampfalse
  \else
    \global\advance\NumberOfColumns\@ne
    \expandafter\def\expandafter\@preamble\expandafter{\@preamble
      &\global\advance\CurrentColumn\@ne}%
  \fi
  }%
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@measure}
% Take the last row apart, unbox it, take each cell in turn
% and note its width.
%    \begin{macrocode}
\def\TEW@measure{%
  \setbox\z@\vbox{\unvbox\@ne\unskip\global\setbox\@ne\lastbox}%
  \setbox\tw@\hbox{%
    \count@\NumberOfColumns
    \unhbox\@ne
    \loop
      \unskip
      \setbox\tw@\lastbox
    \ifhbox\tw@
      \@computation{\wd\tw@}%
      \advance\count@\m@ne
    \repeat
    }%
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@firstcomp}
% The argument is stored in the database.
%    \begin{macrocode}
\def\TEW@firstcomp#1{%
Z \message{Col \the\count@: Intrinsic Width=\the#1^^J}%
  \TEW@width\xdef{\the#1}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@secondcomp}
% The argument is used to compute the excess width which is then stored
% in the database.
%    \begin{macrocode}
\def\TEW@secondcomp#1{%
  \TEW@width\dimen@
  \advance\dimen@-#1%
  \multiply\dimen@\m@ne
Z \message{Col \the\count@: Excess width=\the\dimen@^^J}%
  \TEW@width\xdef{\the\dimen@}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@width}
% A shorthand.
%    \begin{macrocode}
\def\TEW@width#1{%
  \expandafter#1\csname TEW@\the\count@\endcsname}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@GetExcessWidth}
% Look up the excess widths database.
%    \begin{macrocode}
\def\@GetExcessWidth#1{%
  \count@\CurrentColumn
%    \end{macrocode}
% Process the optional sign. Need
% to |\relax| at the end of the assignment, to prevent the following
% token from
% being expanded too early.
%    \begin{macrocode}
  \@ifnextchar+{\advance\count@}{%
    \@ifnextchar-{\advance\count@}{\count@}}#1\relax
%    \end{macrocode}
% Check that the column exists.
%    \begin{macrocode}
  \@tempswafalse
  \ifnum\count@>\z@
    \ifnum\count@>\NumberOfColumns
    \else\@tempswatrue
    \fi\fi
%    \end{macrocode}
% Retrieve the datum.
%    \begin{macrocode}
  \if@tempswa\TEW@width\ExcessWidth
  \else\ExcessWidth\z@
    \TEW@warn{nonexistent column \the\count@, assuming EW=0pt}%
  \fi
Z \message{EW in col. \the\CurrentColumn\space references
Z   col. \the\count@: \the\ExcessWidth^^J}%
  }
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@GetExcessWidthz@}
% To be used in the first two passes, when the excess widths are still
% unknown.
%    \begin{macrocode}
\def\@GetExcessWidthz@#1{\ExcessWidth\z@}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@spew}
% A useful shorthand.
%    \begin{macrocode}
\def\TEW@spew#1#2{\GetExcessWidth{#2}\hspace{#1\ExcessWidth}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\TEW@warn}
% Warning messages.
%    \begin{macrocode}
\def\TEW@warn{%
  \PackageWarning{tabularew}}
%    \end{macrocode}
% \end{macro}

%    \begin{macrocode}
\catcode`\Z=11
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \PrintIndex
% \Finale
%