summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/tabulary/tabulary.dtx
blob: b455cf03820973589ab48950b1212e51dd95e57a (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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
% \iffalse
%%
%% File `tabulary.dtx'.
%% Copyright (C) 1995 1996 2003 2008 David Carlisle
%% This file may be distributed under the terms of the LPPL.
%% See 00readme.txt for details.
%%
%
%<*dtx>
          \ProvidesFile{tabulary.dtx}
%</dtx>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{tabulary}
% \fi
%         \ProvidesFile{tabulary.dtx}
          [2014/06/11 v0.10 tabulary package (DPC)]
%
% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{tabulary}
\EnableCrossrefs
\CodelineIndex
\GetFileInfo{tabulary.dtx}
\title{The \textsf{tabulary} package\thanks{This file
         has version number \fileversion, last
         revised \filedate.}}
\author{David Carlisle}
\date{\filedate}
\RecordChanges
\begin{document}
\maketitle
\DocInput{tabulary.dtx}
\PrintChanges
\end{document}
%</driver>
% \fi
% \CheckSum{1346}
%
% \changes{v0.1}{1995/10/08}
%      {Initial version}
% \changes{v0.2}{1995/11/05}
%      {Changed everything except the name}
% \changes{v0.3}{1995/12/21}
%      {Changed everything except the name: s and CLRS added}
% \changes{v0.5}{1998/12/08}
%      {Further SPQR modifications to multi pass table env}
% \changes{v0.6}{2003/02/26}
%      {Remove multi pass table env and unboxed star form}
% \changes{v0.8}{2003/09/14}
%      {Rename S to J and `hide' s (until it works)}
% \changes{v0.10}{2014/06/21}
%      {support \cs{cellcolor} see  http://tex.stackexchange.com/a/185851/1090}
%
% \section{User Documentation}
%
%
% |\begin{tabulary}|^^A
%       \marg{length}\marg{pream}| |\ \ldots\ |\end{tabulary}|
%
% The rather daft name may change in a later release but it is a pun
% on |tabularx|, which itself was a pun on |tabular*|\ldots
%
% These environments work pretty much like the standard tabular
% environment (or more correctly, the enhanced version from the array
% package) except that there are more possibilities for the column
% types.
% \begin{description}
% \iffalse
% \item[s] An `|s|' column relates to a `|c|' column the way the |[s]|
% |\makebox| option relates to |[c]|. The entries are set to the
% widest width in the column, but no centering is done, any `glue' in
% the entry will be used to `stretch' the text across the column. This
% package adds the |s| column type to \emph{all} array and tabular
% environments.
% \fi
% \item[LCRJ] These new `uppercase' column types are only activated in
% the |tabulary| environment.
% In order to make the total table width equal to \meta{length} the
% |LCRJ| columns are converted to |p| columns (with |\raggedright|,
% |\centering|, or |\raggedleft| or normal justification respectively
% applied). The width of  these converted columns is proportional to
% the natural width of the longest entry in each column.
% \end{description}
%
% To stop very narrow columns being too `squeezed' by this process any
% columns that are narrower than |\tymin| are set to their natural
% width. This length may be set with |\setlength| and is
% arbitrarily initialised to 10\,pt. (If you know that a column will
% be narrow, it may be preferable to use, say, |c| rather than |C| so
% that the \textsf{tabulary} mechanism is never invoked on that column.)
%
% Similarly one very large entry can force its column to be too wide.
% So to prevent this, all columns with natural length greater than
% |\tymax| are set to the same width (with the proportion being taken
% as if the natuaral length was \emph{equal} to |\tymax|). This is
% initially set to twice the text width..
%
% Narrow |p| columns are sometimes quite hard to set, and so
% you may redefine the command |\tyformat| to be any declarations
% to make just after the |\centering| or |\ragged|\ldots\ declaration.
% By default it redefines |\everypar| to insert a zero space at the
% start of every paragraph, so the first word may be hyphenated. (See
% DogBook).
%
%
% As the environment makes a standard \LaTeX\ box, it will be indented
% by the paragraph indent at the start of a paragraph, and so will not
% fit on a line if given argument |\textwidth| unless it is preceded by
% |\noindent| or is in a |center| environment or some other
% environment with zero paragraph indent.
%
% \section{Features}
%
%
% You can use |\multicolumn| but if the multicolumn text turns out to
% be longer than the final calculated widths of the columns that it
% spans, then the final table will be too wide.
%
% |\verb| doesnt work. (except in restricted version as in tabularx)
%
% The whole table is evaluated twice, so take care with some
% \TeX\ constructions that may have side effects like writing to files.
%
%
% \section{Options}
%  The following package option is defined:
%  \begin{description}
%  \item[debugshow] Causes a lot of stuff to appear on the terminal.
%   I find this invaluable, you may find it less so.
% \end{description}
%
% \clearpage
% \section{Examples}
%
% \begin{center}
%
% With C columns\\
%\begin{tabulary}{\linewidth}{C|C@{ (an @ expr.) }C}
% 1&the rain in spain falls mainly on the plain&
% the rain in spain falls mainly on the plain
% the rain in spain falls mainly on the plain\\
% a&b&c\\
% a a&b b&c c
% \end{tabulary}
%
% \bigskip
%
% With J columns\\
%\begin{tabulary}{\linewidth}{J|J@{ (an @ expr.) }J}
% 1&the rain in spain falls mainly on the plain&
% the rain in spain falls mainly on the plain
% the rain in spain falls mainly on the plain\\
% a&b&c\\
% a a&b b&c c
% \end{tabulary}
%
% \bigskip
%
% With L, R and C columns, and a |\multicolumn|\\
% \begin{tabulary}{\linewidth}{LR|LC}
% 1&the rain in spain falls mainly on the plain&
% the rain in spain falls mainly on the plain
% the rain in spain falls mainly on the plain&
% and now for something completely different\\
% x&\multicolumn{3}{c}
%      {some multicolumn text across columns 2--4}\\
% a&b&c&d\\
% a a&b b&c c&d d
% \end{tabulary}
% \end{center}
%
% \newcommand\test[3]{^^A
% \begin{center}
% \setlength\tymin{#1}^^A
% \setlength\tymax{#2}^^A
% \parbox{200pt}
%    {\texttt{\string\tymin=#1\\ \string\tymax=\string#2}\\#3}\par
% \fbox{\tiny\begin{tabulary}{200pt}{|C|C|C|C|}
%  a&b b b b&
% c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c&
% d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d
% d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d
% d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d
% d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d d
% \end{tabulary}}
% \end{center}}
%
% \clearpage
% 
% The following examples attempt to show the effect of the |\tymin| and
% |\tymax| parameters. One should also perhaps note that |\tymax|
% refers to the total column width (inluding any inter-column space,
% rules etc) but |\tymin| just refers to the width of the column entry
% (like the argument to the standard |p| column).
%
% \test{0pt}{\maxdimen}{Note how the first column is `squeezed'.
% In fact it is in such a narrow column that even `a' produces an
% overfull box warning!}
%
% \test{20pt}{\maxdimen}{Here increase \texttt{\string\tymin} so that
% columns b and a are not so narrow. `a' is set to its natural width,
% and `b' is set to \texttt{\string\tymin}.}
%
% \test{20pt}{200pt}{In the previous example, the large d column
% dominated the table, being a lot wider than the c column. By
% reducing \texttt{\string\tymax} can limit the width of column d
% producing more even column widths, but now producing an entry for d
% that is longer than that for c.}
%
%
%  \StopEventually{}
%
% \clearpage
% \section{The Code}
%
%    \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}{\arraybackslash}
% Borrowed from tabularx.
%    \begin{macrocode}
\def\arraybackslash{\let\\=\@arraycr}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@finalstrut}
% Bug fixed version from December 1995 \LaTeX\ release.
% Old bug going back to \LaTeX2.09\ldots
%    \begin{macrocode}
\def\@finalstrut#1{%
  \unskip\ifhmode\nobreak\fi\vrule\@width\z@\@height\z@\@depth\dp#1}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@count}
% Counter so that we know what column we are hacking around in.
%    \begin{macrocode}
\newcount\TY@count
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tabulary}
% Top level macro for standard form.
%    \begin{macrocode}
\def\tabulary{%
  \let\TY@final\tabular
  \let\endTY@final\endtabular
  \TY@tabular}
%    \end{macrocode}
% \end{macro}
%
%^^A% \begin{macro}{\tabulary*}
%^^A% Top level macro for star form, unboxes everything so it will break
%^^A% over a page.
%^^A%    \begin{macrocode}
%^^A\@namedef{tabulary*}{%
%^^A   \def\TY@final{\def\@arrayleft{\let\@arrayleft\relax
%^^A       \global\setbox\@ne}\tabular[t]}%
%^^A  \def\endTY@final{\endtabular\endgraf\unvbox\@ne}%
%^^A  \TY@tabular}
%^^A\@namedef{endtabulary*}{\endtabulary}
%^^A%    \end{macrocode}
%^^A% \end{macro}
%
% \begin{macro}{\TY@tabular}
% Looks a lot like tabularx at this stage. Grab everything into a
% token register.
%    \begin{macrocode}
\def\TY@tabular#1{%
  \edef\TY@{\@currenvir}%
  {\ifnum0=`}\fi
%    \end{macrocode}
% At this point need to save locally things that \textsf{tabulary}
% will globally mess up. These are restored at the end of the
% environment.
% \changes{v0.4}{1996/03/29}
%      {Locally preserve global commands}
%    \begin{macrocode}
  \@ovxx\TY@linewidth
  \@ovyy\TY@tablewidth
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
  \advance\count@\@ne
  \expandafter\ifx\csname TY@F\the\count@\endcsname\relax
    \@tempswafalse
  \else
    \expandafter\let\csname TY@SF\the\count@\expandafter\endcsname
                     \csname TY@F\the\count@\endcsname
    \global\expandafter\let\csname TY@F\the\count@\endcsname\relax
    \expandafter\let\csname TY@S\the\count@\expandafter\endcsname
                     \csname TY@\the\count@\endcsname
  \fi}%
%    \end{macrocode}
%
%    \begin{macrocode}
    \global\TY@count\@ne
    \TY@width\xdef{0pt}%
    \global\TY@tablewidth\z@
    \global\TY@linewidth#1\relax
Z\message{^^J^^JTable^^J%
Z        Target Width: \the\TY@linewidth^^J%
Z        \string\tabcolsep: \the\tabcolsep\space
Z        \string\arrayrulewidth: \the\arrayrulewidth\space
Z        \string\doublerulesep: \the\doublerulesep^^J%
Z        \string\tymin: \the\tymin\space
Z        \string\tymax: \the\tymax^^J}%
%    \end{macrocode}
%   Placing this here means that nested tabulars will get this
%   definition but that's probably OK, the extra code for LCR etc
%   shouldn't do any harm
%    \begin{macrocode}
    \let\@classz\TY@classz
%    \end{macrocode}
%
%    \begin{macrocode}
    \let\verb\TX@verb
%    \end{macrocode}
%
%    \begin{macrocode}
    \toks@{}\TY@get@body}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\TY@@mkpream}
%    Saved version.
%    \begin{macrocode}
\let\TY@@mkpream\@mkpream
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\TY@mkpream}
%    TY version.
%    \begin{macrocode}
\def\TY@mkpream{%
    \def\@addamp{%
      \if@firstamp \@firstampfalse \else
      \global\advance\TY@count\@ne
      \edef\@preamble{\@preamble &}\fi
      \TY@width\xdef{0pt}}%
%    \end{macrocode}
%
%    \begin{macrocode}
    \def\@acol{%
      \TY@subwidth\col@sep
      \@addtopreamble{\hskip\col@sep}}%
%    \end{macrocode}
%
%    \begin{macrocode}
    \let\@arrayrule\TY@arrayrule
%    \end{macrocode}
%
%    \begin{macrocode}
    \let\@classvi\TY@classvi
%    \end{macrocode}
%
%    \begin{macrocode}
    \def\@classv{\save@decl
      \expandafter\NC@ecs\@nextchar\extracolsep{}\extracolsep\@@@
      \sbox\z@{\d@llarbegin\@nextchar\d@llarend}%
      \TY@subwidth{\wd\z@}%
      \@addtopreamble{\d@llarbegin\the@toks\the\count@\relax\d@llarend}%
      \prepnext@tok}%
%    \end{macrocode}
%
%    \begin{macrocode}
  \global\let\@mkpream\TY@@mkpream
%    \end{macrocode}
%
%    \begin{macrocode}
  \TY@@mkpream}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\TY@arrayrule}
% \changes{v0.6}{2003/02/26}{macro added}
% Pull this out so the colortbl support below can redefine
%    \begin{macrocode}
\def\TY@arrayrule{%
  \TY@subwidth\arrayrulewidth
  \@addtopreamble \vline}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\TY@classvi}
% \changes{v0.6}{2003/02/26}{macro added}
% Pull this out so the colortbl support below can redefine
%    \begin{macrocode}
\def\TY@classvi{\ifcase \@lastchclass
  \@acol \or
  \TY@subwidth\doublerulesep
  \@addtopreamble{\hskip \doublerulesep}\or
  \@acol \or
  \@classvii
  \fi}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\TY@tab}
% First run a tabular with all the column types fudged
% so that the widths of any rules or @-expresions are noted.
%    \begin{macrocode}
\def\TY@tab{%
  \setbox\z@\hbox\bgroup
%    \end{macrocode}
% Support displaymath by making it non-display in the first pass.
% (Other display environments defined in terms of |$$| would need
% to be added here by packages that define them.)
%    \begin{macrocode}
  \let\[$\let\]$%
  \let\equation$\let\endequation$%
%    \end{macrocode}
%
%    \begin{macrocode}
    \col@sep\tabcolsep
    \let\d@llarbegin\begingroup\let\d@llarend\endgroup
%    \end{macrocode}
%
%    \begin{macrocode}
    \let\@mkpream\TY@mkpream
%    \end{macrocode}
%
%    \begin{macrocode}
      \def\multicolumn##1##2##3{\multispan##1\relax}%
    \CT@start\TY@tabarray}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@tabarray}
% \changes{v0.7}{2003/09/07}
%      {new macro to support [t] optional arg}
%    \begin{macrocode}
\def\TY@tabarray{\@ifnextchar[{\TY@array}{\@array[t]}}
\def\TY@array[#1]{\@array[t]}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@width}
% Just a shorthand to access a column width macro.
%    \begin{macrocode}
\def\TY@width#1{%
  \expandafter#1\csname TY@\the\TY@count\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@subwidth}
% Subtract a width from the current column width and also
% The total line table width and the target line width.
%    \begin{macrocode}
\def\TY@subwidth#1{%
  \TY@width\dimen@
  \advance\dimen@-#1\relax
  \TY@width\xdef{\the\dimen@}%
  \global\advance\TY@linewidth-#1\relax}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endtabulary}
% First run one modified tabular, making sure to add a
% blank row (cf longtable) to the end in case the user supplied last
% row is hidden by an hline or something.
%    \begin{macrocode}
\def\endtabulary{%
  \gdef\@halignto{}%
  \expandafter\TY@tab\the\toks@
  \crcr\omit
  {\xdef\TY@save@row{}%
     \loop
    \advance\TY@count\m@ne
    \ifnum\TY@count>\z@
    \xdef\TY@save@row{\TY@save@row&\omit}%
    \repeat}\TY@save@row
  \endarray\global\setbox1=\lastbox\setbox0=\vbox{\unvbox1
    \unskip\global\setbox1=\lastbox}\egroup
%    \end{macrocode}
% Check that |\tymin| is not too large.
%    \begin{macrocode}
  \dimen@\TY@linewidth
  \divide\dimen@\TY@count
  \ifdim\dimen@<\tymin
    \TY@warn{tymin too large (\the\tymin), resetting to \the\dimen@}%
    \tymin\dimen@
  \fi
%    \end{macrocode}
% Now take the last row apart, cf longtable or appendix D.
%    \begin{macrocode}
  \setbox\tw@=\hbox{\unhbox\@ne
    \loop
\@tempdima=\lastskip
\ifdim\@tempdima>\z@
Z   \message{ecs=\the\@tempdima^^J}%
   \global\advance\TY@linewidth-\@tempdima
\fi
    \unskip
    \setbox\tw@=\lastbox
    \ifhbox\tw@
Z     \message{Col \the\TY@count: Initial=\the\wd\tw@\space}%
      \ifdim\wd\tw@>\tymax
        \wd\tw@\tymax
Z       \message{> max\space}%
Z     \else
Z       \message{ \@spaces\space}%
      \fi
  \TY@width\dimen@
Z \message{\the\dimen@\space}%
  \advance\dimen@\wd\tw@
Z \message{Final=\the\dimen@\space}%
   \TY@width\xdef{\the\dimen@}%
      \ifdim\dimen@<\tymin
Z        \message{< tymin}%
         \global\advance\TY@linewidth-\dimen@
         \expandafter\xdef\csname TY@F\the\TY@count\endcsname
                                                        {\the\dimen@}%
       \else
      \expandafter\ifx\csname TY@F\the\TY@count\endcsname\z@
Z        \message{***}%
         \global\advance\TY@linewidth-\dimen@
         \expandafter\xdef\csname TY@F\the\TY@count\endcsname
                                                        {\the\dimen@}%
        \else
Z        \message{> tymin}%
         \global\advance\TY@tablewidth\dimen@
         \global\expandafter\let\csname TY@F\the\TY@count\endcsname
                                                               \maxdimen
       \fi\fi
       \advance\TY@count\m@ne
    \repeat}%
%    \end{macrocode}
%
% A bit cheap just doing this four times, but prevents any
% possibilities of looping\ldots.
%    \begin{macrocode}
    \TY@checkmin
    \TY@checkmin
    \TY@checkmin
    \TY@checkmin
%    \end{macrocode}
% Reset the counter.
%    \begin{macrocode}
    \TY@count\z@
%    \end{macrocode}
%
% Reset the LCRJ column definition to set paragraphs to the calculated
% widths.
%    \begin{macrocode}
    \let\TY@box\TY@box@v
%    \end{macrocode}
%
% Run a second tabular, and for the star form, unbox it.
%    \begin{macrocode}
  {\expandafter\TY@final\the\toks@\endTY@final}%
%    \end{macrocode}
%
% Finish off by restoring global commands.
%    \begin{macrocode}
  \count@\z@
  \@tempswatrue
  \@whilesw\if@tempswa\fi{%
  \advance\count@\@ne
  \expandafter\ifx\csname TY@SF\the\count@\endcsname\relax
    \@tempswafalse
  \else
    \global\expandafter\let\csname TY@F\the\count@\expandafter\endcsname
                   \csname TY@SF\the\count@\endcsname
    \global\expandafter\let\csname TY@\the\count@\expandafter\endcsname
                   \csname TY@S\the\count@\endcsname
  \fi}%
  \TY@linewidth\@ovxx
  \TY@tablewidth\@ovyy
%    \end{macrocode}
%
%    \begin{macrocode}
    \ifnum0=`{\fi}}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\TY@checkmin}
% Check that no column is squeezed below |\tymin|. If it is, fix the
% width of that column to |\tymin| and try again re-computing the
% ratio. (The new ratio will be smaller, and may squeeze yet more
% rows, so need to iterate this, currently just do it four times.)
%    \begin{macrocode}
\def\TY@checkmin{%
  \let\TY@checkmin\relax
\ifdim\TY@tablewidth>\z@
  \Gscale@div\TY@ratio\TY@linewidth\TY@tablewidth
% \changes{v0.9}{2008/12/01}
%      {\cs{TY@linewidth}}
 \ifdim\TY@tablewidth <\TY@linewidth
   \def\TY@ratio{1}%
 \fi
\else
  \TY@warn{No suitable columns!}%
  \def\TY@ratio{1}%
\fi
\count@\z@
Z \message{^^JLine Width: \the\TY@linewidth,
Z             Natural Width: \the\TY@tablewidth,
Z             Ratio: \TY@ratio^^J}%
\@tempdima\z@
\loop
\ifnum\count@<\TY@count
\advance\count@\@ne
  \ifdim\csname TY@F\the\count@\endcsname>\tymin
    \dimen@\csname TY@\the\count@\endcsname
    \dimen@\TY@ratio\dimen@
    \ifdim\dimen@<\tymin
Z     \message{Column \the\count@\space ->}%
%    \end{macrocode}
%
% \changes{v0.4}{1996/03/29}
%      {\cs{global} added}
%    \begin{macrocode}
      \global\expandafter\let\csname TY@F\the\count@\endcsname\tymin
      \global\advance\TY@linewidth-\tymin
      \global\advance\TY@tablewidth-\csname TY@\the\count@\endcsname
      \let\TY@checkmin\TY@@checkmin
    \else
%    \end{macrocode}
%
% \changes{v0.4}{1996/03/29}
%      {\cs{xdef} not \cs{edef}}
%    \begin{macrocode}
      \expandafter\xdef\csname TY@F\the\count@\endcsname{\the\dimen@}%
      \advance\@tempdima\csname TY@F\the\count@\endcsname
    \fi
  \fi
Z \dimen@\csname TY@F\the\count@\endcsname\message{\the\dimen@, }%
\repeat
Z \message{^^JTotal:\the\@tempdima^^J}%
}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\TY@@checkmin}
% Saved value 
%    \begin{macrocode}
\let\TY@@checkmin\TY@checkmin
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{TY@linewidth}
% Stores the target width.
%    \begin{macrocode}
\newdimen\TY@linewidth
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tyformat}
% What to do with columns
%    \begin{macrocode}
\def\tyformat{\everypar{{\nobreak\hskip\z@skip}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{tymin}
% Columns narrower than this are not fudged.
%    \begin{macrocode}
\newdimen\tymin
\tymin=10pt
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{tymin}
% Columns wider than this are all treated alike and set to the same
% width, to stop one particularly long entry hijacking the entire
% table.
%    \begin{macrocode}
\newdimen\tymax
\tymax=2\textwidth
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\@testpatch}
% \iffalse
% Add |s| possibility. This works for \emph{all} |array| and |tabular|
% environments.
% \fi
% Also add |LCRJ| although these don't do anything useful
% except in |tabulary|.
%    \begin{macrocode}
\def\@testpach{\@chclass
 \ifnum \@lastchclass=6 \@ne \@chnum \@ne \else
  \ifnum \@lastchclass=7 5 \else
   \ifnum \@lastchclass=8 \tw@ \else
    \ifnum \@lastchclass=9 \thr@@
   \else \z@
   \ifnum \@lastchclass = 10 \else
   \edef\@nextchar{\expandafter\string\@nextchar}%
   \@chnum
   \if \@nextchar c\z@ \else
    \if \@nextchar l\@ne \else
     \if \@nextchar r\tw@ \else
%     \if \@nextchar s6 \else
   \if \@nextchar C7 \else
    \if \@nextchar L8 \else
     \if \@nextchar R9 \else
     \if \@nextchar J10 \else
   \z@ \@chclass
   \if\@nextchar |\@ne \else
    \if \@nextchar !6 \else
     \if \@nextchar @7 \else
      \if \@nextchar <8 \else
       \if \@nextchar >9 \else
  10
  \@chnum
  \if \@nextchar m\thr@@\else
   \if \@nextchar p4 \else
    \if \@nextchar b5 \else
   \z@ \@chclass \z@ \@preamerr \z@ \fi \fi \fi \fi\fi \fi \fi\fi \fi
%   \fi
     \fi  \fi  \fi  \fi  \fi  \fi \fi \fi \fi \fi \fi}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\TY@classz}
% Here hacked around without the respect Frank's code deserves\ldots
%    \begin{macrocode}
\def\TY@classz{%
  \@classx
  \@tempcnta\count@
  \ifx\TY@box\TY@box@v
    \global\advance\TY@count\@ne
  \fi
  \let\centering c%
  \let\raggedright\noindent
  \let\raggedleft\indent
  \let\arraybackslash\relax
  \prepnext@tok
  \ifnum\@chnum<4 
    \global\expandafter\let\csname TY@F\the\TY@count\endcsname\z@
  \fi
  \ifnum\@chnum=6
    \global\expandafter\let\csname TY@F\the\TY@count\endcsname\z@
  \fi
  \@addtopreamble{%
    \ifcase\@chnum
      \hfil \d@llarbegin\insert@column\d@llarend \hfil \or
      \kern\z@
       \d@llarbegin \insert@column \d@llarend \hfil \or
      \hfil\kern\z@ \d@llarbegin \insert@column \d@llarend \or
      $\vcenter\@startpbox{\@nextchar}\insert@column \@endpbox $\or
      \vtop \@startpbox{\@nextchar}\insert@column \@endpbox \or
      \vbox \@startpbox{\@nextchar}\insert@column \@endpbox \or
      \d@llarbegin \insert@column \d@llarend \or% dubious "s" case
      \TY@box\centering\or
      \TY@box\raggedright\or
      \TY@box\raggedleft\or
      \TY@box\relax
    \fi}\prepnext@tok}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@box}
% The argument is |\centering| etc depending on whether LCRJ is used.
% However in this version the entries are set in horizontal mode with
% definitions mimicing the standard  lcr columns. Later |\TY@box| will
% be redefined to |\TY@box@v| which really sets the entries in
% vertical mode.
%    \begin{macrocode}
\def\TY@box#1{%
  \ifx\centering#1%
      \hfil \d@llarbegin\insert@column\d@llarend \hfil \else
  \ifx\raggedright#1%
        \kern\z@%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      \d@llarbegin \insert@column \d@llarend \hfil \else
  \ifx\raggedleft#1%
      \hfil\kern\z@ \d@llarbegin \insert@column \d@llarend \else
  \ifx\relax#1%
       \d@llarbegin \insert@column \d@llarend
  \fi  \fi  \fi  \fi}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\TY@box@v}
% The version to use in a final run, set the |CLRJ| columns in a
% parbox of the appropriate width.
%    \begin{macrocode}
\def\TY@box@v#1{%
      \vtop \@startpbox{\csname TY@F\the\TY@count\endcsname}%
              #1\arraybackslash\tyformat
                              \insert@column\@endpbox}
%    \end{macrocode}
%  \end{macro}
%
% \begin{macro}{\TY@tablewidth}
% The natural width of the table on the first run.
%    \begin{macrocode}
\newdimen\TY@tablewidth
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\Gscale@div}
% Stolen from graphics package.
%    \begin{macrocode}
\def\Gscale@div#1#2#3{%
  \setlength\dimen@{#3}%
  \ifdim\dimen@=\z@
    \PackageError{graphics}{Division by 0}\@eha
    \dimen@#2%
  \fi
  \edef\@tempd{\the\dimen@}%
  \setlength\dimen@{#2}%
  \count@65536\relax
  \ifdim\dimen@<\z@
    \dimen@-\dimen@
    \count@-\count@
  \fi
  \loop
    \ifdim\dimen@<8192\p@
      \dimen@\tw@\dimen@
      \divide\count@\tw@
  \repeat
  \dimen@ii=\@tempd\relax
  \divide\dimen@ii\count@
  \divide\dimen@\dimen@ii
  \edef#1{\strip@pt\dimen@}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@get@body}
% Place all tokens as far as the first |\end| into a token register.
% Then call |\TY@find@end| to see if we are at |\end{tabulary}|.
%    \begin{macrocode}
\long\def\TY@get@body#1\end
  {\toks@\expandafter{\the\toks@#1}\TY@find@end}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TY@find@end}
% If we are at |\end{tabulary}|, call |\end{tabulary}|, otherwise
% add |\end{...}| to the register, and call |\TY@get@body| again.
%    \begin{macrocode}
\def\TY@find@end#1{%
  \def\@tempa{#1}%
  \ifx\@tempa\TY@\def\@tempa{\end{#1}}\expandafter\@tempa
  \else\toks@\expandafter
    {\the\toks@\end{#1}}\expandafter\TY@get@body\fi}
%    \end{macrocode}
% \end{macro}
%
%  \begin{macro}{\TY@warn}
% Warning messages.
%    \begin{macrocode}
\def\TY@warn{%
  \PackageWarning{tabulary}}
%    \end{macrocode}
%  \end{macro}
%
%
%    \begin{macrocode}
\catcode`\Z=11
%    \end{macrocode}
%
% \textsf{colortbl} support.
%    \begin{macrocode}
\AtBeginDocument{
\@ifpackageloaded{colortbl}{%
\expandafter\def\expandafter\@mkpream\expandafter#\expandafter1%
  \expandafter{%
    \expandafter\let\expandafter\CT@setup\expandafter\relax
    \expandafter\let\expandafter\CT@color\expandafter\relax
    \expandafter\let\expandafter\CT@do@color\expandafter\relax
    \expandafter\let\expandafter\color\expandafter\relax
    \expandafter\let\expandafter\CT@column@color\expandafter\relax
    \expandafter\let\expandafter\CT@row@color\expandafter\relax
    \expandafter\let\expandafter\CT@cell@color\expandafter\relax
    \@mkpream{#1}}
\let\TY@@mkpream\@mkpream
\def\TY@classz{%
  \@classx
  \@tempcnta\count@
  \ifx\TY@box\TY@box@v
    \global\advance\TY@count\@ne
  \fi
  \let\centering c%
  \let\raggedright\noindent
  \let\raggedleft\indent
  \let\arraybackslash\relax
  \prepnext@tok
\expandafter\CT@extract\the\toks\@tempcnta\columncolor!\@nil
  \ifnum\@chnum<4
    \global\expandafter\let\csname TY@F\the\TY@count\endcsname\z@
  \fi
  \ifnum\@chnum=6
    \global\expandafter\let\csname TY@F\the\TY@count\endcsname\z@
  \fi
  \@addtopreamble{%
    \setbox\z@\hbox\bgroup\bgroup
    \ifcase\@chnum
      \hskip\stretch{.5}\kern\z@
      \d@llarbegin\insert@column\d@llarend\hskip\stretch{.5}\or
      \kern\z@%<<<<<<<<<<<<<<<<<<<<<<<<<<<
       \d@llarbegin \insert@column \d@llarend \hfill \or
      \hfill\kern\z@ \d@llarbegin \insert@column \d@llarend \or
      $\vcenter\@startpbox{\@nextchar}\insert@column \@endpbox $\or
      \vtop \@startpbox{\@nextchar}\insert@column \@endpbox \or
      \vbox \@startpbox{\@nextchar}\insert@column \@endpbox \or
      \d@llarbegin \insert@column \d@llarend \or% dubious s case
      \TY@box\centering\or
      \TY@box\raggedright\or
      \TY@box\raggedleft\or
      \TY@box\relax
    \fi
 \egroup\egroup
\begingroup
  \CT@setup
  \CT@column@color
  \CT@row@color
  \CT@cell@color
  \CT@do@color
\endgroup
        \@tempdima\ht\z@
        \advance\@tempdima\minrowclearance
        \vrule\@height\@tempdima\@width\z@
\unhbox\z@
}\prepnext@tok}%
%    \end{macrocode}
%
%    \begin{macrocode}
    \def\TY@arrayrule{%
      \TY@subwidth\arrayrulewidth
      \@addtopreamble{{\CT@arc@\vline}}}%
%    \end{macrocode}
%
%    \begin{macrocode}
    \def\TY@classvi{\ifcase \@lastchclass
      \@acol \or
      \TY@subwidth\doublerulesep
      \ifx\CT@drsc@\relax
        \@addtopreamble{\hskip\doublerulesep}%
      \else
        \@addtopreamble{{\CT@drsc@\vrule\@width\doublerulesep}}%
      \fi\or
      \@acol \or
      \@classvii
      \fi}%
%    \end{macrocode}
%
%    \begin{macrocode}
}{%
\let\CT@start\relax
}
%    \end{macrocode}
% end of at begin document
%    \begin{macrocode}
}
%    \end{macrocode}
%
%  \begin{macro}{\TX@warn}
% \changes{v0.6}{2003/02/26}{macro added}
% |\verb| support, uses same csnames as in TX so they share code if
% both loaded (this version names tabulary in the warning though).
% See tabularx for documentation.
%    \begin{macrocode}
{\uccode`\*=`\ %
\uppercase{\gdef\TX@verb{%
  \leavevmode\null\TX@vwarn
  {\ifnum0=`}\fi\ttfamily\let\\\ignorespaces
  \@ifstar{\let~*\TX@vb}{\TX@vb}}}}
\def\TX@vb#1{\def\@tempa##1#1{\toks@{##1}\edef\@tempa{\the\toks@}%
    \expandafter\TX@v\meaning\@tempa\\ \\\ifnum0=`{\fi}}\@tempa!}
\def\TX@v#1!{\afterassignment\TX@vfirst\let\@tempa= }
\begingroup
\catcode`\*=\catcode`\#
\catcode`\#=12
\gdef\TX@vfirst{%
  \if\@tempa#%
    \def\@tempb{\TX@v@#}%
  \else
    \let\@tempb\TX@v@
    \if\@tempa\space~\else\@tempa\fi
  \fi
  \@tempb}
\gdef\TX@v@*1 *2{%
  \TX@v@hash*1##\relax\if*2\\\else~\expandafter\TX@v@\fi*2}
\gdef\TX@v@hash*1##*2{*1\ifx*2\relax\else#\expandafter\TX@v@hash\fi*2}
\endgroup
\def\TX@vwarn{%
  \@warning{\noexpand\verb may be unreliable inside tabularx/y}%
  \global\let\TX@vwarn\@empty}
%    \end{macrocode}
%  \end{macro}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
% \PrintIndex
% \Finale
%