
% \iffalse
%
% This file is part of the array package.
% ---------------------------------------
%
% This file was contributed. In case of error please inform the
% original author.
%
% \fi

%\iffalse
%%% ====================================================================
%%%  @LaTeX-style-file{
%%%     author          = "David Carlisle",
%%%     version         = "1.04",
%%%     date            = "02 September 1992",
%%%     time            = "14:14:27 BST",
%%%     filename        = "tabularx.sty",
%%%     address         = "Computer Science Department
%%%                        Manchester University
%%%                        Oxford Road
%%%                        Manchester
%%%                        England
%%%                        M13 9PL",
%%%     telephone       = "+44 61 275 6139",
%%%     FAX             = "+44 61 275 6236",
%%%     checksum        = "26599 791 3987 29287",
%%%     email           = "carlisle@cs.man.ac.uk (Internet)",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "LaTeX, tabular, tabularx",
%%%     supported       = "yes",
%%%     docstring       = "
%%%
%%%     tabularx.sty
%%%
%%%     A LaTeX style option defining the tabularx environment. This is
%%%     like tabular*, except that the widths of certain `p' columns are
%%%     modified to achieve the desired total width, rather than the
%%%     width of the inter-column space.
%%%
%%%          \begin{tabularx}{final width}{tabular-preamble}
%%%              tabular body
%%%          \end{tabularx}
%%%
%%%     In the {tabular-preamble} argument, X should be used to denote a
%%%     column that gets the final specification of p{<whatever width is
%%%     needed>}. So for example
%%%          \begin{tabularx}{250pt}{|cXcX|} ...
%%%     is equivalent to
%%%          \begin{tabular*}{250pt}{|cp{??}cp{??}|} ...
%%%     where the ?? is chosen automatically.
%%%
%%%     This style requires array.sty.
%%%
%%%     Documentation requires Mittelbach's doc.sty.
%%%
%%%     The checksum field above was produced by
%%%     Robert Solovay's checksum utility.",
%%%  }
%%% ====================================================================
%
% Version 1.0   30 January 1992
% Version 1.01  07 July 1992 Re-issue for the new doc and docstrip.
% Version 1.02  17 July 1992 Added some support for \verb and \verb*
% Version 1.03  17 August 1992 Added \ifnum0='{\fi} brackets after bug
%               report posted by maassen@kreon.informatik.rwth-aachen.de
%               (Andreas Maassen) to comp.text.tex
% Version 1.04  02 September 1992 fixed \verb|#|, and support footnotes.
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\fi
%
\def\fileversion{v1.04}
\def\filedate{92/09/02}
\def\docdate {92/09/02}
%
% \CheckSum{432}
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \title{tabularx.sty\thanks
%   {Version \fileversion, revised \filedate, documentation dated
%   \docdate}}
% \author{D. P. Carlisle}
% \date{02 September 1992}
%
% \maketitle
% \begin{abstract}
% A new environment, {\tt tabularx}, is defined, which takes the same
% arguments as {\tt tabular*}, but modifies the widths of certain
% columns, rather than the inter column space, to set a table with the
% requested total width. The columns that may stretch are marked with
% the new token {\tt X} in the preamble argument.
%
% This style requires {\tt array.sty}.
% \end{abstract}
%
% \section{Introduction}
% This style file implements a version of the {\tt tabular} environment
% in which the widths of certain columns are calculated so that the
% table is is a specified width. Requests for such an environment seem
% to occur quite regularly in {\tt comp.text.tex}.
%
% The columns which are affected by the {\tt tabularx} environment
% should be denoted with the letter {\tt X} in the preamble argument.
% The {\tt X} column specification will be converted to "p{"\meta{some
% value}"}" once the correct column width has been calculated.
%
% \section{Examples}
% \tracingtabularx
% The following table is set with
% "\begin{tabularx}{250pt}{|c|X|c|X|} ...".
%
% \begin{center}
% \begin{tabularx}{250pt}{|c|X|c|X|}
% \hline
% \multicolumn{2}{|c|}{Multicolumn entry!}&
% THREE&
% FOUR\\
% \hline
% one&
% \raggedright\arraybackslash The width of this column depends on the
% width of the table.\footnote
% {You can now use {\tt \bslash footnote} inside {\tt tabularx}!}&
% three&
% \raggedright\arraybackslash Column four will act in the same way as
% column two, with the same width.\\
% \hline
% \end{tabularx}
% \end{center}
% If we change the first line to "\begin{tabularx}{300pt}{|c|X|c|X|}" we
% get:
% \begin{center}
% \begin{tabularx}{300pt}{|c|X|c|X|}
% \hline
% \multicolumn{2}{|c|}{Multicolumn entry!}&
% THREE&
% FOUR\\
% \hline
% one&
% \raggedright\arraybackslash The width of this column depends on the
% width of the table.&
% three&
% \raggedright\arraybackslash Column four will act in the same way as
% column two, with the same width.\\
% \hline
% \end{tabularx}
% \end{center}
%
% \edef\mytt{\expandafter\noexpand\csname
%             mediumseries\endcsname\noexpand\tt}
% \section{Differences between {\mytt tabularx} and {\mytt tabular*}}
% These two environments take the same arguments, to produce a table of
% a specified width. The main differences between them are:
% \begin{itemize}
% \item {\tt tabularx} modifies the widths of the {\em columns\/},
% whereas {\tt tabular*} modifies the widths of the inter-column {\em
% spaces\/}.
% \item {\tt tabular} and {\tt tabular*} environments may be nested with
% no restriction, however if one {\tt tabularx} environment occurs
% inside another, then the inner one {\em must\/} be enclosed by "{ }".
% \item The body of the {\tt tabularx} environment is in fact the
% argument to a command, and so certain constructions which are not
% allowed in command arguments (like "\verb") may not be used.\footnote
% {Since Version 1.02, {\tt\bslash verb and \tt\bslash verb*} may be
% used, but they may treat spaces incorrectly, and the argument can not
% contain an unmatched {\tt\char`\{} or {\tt\char`\}}, or a
% {\tt\char`\%} character.}
% \item {\tt tabular*} uses a primitive capability of \TeX\ to modify
% the inter column space of an alignment. {\tt tabularx} has to set the
% table several times as it searches for the best column widths, and is
% therefore much slower. Also the fact that the body is expanded several
% times may break certain \TeX\ constructs.
% \end{itemize}
%
% \section{Customising the behaviour of {\mytt tabularx}}
%
% \subsection{Terminal output}
% \DescribeMacro{\tracingtabularx}
% If this declaration is made, say in the document preamble, then all
% following {\tt tabularx} environments will print information about
% column widths as they repeatedly re-set the tables to find the correct
% widths.
%
% \subsection{The environment used to typeset the {\mytt X} columns}
% By default the {\tt X} specification is turned into
% "p{"\meta{some value}"}". Such narrow columns often
% require a special format, this may be achieved using the ">" syntax
% of {\tt array.sty}. So for example you may give a specification of
% ">{\small}X". Another format which is useful in narrow columns is
% ragged right, however \LaTeX's "\raggedright" macro redefines
% "\\" in a way which conflicts with its use in a tabular or array
% environments.
%\DescribeMacro{\arraybackslash}
% For this reason this style introduces the command "\arraybackslash",
% this may be used after a "\raggedright", "\raggedleft"  or
% "\centering" declaration. Thus a {\tt tabularx} preamble may specify\\
% ">{\raggedright\arraybackslash}X".
%
% \DescribeMacro{\newcolumntype}
% These preamble specifications may of course be saved using the
% "\newcolumntype" command defined in {\tt array.sty}. Thus we may
% say\\
% "\newcolumntype{Y}{>{\small\raggedright\arraybackslash}X}"\\
% and then use {\tt Y} in the {\tt tabularx} preamble argument.
%
% \DescribeMacro{\tabularxcolumn}
% The {\tt X} columns are set using the {\tt p} column which corresponds
% to "\parbox[t]". You may want them set using, say, the {\tt m} column,
% which corresponds to "\parbox[c]". It is not possible to change the
% column type using the ">" syntax, so another system is provided.
% "\tabularxcolumn" should be defined to be a macro with one argument,
% which expands to the {\tt tabular} preamble specification that you
% want to correspond to {\tt X}. The argument will be replaced by the
% calculated width of a column.
%
% The default is "\newcommand{\tabularxcolumn}[1]{p{#1}}". So we may
% change this with a command such as:\\
% "\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}}"
%
% \subsection{Column widths}
% Normally all {\tt X} columns in a single table are set to the same
% width, however it is possible to make {\tt tabularx} set them to
% different widths.
% A preamble argument of "{>{\hsize=.5\hsize}X>{\hsize=1.5\hsize}X}"
% specifies two columns, the second will be three times as wide as the
% first. However if you want to play games like this you should follow
% the following two rules.
% \begin{itemize}
% \item Make sure that the sum of the widths of all the {\tt X} columns
% is unchanged. (In the above example, the new widths still add up to
% twice the default width, the same as two standard {\tt X} columns.)
% \item Do not use "\multicolumn" entries which cross any {\tt X}
% column.
% \end{itemize}
% As with most rules, these may be broken if you know what you are
% doing.
%
% \StopEventually{}
%
% \section{The Macros}
%
%    \begin{macrocode}
%<*style>
\@ifundefined{TX@target}{}{\endinput}
\wlog{Style-Option: `tabularx' \fileversion
         \space\space <\filedate> (D.P.C.)}
\wlog{English documentation dated \space <\docdate> (D.P.C.)}

%    \end{macrocode}
%
% This requires {\tt array.sty}, so we may as well load it if
% "\newcolumntype" is not defined.
%    \begin{macrocode}
\@ifundefined{newcolumntype}{\input array.sty}{}
%    \end{macrocode}
%
% First some registers etc.\ that we need.
%    \begin{macrocode}
\newdimen\TX@col@width
\newdimen\TX@old@table
\newdimen\TX@old@col
\newdimen\TX@target
\newdimen\TX@delta
\newcount\TX@cols
\newif\ifTX@
%    \end{macrocode}
%
% Now a trick to get the body of an environment into a token register,
% without doing any expansion. This does not do any real checking of
% nested environments, so if you should need to nest one {\tt tabularx}
% inside another, the inner one must be surrounded by "{ }".
%
% \begin{macro}{\tabularx}
% Save the first two arguments in registers, and  clear the
% token register "\toks@". Then call "\TX@get@body" to begin
% saving the body of the table.
% The "{\ifnum0=`}\fi" was added at v1.03, to allow "tabularx" to appear
% inside a "\halign".^^A
% \setbox0=\hbox{\footnotesize"\iffalse{\fi\ifnum0=`}\fi"}^^A
% \setbox2=\hbox{\footnotesize"\ifnum0=`{}\fi"}^^A
% \footnote{This adds an extra level of grouping,
% which is not really needed. Instead, I could use \box0\ here, and
% \box2\ below, however the code here would then have to be moved after
% the first line, because of the footnote to page 386 of the \TeX{}Book,
% and I do not think I should be writing code that is so obscure as to
% be documented in a footnote in an appendix called ``Dirty Tricks''!}
%    \begin{macrocode}
\def\tabularx#1#2{%
  {\ifnum0=`}\fi
  \TX@target=#1
  \TX@typeout{Target width: #1 = \the\TX@target.}%
  \@temptokena={#2}\toks@={}\TX@get@body}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@get@body}
% Place all tokens as far as the first "\end" into a token register.
% Then call "\TX@find@end" to see if we are at "\end{tabularx}".
%    \begin{macrocode}
\long\def\TX@get@body#1\end
  {\toks@=\expandafter{\the\toks@#1}\TX@find@end}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@find@end}
% If we are at "\end{tabularx}", call "\TX@endtabularx", otherwise
% add "\end{...}" to the register, and call "\TX@get@body" again.
%    \begin{macrocode}
\def\TX@find@end#1{%
  \def\@tempa{#1}%
  \ifx\@tempa\TX@\expandafter\TX@endtabularx
  \else\toks@=\expandafter
    {\the\toks@\end{#1}}\expandafter\TX@get@body\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@}
% The string {\tt tabularx} as a macro for testing with "\ifx".
%    \begin{macrocode}
\def\TX@{tabularx}
%    \end{macrocode}
% \end{macro}
%
% Now that all the parts of the table specification are stored in
% registers, we can begin the work of setting the table.
%
% The algorithm for finding the correct column widths is as follows.
% Firstly set the table with each {\tt X} column the width of the final
% table. Assuming that there is at least one {\tt X} column, this will
% produce a table that is too wide. Divide the excess width by the
% number of {\tt X} columns, and reduce the column width by this amount.
% Reset the table. If the table is not now the correct width, a
% "\multicolumn" entry must be `hiding' one of the {\tt X} columns,
% and so there is one less {\tt X} column affecting the width of the
% table. So we reduce by 1 the number of X columns and repeat the
% process.
%
% \begin{macro}{\TX@endtabularx}
% Although I have tried to make {\tt tabularx} look like an environment,
% it is in fact a command, all the work is done by this macro.
%    \begin{macrocode}
\def\TX@endtabularx{%
%    \end{macrocode}
% Define the {\tt X} column, with an internal version of the
% "\newcolumntype" command. The "\expandafter" commands enable
% "\NC@newcol"  to get the {\em expansion} of\\
% "\tabularxcolumn{\TX@col@width}" as its
% argument. This will be the definition of an {\tt X} column, as
% discussed in section 4.
%    \begin{macrocode}
  \expandafter\TX@newcol\expandafter{\tabularxcolumn{\TX@col@width}}%
%    \end{macrocode}
% Initialise the column width, and the number of {\tt X} columns. The
% number of {\tt X} columns is set to one, which means that the initial
% count will be one too high, but this value is decremented before it is
% used in the main loop.
%
% Since v1.02, switch the definition of "\verb".
% Since v1.04, save the value of the {\tt footnote} counter.
%    \begin{macrocode}
  \let\verb\TX@verb
  \global\count@=\csname c@\@mpfn\endcsname
  \TX@old@table=\maxdimen
  \TX@col@width=\TX@target
  \global\TX@cols=\@ne
%    \end{macrocode}
% Typeout some headings (unless this is disabled).
%    \begin{macrocode}
  \TX@typeout
    {\@spaces Table Width\@spaces Column Width\@spaces X Columns}%
%    \end{macrocode}%
% First attempt. Modify the {\tt X} definition to count {\tt X} columns.
%    \begin{macrocode}
  \TX@trial{\def\NC@rewrite@X{%
          \global\advance\TX@cols\@ne\NC@find p{\TX@col@width}}}%
%    \end{macrocode}
% Repeatedly decrease column width until table is the correct width,
% or stops shrinking, or the columns become two narrow.
% If there are no multicolumn entries, this will only take one attempt.
%    \begin{macrocode}
  \loop
    \TX@arith
    \ifTX@
    \TX@trial{}%
  \repeat
%    \end{macrocode}
% One last time, with warnings back on (see appendix D)
% use {\tt tabular*} to put it in a box of the right size, in case the
% algorithm failed to find the correct size.
%
% Since v1.04, reset the "footnote" counter, and locally make
% "\footnotetext" save its argument in a token register.
% ("\@mpfn" expands to {\tt footnote} except in a {\tt minipage}
% environment.)
%    \begin{macrocode}
  \global\csname c@\@mpfn\endcsname=\count@
  {\let\@footnotetext\TX@ftntext\let\@xfootnotenext\TX@xftntext
    \csname tabular*\expandafter\endcsname\expandafter\TX@target
       \expandafter{\the\@temptokena}%
      \the\toks@
    \csname endtabular*\endcsname}%
%    \end{macrocode}
% Now the alignment is finished, and the "}" has restored the meaning of
% "\@footnotetext" expand the register "\TX@ftn" which will execute a
% series of\\
% "\footnotetext["\meta{num}"]{"\meta{note}"}"\\
% commands. We need to be careful about clearing the register as we may
% be inside a nested {\tt tabularx}.
%    \begin{macrocode}
  \global\TX@ftn\expandafter{\expandafter}\the\TX@ftn
%    \end{macrocode}
% Now finish off the {\tt tabularx} environment. Note that we need
% "\end{tabularx}" here as the "\end{tabularx}" in the user's
% file is never expanded.
% We also need to finish off the group started by "{\ifnum0=`}\fi" in
% the macro "\tabularx".
%    \begin{macrocode}
  \ifnum0=`{\fi}%
  \end{tabularx}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\TX@arith}
% Calculate the column width for the next try, setting the flag
% "\ifTX@" to false if the loop should be aborted.
%    \begin{macrocode}
\def\TX@arith{%
  \TX@false
  \ifdim\TX@old@table=\wd\@tempboxa
%    \end{macrocode}
% If we have reduced the column width, but the table width has not
% changed, we stop the loop, and output the table (which will cause an
% over-full alignment) with the previous value of "\TX@col@width".
%    \begin{macrocode}
    \TX@col@width=\TX@old@col
    \TX@typeout{Reached minimum width, backing up.}%
  \else
%    \end{macrocode}
% Otherwise calculate the amount by which the current table is too wide.
%    \begin{macrocode}
    \dimen@=\wd\@tempboxa
    \advance\dimen@ -\TX@target
    \ifdim\dimen@<\TX@delta
%    \end{macrocode}
% If this amount is less than "\TX@delta", stop. ("\TX@delta"
% should be non-zero otherwise we may miss the target due to rounding
% error.)
%    \begin{macrocode}
      \TX@typeout{Reached target.}%
    \else
%    \end{macrocode}
% Reduce the number of effective {\tt X} columns by one. (Checking that
% we do not get 0, as this would produce an error later.) Then divide
% excess width by the number of effective columns, and calculate the new
% column width. Temporarily store this value (times -1) in "\dimen@".
%    \begin{macrocode}
      \ifnum\TX@cols>\@ne
        \advance\TX@cols by \m@ne
      \fi
      \divide\dimen@ by \TX@cols
      \advance\dimen@ by -\TX@col@width
      \ifdim \dimen@ > \z@
%    \end{macrocode}
% If the new width would be too narrow, abort the loop. At the moment
% too narrow, means less than 0\,pt!
%    \begin{macrocode}
        \TX@typeout{Columns too narrow.}%
      \else
%    \end{macrocode}
% Otherwise save the old settings, and set the new column width. Set the
% flag to true so that the table will be set, and the loop will be
% executed again.
%    \begin{macrocode}
        \TX@old@col=\TX@col@width
        \TX@old@table=\wd\@tempboxa
        \TX@col@width=-\dimen@
        \TX@true
      \fi
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@delta}
% Accept a table that is within "\hfuzz" of the correct width.
%    \begin{macrocode}
\TX@delta=\hfuzz
%    \end{macrocode}
% \end{macro}
%
% Initialse the {\tt X} column. The definition can be empty here, as it
% is set for each {\tt tabularx} environment.
%    \begin{macrocode}
\newcolumntype{X}{}
%    \end{macrocode}
%
% \begin{macro}{\tabularxcolumn}
% The default definition of {\tt X} is "p{#1}".
%    \begin{macrocode}
\def\tabularxcolumn#1{p{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@newcol}
% A little macro just used to cut down the number of "\expandafter"
% commands needed.
%    \begin{macrocode}
\def\TX@newcol{\newcol@{X}[0]}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@trial}
% Make a test run.
%    \begin{macrocode}
\def\TX@trial#1{%
  \setbox\@tempboxa=\hbox{%
%    \end{macrocode}
% Any extra commands. This is used on the first run to count the number
% of {\tt X} columns.
%    \begin{macrocode}
    #1\relax
%    \end{macrocode}
% Since v1.04, reset the {\tt footnote} counter each trial, and make
% "\footnotetext" gobble its arguments. Also locally clear "\TX@vwarn"
% so that the warning is generated by the {\tt final} run, and does not
% appear in the middle of the table if "\tracingtabularx".
%    \begin{macrocode}
  \let\@footnotetext\TX@trial@ftn
  \global\csname c@\@mpfn\endcsname\count@
  \let\TX@vwarn\@empty
%    \end{macrocode}
% Do not nest {\tt tabularx} environments during trial runs. This would
% waste time, and the global setting of "\TX@cols" would break the
% algorithm.
%    \begin{macrocode}
   \expandafter\let\expandafter\tabularx\csname tabular*\endcsname
   \expandafter\let\expandafter\endtabularx\csname endtabular*\endcsname
%    \end{macrocode}
% Turn off warnings (see appendix D). Also prevent them being turned
% back on by setting the parameter names to be registers.
%    \begin{macrocode}
    \hbadness=\@M\hfuzz=\maxdimen
    \let\hbadness=\@tempcnta\let\hfuzz=\@tempdima
%    \end{macrocode}
% Make the table, and finish the hbox.
%    \begin{macrocode}
    \expandafter\tabular\expandafter{\the\@temptokena}%
      \the\toks@
    \endtabular}%
%    \end{macrocode}
% Print some statistics.
%    \begin{macrocode}
  \TX@typeout{\@spaces
     \the\wd\@tempboxa\@spaces\the\TX@col@width\@spaces\the\TX@cols}}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\arraybackslash}
% "\\" hack.
%    \begin{macrocode}
\def\arraybackslash{\let\\=\@arraycr}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\tracingtabularx}
% Print statistics on column and table widths.
%    \begin{macrocode}
\def\tracingtabularx{\let\TX@typeout\typeout}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@typeout}
% The default is to be to be quiet
%    \begin{macrocode}
\let\TX@typeout\@gobble
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@ftn}
% A token register for saving footnote texts.
%    \begin{macrocode}
\newtoks\TX@ftn
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@ftntext}
% \begin{macro}{\TX@xftntext}
% Inside the alignment just save up the footnote text in a token
% register.
%    \begin{macrocode}
\long\def\TX@ftntext#1{%
  \edef\@tempa{\the\TX@ftn\noexpand\footnotetext
                    [\the\csname c@\@mpfn\endcsname]}%
  \global\TX@ftn\expandafter{\@tempa{#1}}}%
\long\def\TX@xftntext[#1]#2{%
  \global\TX@ftn\expandafter{\the\TX@ftn\footnotetext[#1]{#2}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\TX@trial@ftn}
% On trial runs, gobble footnote texts.
%    \begin{macrocode}
\long\def\TX@trial@ftn#1{}
%    \end{macrocode}
% \end{macro}
%
% This last section was added at Version 1.02. Previous versions
% documentented the fact that "\verb" did not work inside {\tt
% tabularx}, but that did not stop people using it! This usually put
% \LaTeX\ into an irrecoverable error position, with error messages that
% did not mention the cause of the error. The `poor man's "\verb"' (and
% "\verb*") defined here is based on page 382 of the \TeX{}Book. As
% explained there, doing verbatim this way means that spaces are not
% treated correctly, and so "\verb*" may well be useless, however I
% consider this section of code to be error-recovery, rather than a real
% implementation of verbatim.
%
% The mechanism is quite general, and any macro which wants to allow a
% form of "\verb" to be used within its argument may
% "\let\verb=\TX@verb". (Making sure to restore the real definition
% later!)
%
% "\verb" and "\verb*" are subject to the following restictions:
% \begin{enumerate}
% \item Spaces in the argument are not read verbatim, but may be skipped
%       according to \TeX's usual rules.
% \item Spaces will be added to the output after control words, even if
%       they were not present in the input.
% \item Unless the argument is a single space, any trailing space,
%       whether in the original argument, or added as in (2),
%       will be omitted.
% \item The argument must not end with "\", so "\verb|\|" is not
%      allowed, however, because of (3), "\verb|\ |" produces
%      "\".
% \item The argument must be balanced with respect to "{" and "}". So
%      "\verb|{|" is not allowed.
% \item A comment character like "%" will not appear verbatim. It will
%       act as usual, commenting out the rest of the input line!
% \item The combinations "?`" and "!`" will appear as
%       {\tt?`} and {\tt!`} if the {\tt cmtt} font is being used.
% \end{enumerate}
%
% \begin{macro}{\TX@verb}
% The internal definition of "\verb". Spaces will be replaced by "~", so
% for the star-form, "\let" "~" be \verb*| |, which we obtain as
% "\uppercase{*}". Use "{\ifnum0=`}\fi" rather than "\bgroup" to allow
% "&" to appear in the argument.
%    \begin{macrocode}
{\uccode`\*=`\ %
\uppercase{\gdef\TX@verb{%
  \leavevmode\null\TX@vwarn
  {\ifnum0=`}\fi\tt\let\\\ignorespaces
  \@ifstar{\let~*\TX@vb}{\TX@vb}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@vb}
% Get the `almost verbatim' text using "\meaning". The `"!"' is added to
% the front of the user supplied text, to ensure that the whole argument
% does not consist of a single "{ }" group. \TeX\ would strip the outer
% braces from such a group. The `"!"' will be removed later.
%
% Originally I followed Knuth, and had "\def\@tempa{##1}", however this
% did not allow "#" to appear in the argument. So in v1.04, I changed
% this to to use a token register, and "\edef". This allows "#" appear,
% but makes each one appear twice!, so later we loop through, replacing
% "##" by "#".
%    \begin{macrocode}
\def\TX@vb#1{\def\@tempa##1#1{\toks@{##1}\edef\@tempa{\the\toks@}%
    \expandafter\TX@v\meaning\@tempa\\ \\\ifnum0=`{\fi}}\@tempa!}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@v}
% Strip the initial segment of the "\meaning", including the `"!"'
% added earlier.
%    \begin{macrocode}
\def\TX@v#1!{\afterassignment\TX@vfirst\let\@tempa= }
%    \end{macrocode}
% \end{macro}
%
% As explained above we are going to replace "##" pairs by "#". To do
% this we need non-special "#" tokens.  Make "*" into a parameter
% token so that we can define macros with arguments. The normal meanings
% will be restored by the "\endgroup" later.
%    \begin{macrocode}
\begingroup
\catcode`\*=\catcode`\#
\catcode`\#=12
%    \end{macrocode}
%
% \begin{macro}{\TX@vfirst}
% As a special case, prevent the first character from being dropped.
% This makes "\verb*| |" produce \verb*| |. Then call "\TX@v@".
% This is slightly tricky since v1.04, as I have to ensure that an
% actual "#" rather than a command "\let" to "#" is passed on if the
% first character is "#".
%    \begin{macrocode}
\gdef\TX@vfirst{%
  \if\@tempa#%
    \def\@tempb{\TX@v@#}%
  \else
    \let\@tempb\TX@v@
    \if\@tempa\space~\else\@tempa\fi
  \fi
  \@tempb}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@v@}
% Loop through the "\meaning", replacing all spaces by "~". If the last
% charcter is a space it is dropped, so that "\verb*|\LaTeX|" produces
% "\LaTeX" not \verb*|\LaTeX |. The rewritten tokens are then further
% processed to replace "##" pairs.
%    \begin{macrocode}
\gdef\TX@v@*1 *2{%
  \TX@v@hash*1##\relax\if*2\\\else~\expandafter\TX@v@\fi*2}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\TX@v@hash}
% The inner loop, replacing "##" by "#".
%    \begin{macrocode}
\gdef\TX@v@hash*1##*2{*1\ifx*2\relax\else#\expandafter\TX@v@hash\fi*2}
%    \end{macrocode}
% \end{macro}
%
% As promised, we now restore the normal meanings of "#" and "*".
%    \begin{macrocode}
\endgroup
%    \end{macrocode}
%
% \begin{macro}{\TX@vwarn}
% Warn the user the first time this "\verb" is used.
%    \begin{macrocode}
\def\TX@vwarn{%
  \@warning{\noexpand\verb may be unreliable inside tabularx}%
  \global\let\TX@vwarn\@empty}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
%</style>
%    \end{macrocode}
%
% \section{The documentation driver file}
%
% The next bit of code contains the documentation driver file for
% \TeX{}, i.e., the file that will produce the documentation you are
% currently reading. It will be extracted from this file by the {\tt
% docstrip} program.
%    \begin{macrocode}
%<+driver>\documentstyle[tabularx,doc]{article}
%<+driver>
%<+driver>% dimensions from ltugboat.sty:
%<+driver>
%<+driver>\setlength\textwidth{31pc} \setlength\textheight{54pc}
%<+driver>\parindent 0pt \parskip 2pt plus 1pt minus 1pt
%<+driver>\setlength{\oddsidemargin}{8pc}
%<+driver>\setlength{\marginparwidth}{8pc}
%<+driver>\setlength{\topmargin}{-2.5pc}
%<+driver>\setlength{\headsep}{20pt}
%<+driver>\setlength{\columnsep}{1.5pc}
%<+driver>\setlength{\columnwidth}{18.75pc}
%<+driver>
%<+driver>\MakeShortVerb{\"}  % "\foo" acts like \verb+\foo+
%<+driver>
%<+driver>\DisableCrossrefs
%<+driver>
%<+driver>\CodelineIndex                  % Index code by line number
%<+driver>
%<+driver>%\OnlyDescription    % comment out for implementation details
%<+driver>
%<+driver>\setcounter{StandardModuleDepth}{1}
%<+driver>
%<+driver>\begin{document}
%<+driver>   \DocInput{tabularx.doc}
%<+driver>\end{document}
%    \end{macrocode}
%
%
% \Finale
\endinput
