% \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
%
