% \iffalse meta-comment
%
% Copyright (C) 2005 by Wybo Dekker <wybo@dekkerdocumenten.nl>
% -------------------------------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.2
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{ctable.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{ctable}
%<*package>
    [2011/08/24 v1.20 ctable package]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{ctable,txfonts,paralist,desclist}
\usepackage[l2tabu,orthodox]{nag}
\usepackage[
   verbose,
   pdftex,
   paper=a4paper,
   margin=20mm,
   bottom=30mm,
   left=50mm,
   nohead
]{geometry}
\usepackage{hyperref}
\graphicspath{{./doc/}}
\definecolor{vbgreen}{rgb}{0,.6,0}
\definecolor{B}{rgb}{0.77, 0.90, 0.96}
\definecolor{Y}{rgb}{1,1,.878}
\def\URL#1{http://www.ctan.org/tex-archive/help/Catalogue/entries/#1.html}
\def\REF#1{\href{\URL{#1}}{\texttt{#1}}}
\parindent0pt\parskip1ex
\newcommand{\ROW}[3]{\parskip0pt%
  \colorbox{#1}{%
    \parbox{.98\hsize}{%
      \rule{0pt}{1.5ex}\\%%
      \parbox{.5\hsize}{\includegraphics{#2}}%%
      \parbox{.5\hsize}{\centering{\includegraphics{#3}}}%%
      \\\rule{0pt}{1.5ex}%%
    }%%
  }\par%%
}
\AtBeginDocument{\RecordChanges}
\AtEndDocument{\PrintChanges}
\EnableCrossrefs
\hypersetup{
   colorlinks = true,
     pdftitle = The ctable package,
    pdfauthor = Wybo Dekker,
   pdfsubject = {Typesetting centered, right, and left aligned table and figure
                 floats, with many configuration options},
  pdfkeywords = {
                 table floats,
                 figure floats,
                 key=value options,
                 footnotes,
                 booktabs,
                 tabularx,
                 threeparttable
                },
  bookmarksopen
}
\begin{document}
\DocInput{ctable.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{567}
%
% \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         \~}
%
%
% \changes{v1.00}{2000/06/01}{First release.}
% \changes{v1.01}{2001/03/17}{Making use of booktabs package}
% \changes{v1.02}{2002/06/24}{Using keyval to reduce args to 4}
% \changes{v1.03}{2002/07/16}{Many syntactic corrections,
%                            thanks to Johannes Braams}
% \changes{v1.04}{2003/08/11}{Caption, if empty, will not be typeset
%                            rotate option added
%                            star option added to use table* and figure*
%                            environments}
% \changes{v1.06}{2004/03/20}{left, right and center options added
%                            frame{sep,rule,fg,bg} options added
%                            error in width-setting corrected}
% \changes{v1.05}{2003/10/03}{maxwidth option added}
% \changes{v1.06a}{2004/04/01}{two errors corrected:
%                            made setting fboxsep and fboxrule only temporary
%                            removed superfluous space after tabulars}
% \changes{v1.06b}{2004/06/19}{Added several % at eol to remove superfluous
%                             whitespace occurring sometimes}
% \changes{v1.07}{2005/08/09}{Added option sideways, option rotate now obsolete;
%                            added option captionskip}
% \changes{v1.08}{2006/04/10}{Standardized file setup following
%                            http://www.ctan.org/tex-archive/info/dtxtut/dtxtut.pdf
%                            mincapwidth option added
%                            Moved newdimen definition outside ctable macro
%                           }
% \changes{v1.09}{2007/03/04}{Added option nosuper;
%                            corrected incorrect positioning when table is wider than
%                            mincapwidth}
% \changes{v1.10}{2007/08/17}{Footnote markers now stay superscript with nosuper.
%                             Documentation: added many examples for the options.
%                             Corrected some unwanted white space in captions.
%                             Caption package included to correct booktabs
%                             errors in caption position. And for later use of
%                             its facilities. *Captionskip option redefined*: 0pt
%                             value now corresponds to LaTeX defaults
%                            }
% \changes{v1.11}{2007/09/07}{Added some percent signs at EOL to prevent
%                             whitespace, 
%                             Removed xspace usage - caused overfull badness
%                            }
% \changes{v1.12}{2008/04/12}{Option notespar added}
% \changes{v1.13}{2008/05/01}{cap option with empty argument will not be inserted
%                             in lot/lof
%                             Added option continued, for continuation tables:
%                             same number as previous table, ` (continued' added
%                             to caption.
%                            }
% \changes{v1.14}{2009/09/15}{nosuper propagation to later tables prohibited
%                             added option doinside
%                             use of (obsolete) carom.sty for docs discontinued
%                             empty labels not created
%                             newcolumntype warnings removed
%                             caption package not needed anymore
%                            }
% \changes{v1.15}{2009/09/17}{removed whitespace before tables,
%                             corrected marginpars in the documentation
%                            }
% \changes{v1.16}{2010/06/26}{option cap={} did not suppress lot/lof entry
%                             notespar option now generates fully justified notes
%                            }
% \changes{v1.17}{2010/10/30}{option cap={} did not suppress lot/lof entry
%                             notespar option now generates fully justified notes
%                            }
% \changes{v1.18}{2011/04/15}{added setupctable for option defaults
%                             added complement for several options (topcap, nosideways, et cetera
%                            }
% \changes{v1.19}{2011/05/01}{sideways option did not work anymore; corrected
%                            }
% \changes{v1.20}{2011/08/24}{added options captionsleft, captionsright, captionsinside;
%                             (for setupctable only)
%                            }
%
% \GetFileInfo{ctable.dtx}
%
% \DoNotIndex{     \newcommand,   \\,             \,,
% \def,            \definecolor,  \hbox,          \raggedright,
% \else,           \ifdim,        \sbox,          \empty,
% \ifx,            \setkeys,      \end,           \label,
% \specialrule,    \fboxrule,     \let,           \tabularnewline,
% \PackageError,   \fboxsep,      \newbox,        \textit,
% \PackageWarning, \fcolorbox,    \newcolumntype, \usebox,
% \RequirePackage, \fi,           \newdimen,      \vspace,
% \begin,          \footnotesize, \normalfont,    \wd,
% \define@key,
% }
%
% \title{The \textsf{ctable} package\thanks{This document
%   corresponds to \textsf{ctable}~\fileversion, dated \filedate.}}
% \author{Wybo Dekker \\ \texttt{wybo@dekkerdocumenten.nl}}
%
% \maketitle
% \begin{abstract}\noindent
% The \texttt{ctable} package provides a \texttt{ctable}
% command for the typesetting of table and figure floats. You will
% not need to type the usual nested begin...end sequences, as
% \texttt{ctable} is a command, not an environment. \texttt{ctable}
% has only 4 arguments, but the optional first one may hold many
% \textsl{key=value} pairs and makes \texttt{ctable} very flexible
% and extensible. It uses Simon Fear's \REF{booktabs} package for
% better vertical spacing around horizontal rules and it provides
% facilities for making table footnotes.
% \end{abstract}
%
% \section{Purpose} The |ctable| package lets you easily typeset
% captioned table and figure floats with optional
% footnotes. Both caption and footnotes will normally be forced within the
% width of the table.
% If the width of the table is specified, then \REF{tabularx} will be used
% to typeset it, and one or more |X| column specifiers should be specified.
% Otherwise tabular will be used.
%
% This package defines the commands |\ctable|, |\tnote| and
% |\tmark|, and four |\tabularnewline| generating
% commands. The latter generate reasonable amounts of whitespace
% around horizontal rules and are also useful for tabulars
% outside this package.
%
% Since the |ctable| package imports the \REF{array} and \REF{booktabs}
% packages, all commands from those packages are available as well.
%
% Note that, in line with the comments that Simon Fear made
% describing his \REF{booktabs} package, vertical rules for column
% separation can be produced with |\ctable|, but no provisions are
% made to have them make contact with horizontal rules.
%
% \section{Usage}
% \DescribeMacro{\setupctable}
% |\ctable| defaults can be set, either in the preamble or in the body, 
% with:\\
%
% |\setupctable{options}  % key=value,...|\\
%
% \DescribeMacro{\ctable}
% |\ctable| is called with 4 parameters, of which the first is optional: \\
%
% |\ctable[options]       % key=value,...|\\
% |       {coldefs}       % for \begin{tabular}|\\
% |       {foottable}     % zero or more \tnote commands (see below)|\\
% |       {table rows}    % rows for the table|\\
%
% Options are given as key=value pairs, separated by comma's.
% Extra comma's, including one behind the last pair, don't hurt.
% Arguments to option should be put between braces if they contain
% comma's or equals signs.
% \section{Options}
% Currently the following option keys have been defined:
% 
% { \marginparwidth25mm
% \DescribeMacro{botcap\hfill}
% put the caption at the bottom of the float instead of on top of it.
% See also: |topcap|.
%
% \DescribeMacro{caption=...\hfill}
% table caption; the braces are needed only if your caption contains a
% comma or an equals sign.
%
% \DescribeMacro{cap=...\hfill}
% for a short caption to go to the |\listoftables|. Without the |cap|
% option, the full caption will go into the |\listoftables|. If |cap| is
% given an empty value, no entry in the |\listoftables| will be made. This
% may be useful, for example, with the |continued| option.
%
% \DescribeMacro{captionskip=...\hfill}
% moves the caption relative to the table; the default is |0ex|, which puts
% captions at their default \LaTeX\ positions: a top caption's baseline at
% |1ex| above the top rule position of the table and a bottom caption's
% baseline at |4ex| below the bottom rule position.
%
% \DescribeMacro{captionsleft\hfill} This option is defined for
% |\setupctable| only, and it is effective only where the |sideways| option
% is used. After |\setupctable{captionsleft}| all tables typeset with the
% |sideways| option will have their captions at the left.
%
% \DescribeMacro{captionsright\hfill} This option is defined for
% |\setupctable| only, and it is effective only where the |sideways| option
% is used. After |\setupctable{captionsright}| all tables typeset with the
% |sideways| option will have their captions at the right.
%
% \DescribeMacro{captionsinside\hfill}
% This option is defined for |\setupctable| only, it is the default, and it is effective
% only where the |sideways| option is used. After
% |\setupctable{captionsinside}| all tables typeset with the |sideways|
% option will have their captions at the left in one-sided documents. In
% twosided documents, captions will be on the left for odd-numbered pages
% and on the right for even-numbered pages.
%
% \DescribeMacro{center\hfill}
% center the table in the available text width; this is the default.
% See also: |left|, |right|.
%
% \DescribeMacro{continued=...\hfill}
% if used, the table will be numbered the same as the previous table. If
% used without an argument, the caption will be suffixed with `
% (continued)', if used with an argument, the suffix will be the argument.
%
% \DescribeMacro{doinside=...\hfill}
% command to be run inside, just before the tabular or tabularx
% environment. You can use this, for example, for the adjustment of the
% font size with |\small|.
%
% \DescribeMacro{figure\hfill}
% produce a figure float instead of a table float. See also: |table|.
%
% \DescribeMacro{framebg={\slshape r g b}\hfill}
% set the background color of the frame (the color inside the frame) to the
% given triplet of \textsl{rgb}-values. The values should be numbers
% between 0 and 1. The default is |1 1 1| (white).
%
%
% \DescribeMacro{framefg={\slshape r g b}\hfill}
% set the foreground color of the frame (the rule color) to the given
% triplet of \textsl{rgb}-values. The values should be numbers between 0
% and 1. The default is |0 0 0| (black).
%
%
% \DescribeMacro{framerule=...\hfill}
% draw a frame around the table with the given rule thickness. The default
% is |0pt|, so that no frame will be seen.
%
%
% \DescribeMacro{framesep=...\hfill}
% set the distance between the frame and the table to the given dimension.
% The default is |0pt|.
%
%
% \DescribeMacro{label=...\hfill}
% labels the float with |\label|.
%
% \DescribeMacro{left\hfill}
% left align the table in the available text width. See also: |center|, |right|.
%
% \DescribeMacro{maxwidth=...\hfill}
% like the \textsl{width} option, but any |X| column specifiers will be
% replaced with |l| if the resulting table width would thus stay within the
% specified maximum width. This is especially useful where the \LaTeX\
% source is generated by a script.
%
% \DescribeMacro{mincapwidth=...\hfill}
% sets the minimum width of the float. Without this option, the width is
% set to that of the tabular, and the caption and footnotes are typeset
% within that width. This may be a problem with very narrow tables;
% |mincapwidth| can then be used to give the float a minimum width. The
% tabular will be centered in it.
%
% \DescribeMacro{nonotespar\hfill}
% typeset footnotes in a table; this is the default. See also: |notespar|.
%
% \DescribeMacro{nosideways\hfill}
% undo the sideways option. See also: |sideways|.
%
% \DescribeMacro{nostar\hfill}
% use the un-starred versions of the |table| and |figure| environments;
% this is the default
%
% \DescribeMacro{nosuper\hfill}
% in the footnote table, typeset footnote markers on the line, instead of
% superscripted.
%
% \DescribeMacro{notespar\hfill}
% typeset footnotes in a paragraph instead of in a table.
%
% \DescribeMacro{pos=...\hfill}
% float position, default: |tbp|.
%
% \DescribeMacro{right\hfill}
% right align the table in the available text width.
%
% \DescribeMacro{sideways\hfill}
% rotate table or figure by 90 degrees anticlockwise and put it on a
% separate page. With the twoside option for the standard \LaTeX\ document
% classes, rotation will be -90 on even pages. If you use this option, the
% |pos| option is not allowed. See also: |nosideways|.
%
% \DescribeMacro{star\hfill}
% use the starred versions of the |table| and |figure| environments, which
% place the float over two columns when the |twocolumn| option or the
% |\twocolumn| command is active. See also: |nostar|.
%
% \DescribeMacro{super\hfill}
% in the footnote table, typeset footnote markers as superscripts; this is
% the default. See also: |nosuper|.
%
% \DescribeMacro{table\hfill}
% produce a table float (this is the default). See also: |figure|.
%
% \DescribeMacro{topcap\hfill}
% put the caption top of the float; this is the default. See also: |botcap|.
%
% \DescribeMacro{width=...\hfill}
% \REF{tabularx} will be used to typeset the table at the specified
% width\,---\,one or more |X| column specifiers must be provided.
%
% }
%
% \section{The width and maxwidth options}
% When \LaTeX-sources containing tables are generated automatically by a
% script, it is often not known in advance what the maximum size of an
% |l|~column will be. A good solution for this is to use an |X|~specifier,
% typesetting the table at the text width with the \REF{tabularx} package.
% However, this will result in too much white space in cases where the
% column contains small texts only. This problem can be solved by using
% the maxwidth option instead of the width option. The |X|~specifiers will
% then be replaced with |l| as long as the width of the resulting table
% stays with the specified maximum width.
%
% \section{Tables wider than the text width}
% When you make a table wider than |\textwidth|, it will extend in the right margin. 
% If it is a large table, occupying a whole page, you can use the geometry package
% and surround your ctable call with |\newgeometry{width=...,margin=...}| and |\restoregeometry|.
% However, both geometry commands imply |\clearpage|, so your table will appear on an otherwise 
% empty page. 
%
% Alternatively, you can center the table on the paper, extending in both margins, by using the 
% option |doinside=\hspace*{<dimen>}| with an appropriate negative |dimen>|.
%
% \section{Setting option defaults: setupctable}
% Every call of |\ctable| resets the options to their defaults before evaluating the
% first (optional) argument. So if you make two ctables: |\ctable[left,...| and
% |\ctable[...|, the first will be left-aligned on the page, but the second, lacking
% the |left| option, will be centered, because that is the default. If you want all
% your tables left-aligned, it's more practical to change the default by calling
% \DescribeMacro{\setupctable}
% |\setupctable{left}|, either in the preamble or somewhere in the body. In latter
% case only tables following the call will have their defaults changed. \par
% |\setupctable| can set the defaults for all options except (of course) |caption|,
% |cap|, and |label|. Actually, the initial option defaults are set by
% calling |\setupctable| as follows:
% \begin{verbatim}
% \setupctable{
%   captionskip=0pt,        framerule=0pt,    nostar,
%   center,                 framesep=0pt,     pos=tbp,
%   continued=(continued),  maxwidth=0pt,     super,
%   doinside={},            mincapwidth=0pt,  table,
%   framebg=1 1 1,          nonotespar,       topcap,
%   framefg=0 0 0,          nosideways,       width=0pt
% }
% \end{verbatim}
% \section{Other commands}
% \DescribeMacro{\tnote}
% |\tnote[label]{footnote text}| places {\footnotesize
% \textsuperscript{\normalfont\textit{label}}\,footnote text}
% under the table.
% This command can only be used in |\ctable|'s third argument, i.e.\ the foottable
% argument described above. The
% label is optional, the default label is a single $a$. For more
% detailed control, you can also replace this command with something
% like |labeltext&footnotetext\NN|.
% The footnotes are placed under the table, without a rule.
% You therefore probably will want to use the |\LL| (last line)
% command if you use footnotes.
% \par
% \DescribeMacro{\tmark}
% |\tmark[label]| this command places the superscripted label in the
% table. It is equivalent with |$^{label}$|.
% The label is optional, the default label is a single $a$.
% \par
% The newline generating commands are a combination of
% |\tabularnewline| and zero or one of \REF{booktabs} |\toprule|,
% |\midrule| or |\bottomrule|. These combinations have been made, and
% short names have been defined, because source texts for complex
% tables often become very crowded:
% \par
% \DescribeMacro{\NN} Normal Newline, generates just a normal new line.
% An optional dimen parameter inserts extra vertical space under the
% line. Is an alias for |\tabularnewline|
% \par
% \DescribeMacro{\FL} First Line, generates a new line and a thick
% rule with some extra space under it.
% An optional dimen parameter sets the line width; the default is 0.08em.
% Is an alias for |\toprule|
% \par
% \DescribeMacro{\ML} Middle Line: generates a new line and a thin
% rule with some extra space over and under it.
% An optional dimen parameter sets the line width; the default is 0.05em.
% Is an alias for |\tabularnewline\midrule|
% \par
% \DescribeMacro{\LL} Last Line:  generates a new line and a thick
% rule with some extra space over it.
% An optional dimen parameter sets the line width; the default is 0.08em.
% Is an alias for |\tabularnewline\bottomrule|
% \par
% These macros can be used outside |\ctable| constructs.
% \par
% Finally, for completeness, here are some of \REF{booktabs}' commands
% that may be useful:
% \par
% \DescribeMacro{\toprule} |\toprule[<wd>]|
% where |<wd>| is the optional thinkness of the rule.
% \par
% \DescribeMacro{\midrule} |\midrule[<wd>]|.
% \par
% \DescribeMacro{\bottomrule} |\bottomrule[<wd>]|.
% \par
% \DescribeMacro{\cmidrule} |\cmidrule[<wd>](<trim>){a-b}|
% where |<trim>| can be |r|, |l|, or |rl|
% and the rule is drawn over columns |a| through |b|.
% \par
% \DescribeMacro{\morecmidrules} |\morecmidrules|
% must be used to separate two successive cmidrules.
% \par
% \DescribeMacro{\addlinespace} |\addlinespace[<wd>]|
% inserts extra space between rows.
% \par
% \DescribeMacro{\specialrule}
% |\specialrule{<wd>}{<abovespace>}{<belowspace>}|.
% \par
% See the \REF{booktabs} documentation for details.
% \newpage
% \section{Examples}
% Table~\ref{nowidth} is an example taken from the related package
% \REF{threeparttable} by Donald Arseneau, with an extra footnote.
% It was typeset with:
% \color{vbgreen}
% \begin{verbatim}
% \ctable[
%    cap     = The Skewing Angles,
%    caption = The Skewing Angles ($\beta$) for
%              $\fam0 Mu(H)+X_2$ and $\fam0 Mu(H)+HX$~\tmark,
%    label   = nowidth,
%    pos     = h
% ]{rlcc}{
%    \tnote{for the abstraction reaction,
%           $\fam0 Mu+HX \rightarrow MuH+X$.}
%    \tnote[b]{1 degree${} = \pi/180$ radians.}
%    \tnote[c]{this is a particularly long note, showing that
%              footnotes are set in raggedright mode as we don't like
%              hyphenation in table footnotes.}
% }{                                                          \FL
%   &            & $\fam0 H(Mu)+F_2$     & $\fam0 H(Mu)+Cl_2$ \ML
%   &$\beta$(H)  & $80.9^\circ$\tmark[b] & $83.2^\circ$       \NN
%   &$\beta$(Mu) & $86.7^\circ$          & $87.7^\circ$       \LL
% }
% \end{verbatim}
% \color{black}
% \ctable[
%    cap     = The Skewing Angles,
%    caption = The Skewing Angles ($\beta$) for
%              $\fam0 Mu(H)+X_2$ and $\fam0 Mu(H)+HX$~\tmark,
%    label   = nowidth,
%    pos     = h
% ]{rlcc}{
%    \tnote{for the abstraction reaction,
%           $\fam0 Mu+HX \rightarrow MuH+X$.}
%    \tnote[b]{1 degree${} = \pi/180$ radians.}
%    \tnote[c]{this is a particularly long note, showing that
%              footnotes are set in raggedright mode as we don't like
%              hyphenation in table footnotes.}
% }{                                                          \FL
%   &            & $\fam0 H(Mu)+F_2$     & $\fam0 H(Mu)+Cl_2$ \ML
%   &$\beta$(H)  & $80.9^\circ$\tmark[b] & $83.2^\circ$       \NN
%   &$\beta$(Mu) & $86.7^\circ$          & $87.7^\circ$       \LL
% }
% Table~\ref{width} is an example with a width specification,
% taken from the \REF{tabularx} documentation, with the vertical rules
% removed. 
% By using the trimming parameters of the \REF{booktabs} |\cmidrule|
% command, some of the horizontal splitting was regained.
% The |left| option left aligns the table. It was typeset with:
% \color{vbgreen}
% \begin{verbatim}
% \ctable[
%    caption = Example with a specified width of 100mm,
%    label   = width,
%    width   = 100mm,
%    pos     = ht,
%    left
% ]{c>{\raggedright}Xc>{\raggedright}X}{
%    \tnote{footnotes are placed under the table}
% }{                                                         \FL
%    \multicolumn{4}{c}{Example using tabularx}              \ML
%    \multicolumn{2}{c}{Multicolumn entry!} & THREE & FOUR   \NN
%        \cmidrule(r){1-2}\cmidrule(rl){3-3}\cmidrule(l){4-4}
%    one&
%    The width of this column depends on the width of the table.\tmark &
%    three&
%    Column four will act in the same way as column two, with the same width.                        \LL
% }
% \end{verbatim}
% \color{black}
% \ctable[
%    caption = Example with a specified width of 100mm,
%    label   = width,
%    width   = 100mm,
%    pos     = ht,
%    left
% ]{c>{\raggedright}Xc>{\raggedright}X}{
%    \tnote{footnotes are placed under the table}
% }{                                                         \FL
%    \multicolumn{4}{c}{Example using tabularx}              \ML
%    \multicolumn{2}{c}{Multicolumn entry!} & THREE & FOUR   \NN
%        \cmidrule(r){1-2}\cmidrule(rl){3-3}\cmidrule(l){4-4}
%    one&
%    The width of this column depends on the width of the
%        table.\tmark &
%    three&
%    Column four will act in the same way as
%    column two, with the same width.                        \LL
% }
%
% Figures, even single ones, are always put in tabular cells. This is not
% particularly handy for single pictures, but it eases the construction of
% arrays of pictures, including sub-captions, delineation, and spacing. For a
% small example, which also shows how you can simplify the construction of
% figure arrays, see subsection~\ref{figureexample} on
% page~\pageref{figureexample}.
%
% \section{Option examples}
% In the following, small examples will be shown illustrating the effect of
% options. In the left column the relevant part of the source is shown, in the
% right column you see the result. In most cases you see a standard example on a
% light yellow background, followed by one or more variations on a light blue
% background. Where necessary, the example will show boxes to indicate the page
% and the text body.
%
% \subsection{\ttfamily\bfseries center, left, right}
% These options align the float in the page; the default is |center|:
% \medskip\\
% \ROW{Y}{s02k}{02k}
% \ROW{B}{s02l}{02l}
% \ROW{B}{s02m}{02m}
% 
% \subsection{\ttfamily\bfseries super, nosuper}
% Footnote markers in |ctable| are typeset superscripted by default. Use the
% |nosuper| option to place them on the base line:
% \medskip\\
% \ROW{Y}{s07a}{07a}
% \ROW{B}{s07b}{07b}
% 
% \subsection{\ttfamily\bfseries notespar, nonotespar}
% By default, footnotes in |ctable| are typeset in a table, one line per note. 
% This corresponds with the |nonotespar| option.You can also typeset them in a paragraph,
% one after the other, by using the |notespar| option: 
% \medskip\\
% \ROW{Y}{s12a}{12a}
% \ROW{B}{s12b}{12b}
% 
% \subsection{\ttfamily\bfseries continued}
% The |continued| option suffixes the caption with ` (continued)', and lowers the table
% number by one, so that it obtains the same number as the previous table.
% This option can be given an argument to replace the default suffix:
% \medskip\\
% \ROW{Y}{s13a}{13a}
% \ROW{B}{s13b}{13b}
% \ROW{B}{s13c}{13c}
% 
% \subsection{\ttfamily\bfseries mincapwidth}
% |ctable| forces caption and footnotes to stay within the width of the table.
% Sometimes, however, tables are so narrow, that this is not really what you want.
% In such cases, use the |mincapwidth| option to give caption and footnotes some
% extra room:
% \medskip\\
% \ROW{Y}{s05a}{05a}
% \ROW{B}{s05b}{05b}
% \medskip
% You can set |mincapwidth| to a large value, say |\hsize|, if you want a one-line
% caption. Note, however, that this may influence the horizontal positioning of
% the table: values larger than |\hsize| will move a centered table out of the
% center, a value of |\hsize| will prevent the |left| and |right| options to do
% their work, because the table is already captured between the left and right
% margins.
% 
% \subsection{\ttfamily\bfseries maxwidth}
% When \LaTeX-sources containing tables are generated automatically by a
% script, it is often not known in advance what the maximum size of an |l|
% column will be. A good solution for this is to use an |X| specifier,
% typesetting the table at the text width with the \REF{tabularx} package.
% However, this will result in too much white space in cases where the
% column contains small texts only. This problem can be solved by using
% the maxwidth option instead of the width option. The |X| specifiers will
% then be replaced with |l| as long as the width of the resulting table
% stays with the specified maximum width.
% \medskip\\
% \ROW{Y}{s06a}{06a}
% \ROW{B}{s06b}{06b}
% 
% \subsection{\ttfamily\bfseries framerule}
% The following examples show the use of frames and backgrounds. Every table
% is typeset by |ctable| with a frame around it, but the frame is, by default,
% drawn with a zero width line, and is therefore invisible. You can make it
% visible by either changing the linewidth to a positive value or by giving it a
% background color, which will be used to fill the frame.
% 
% Here is a simple table without a frame, followed by one with a red, |1pt| thick
% frame:
% \medskip\\
% \ROW{Y}{s08a}{08a}
% \ROW{B}{s08b}{08b}
% \medskip
% As you see, the frame fits closely to the first (|\FL|) and last (|\LL|) table
% lines. This can be a reason to either remove those lines, or to introduce some
% whitespace between the frame and the table with the |framesep| option:
% \medskip\\
% \ROW{B}{s09b}{09b}
% \medskip
% And finally, we could also frame the table by giving it a, say, yellow backgound
% instead of a red frame line, or even do both:
% \medskip\\
% \ROW{Y}{s10a}{10a}
% \ROW{B}{s10b}{10b}
% 
% \newpage
% \subsection{\ttfamily\bfseries captionskip}
% The distance between a top caption and the table is \texttt{2ex},
% but it can be varied with \texttt{captionskip}:
% \medskip\\
% \ROW{Y}{s03a}{03a}
% \ROW{B}{s03b}{03b}
% \medskip
% This works for bottom caption, too:
% \medskip\\
% \ROW{Y}{s04a}{04a}
% \ROW{B}{s04b}{04b}
% 
% \subsection{\ttfamily\bfseries figure, botcap}\label{figureexample}
% By default, |ctable| generates a table float, but with the |figure| option, a
% figure float is generated instead. The caption stays on top, so if you are
% accustomed to have bottom caption for your figures, you will probably also need
% the |botcap| option:
% \medskip\\
% \ROW{Y}{s01a}{01a}
% \ROW{B}{s01b}{01b}
% 
% \newpage
% \subsection{\ttfamily\bfseries doinside}
% The argument of doinside is supposed to be a command to be run inside, just before the tabular or tabularx
% environment. You can use this, for example, for the adjustment of the
% font size with |\small|:
% \medskip\\
% \ROW{Y}{s14a}{14a}
%
% \subsection{\ttfamily\bfseries sideways}
% The |sideways| option creates a landscape table with its head pointing at the
% spine\,---\,when the documentclass' twoside option has been used, that is. The
% following examples show the effect of the |sideways| option, first on page one,
% then on page 2. Note that the |caption| option has not been used, so no caption
% appears:
% \medskip\\
% \ROW{Y}{s11k}{11k}
% \ROW{B}{s11l}{11l}
% \ROW{B}{s11m}{11m}
% 
% \newpage
% \StopEventually{}
%
% \section{Implementation}
% \begin{macrocode}
\RequirePackage{color,xkeyval,array,tabularx,booktabs,rotating}
\def\NN{\tabularnewline}
\def\FL{\toprule}
\def\ML{\NN\midrule}
\def\LL{\NN\bottomrule}
\def\@defaultctblfgcolor#1 #2 #3={\definecolor{@defaultctblframefg}{rgb}{#1,#2,#3}}
\def\@defaultctblbgcolor#1 #2 #3={\definecolor{@defaultctblframebg}{rgb}{#1,#2,#3}}
\def\@ctblfgcolor#1 #2 #3={%
  \definecolor{@ctblframefg}{rgb}{#1,#2,#3}
  \def\@ctblfgactual{@ctblframefg}}
\def\@ctblbgcolor#1 #2 #3={%
  \definecolor{@ctblframebg}{rgb}{#1,#2,#3}
  \def\@ctblbgactual{@ctblframebg}}
\def\@ctbltextsuperscript#1{%
  \ifx\@ctblsuper\@ctbltrue\@textsuperscript{#1}\else{\footnotesize#1}\fi
}
\def\@ctbltrue{1}
\def\@ctblfalse{0}
\def\@ctbldoinside{\relax}
\newdimen\@ctblframesep         \newdimen\@defaultctblframesep
\newdimen\@ctblframerule        \newdimen\@defaultctblframerule
\newdimen\@ctblwidth            \newdimen\@defaultctblwidth
\newdimen\@ctblcaptionskip      \newdimen\@defaultctblcaptionskip
\newdimen\@ctblmaxwidth         \newdimen\@defaultctblmaxwidth
\newdimen\@ctblmincapwidth      \newdimen\@defaultctblmincapwidth
\newdimen\@ctblw % the final width
\newdimen\@ctblfloatwidth
\newdimen\@ctbloldsep
\newdimen\@ctbloldrule
%    \end{macrocode}
%    Allocate box registers so that we can determine the widths of the
%    tables
%    \begin{macrocode}
\newbox\ctbl@t          % tabular saved and measured here
%    \end{macrocode}
% Option setting commands from keyval. The table position (here, top,
% bottom, page) gets a special treatment, since \LaTeX\ does not expand
% commands there. So instead of putting things like \texttt{tbp} in a
% command like |\@ctblbegin| we put
% |\begin{table}[tbp]| in it.
%    \begin{macrocode}

\define@key{suctbl}{botcap}[]{\let\@defaultctblbotcap\@ctbltrue}
\define@key{suctbl}{captionsinside}[]{\def\rot@LR{-1}
                                      \if@twoside\@rot@twosidetrue
                                      \else\@rot@twosidefalse\fi}
\define@key{suctbl}{captionsleft}[]{\@rot@twosidefalse\def\rot@LR{-1}}
\define@key{suctbl}{captionsright}[]{\@rot@twosidefalse\def\rot@LR{0}}
\define@key{suctbl}{captionskip}{\@defaultctblcaptionskip=#1}
\define@key{suctbl}{center}[]{\let\@defaultctblalign\centering}
\define@key{suctbl}{continued}{\def\@defaulttextcontinued{#1}}
\define@key{suctbl}{doinside}{\def\@defaultctbldoinside{#1}}
\define@key{suctbl}{figure}[]{\def\@defaultctbltaborfig{figure}}
\define@key{suctbl}{framebg}{\@defaultctblbgcolor#1=}
\define@key{suctbl}{framefg}{\@defaultctblfgcolor#1=}
\define@key{suctbl}{framerule}{\@defaultctblframerule=#1}
\define@key{suctbl}{framesep}{\@defaultctblframesep=#1}
\define@key{suctbl}{left}[]{\let\@defaultctblalign\raggedright}
\define@key{suctbl}{maxwidth}{\@defaultctblmaxwidth=#1}
\define@key{suctbl}{mincapwidth}{\@defaultctblmincapwidth=#1}
\define@key{suctbl}{nonotespar}[]{\let\@defaultctblnotespar\@ctblfalse}
\define@key{suctbl}{nosideways}[]{\let\@defaultctblsideways\empty}
\define@key{suctbl}{nostar}[]{\def\@defaultctblstarred{}}
\define@key{suctbl}{nosuper}[]{\let\@defaultctblsuper\@ctblfalse}
\define@key{suctbl}{notespar}[]{\let\@defaultctblnotespar\@ctbltrue}
\define@key{suctbl}{pos}{\def\@defaultctblpos{#1}}
\define@key{suctbl}{right}[]{\let\@defaultctblalign\raggedleft}
\define@key{suctbl}{sideways}[]{\def\@defaultctblsideways{sideways}}
\define@key{suctbl}{star}[]{\def\@defaultctblstarred{*}}
\define@key{suctbl}{super}[]{\let\@defaultctblsuper\@ctbltrue}
\define@key{suctbl}{table}[]{\def\@defaultctbltaborfig{table}}
\define@key{suctbl}{topcap}[]{\let\@defaultctblbotcap\@ctblfalse}
\define@key{suctbl}{width}{\@defaultctblwidth=#1}

\newcommand{\setupctable}[1]{\setkeys{suctbl}{#1}}
\setupctable{
  table,
  continued=(continued),
  topcap,
  framerule=0pt,
  captionskip=0pt,
  framesep=0pt,
  width=0pt,
  maxwidth=0pt,
  mincapwidth=0pt,
  nosideways,
  center,
  nostar,
  super,
  nonotespar,
  doinside={},
  framebg=1 1 1,
  framefg=0 0 0,
}

\define@key{ctbl}{botcap}[]{\let\@ctblbotcap\@ctbltrue}
\define@key{ctbl}{captionskip}{\@ctblcaptionskip=#1}
\define@key{ctbl}{caption}{\def\@ctblcaption{#1}}
\define@key{ctbl}{cap}{\def\@ctblcap{#1}}
\define@key{ctbl}{center}[]{\let\@ctblalign\centering}
\define@key{ctbl}{continued}[\@defaulttextcontinued]{\def\@ctblcontinued{#1}}
\define@key{ctbl}{doinside}{\def\@ctbldoinside{#1}}
\define@key{ctbl}{figure}[]{\def\@ctbltaborfig{figure}}
\define@key{ctbl}{framebg}{\@ctblbgcolor#1=}
\define@key{ctbl}{framefg}{\@ctblfgcolor#1=}
\define@key{ctbl}{framerule}{\@ctblframerule=#1}
\define@key{ctbl}{framesep}{\@ctblframesep=#1}
\define@key{ctbl}{label}{\def\@ctbllabel{#1}}
\define@key{ctbl}{left}[]{\let\@ctblalign\raggedright}
\define@key{ctbl}{maxwidth}{\@ctblmaxwidth=#1}
\define@key{ctbl}{mincapwidth}{\@ctblmincapwidth=#1}
\define@key{ctbl}{nonotespar}[]{\let\@ctblnotespar\@ctblfalse}
\define@key{ctbl}{nosideways}[]{\let\@ctblsideways\empty}
\define@key{ctbl}{nostar}[]{\def\@ctblstarred{}}
\define@key{ctbl}{nosuper}[]{\let\@ctblsuper\@ctblfalse}
\define@key{ctbl}{notespar}[]{\let\@ctblnotespar\@ctbltrue}
\define@key{ctbl}{pos}{\def\@ctblpos{#1}\def\@ctblbegin{\@ctblbeg[#1]}}
\define@key{ctbl}{right}[]{\let\@ctblalign\raggedleft}
\define@key{ctbl}{sideways}[]{\def\@ctblsideways{sideways}}
\define@key{ctbl}{star}[]{\def\@ctblstarred{*}}
\define@key{ctbl}{super}[]{\let\@ctblsuper\@ctbltrue}
\define@key{ctbl}{table}[]{\def\@ctbltaborfig{table}}
\define@key{ctbl}{topcap}[]{\let\@ctblbotcap\@ctblfalse}
\define@key{ctbl}{width}{\@ctblwidth=#1}
%    \end{macrocode}
% A caption will only be generated if the \textsl{caption} option was used, with a
% non-empty value. If so, it goes in the lot/lof, unless the \textsl{cap} option 
% specified a different (probably shorter) value for it. A \textsl{cap} option with
% an empty value inhibits a tof/lof entry.
%    \begin{macrocode}
\def\@ctblCaption{
   \ifx\@ctblcap\undefined\let\@ctblcap\@ctblcaption\fi
   \ifx\@ctblcaption\empty\else
      \def\@ctblcaptionarg{\ifx\@ctbllabel\empty\else\label{\@ctbllabel}\fi
         \@ctblcaption\ \@ctblcontinued\strut}
      \ifx\@ctblcap\empty
         \caption[]{\@ctblcaptionarg}
      \else
         \caption[\@ctblcap]{\@ctblcaptionarg}
      \fi
   \fi
}
%    \end{macrocode}
% Need to redefine X columntype, but the array package would generate a warning.
% So first set the type to be redefined to |\undefined| to suppress the warning.
% Save the standard X type once in the new type Y
%    \begin{macrocode}
\newcolumntype{Y}{X}
\def\@ctblXcolumntype#1{%
  \let\NC@find@X\undefined
  \newcolumntype{X}{#1}%
}
\def\@ctblframe#1#2#3{%
   \@ctbloldsep\fboxsep\fboxsep\@ctblframesep%
   \@ctbloldrule\fboxrule\fboxrule\@ctblframerule%
   \fcolorbox{#1}{#2}{\fboxsep\@ctbloldsep\fboxrule\@ctbloldrule #3}%
}
\newcommand{\tnote}[2][a]{%
   \ifx\@ctblnotespar\@ctbltrue%
     \@ctbltextsuperscript{\normalfont\textit{#1}}\,#2
   \else%
     \hbox{\@ctbltextsuperscript{\normalfont\textit{#1}}}&#2\NN
   \fi
}
\newcommand{\tmark}[1][a]{%
   \hbox{\textsuperscript{\normalfont\textit{#1}}}}
\newcommand{\ctable}[4][]{%
   \let\@ctbltaborfig  \@defaultctbltaborfig
   \let\@ctblalign     \@defaultctblalign
   \let\@ctblsideways  \@defaultctblsideways
   \let\@ctblcontinued \empty
   \let\@ctblpos       \@defaultctblpos
   \let\@ctblcaption   \empty
   \let\@ctblcap       \undefined
   \let\@ctbllabel     \empty
   \let\@ctblbotcap    \@defaultctblbotcap
   \let\@ctblstarred   \@defaultctblstarred
   \let\@ctblsuper     \@defaultctblsuper
   \let\@ctblnotespar  \@defaultctblnotespar
   \let\@ctbldoinside  \@defaultctbldoinside
   \@ctblframerule     \@defaultctblframerule
   \@ctblcaptionskip   \@defaultctblcaptionskip
   \@ctblframesep      \@defaultctblframesep
   \@ctblwidth         \@defaultctblwidth
   \@ctblmaxwidth      \@defaultctblmaxwidth
   \@ctblmincapwidth   \@defaultctblmincapwidth
   \def\@ctblfgactual {@defaultctblframefg}
   \def\@ctblbgactual {@defaultctblframebg}
   \def\@ctblbeg      {\begin{\@ctblsideways\@ctbltaborfig\@ctblstarred}}
   \def\@ctblbegin    {\@ctblbeg}
   \def\@ctblend      {\end{\@ctblsideways\@ctbltaborfig\@ctblstarred}}
   \setkeys{ctbl}{#1}
%    \end{macrocode}
% It makes no sense to use \textsl{width} together with \textsl{maxwidth} or
% \textsl{pos} together with \textsl{sideways}
%    \begin{macrocode}
   \ifdim\@ctblwidth=0pt\else
      \ifdim\@ctblmaxwidth=0pt\else
         \PackageError{ctable}{
            You may not use the width and maxwidth options together}{
            Use either width or maxwidth}
      \fi
   \fi
   \ifx\@ctblpos\empty
      \ifx\@ctblsideways\empty\else
      \PackageError{ctable}{
         You may not use the pos and sideways options together}{
         Rotated tables and figures are always typeset on a separate page}
      \fi
   \fi
%    \end{macrocode}
% It makes no sense to label a captionless table, because the label can't
% be placed, leaving the user wondering why references to the table get a ??
%    \begin{macrocode}
   \ifx\@ctblcaption\empty
      \ifx\@ctbllabel\empty\else
         \PackageError{ctable}{
            You may not label a captionless table}{
            Such a label can't be referenced}
      \fi
   \fi
%    \end{macrocode}
% save the table contents in a box, so we can determine its width,
% initially, save the table typeset with the tabular environment:
%    \begin{macrocode}
   \sbox\ctbl@t{%
      \@ctblXcolumntype{l}% temporarily make  type X = l
      \@ctblframe{\@ctblfgactual}{\@ctblbgactual}{%
         \@ctbldoinside
         \begin{tabular}{#2}
            #4%
         \end{tabular}%
      }%
   }%
%    \end{macrocode}
% then look if we'll need the tabularx environment:
%    \begin{macrocode}
   \newif\if@ctblusex\@ctblusexfalse
   \ifdim\@ctblmaxwidth=0pt
      \ifdim\@ctblwidth=0pt
      \else
         \@ctblusextrue
      \fi
   \else
      \ifdim\wd\ctbl@t>\@ctblmaxwidth
         \@ctblusextrue
      \fi
   \fi
%
% if so, replace tabular with tabularx:
%
   \if@ctblusex
      \sbox\ctbl@t{%
         \@ctblXcolumntype{Y}% restore X
         \@ctblframe{\@ctblfgactual}{\@ctblbgactual}{%
            \@ctbldoinside
            \begin{tabularx}{\ifdim\@ctblwidth>0pt\@ctblwidth\else\@ctblmaxwidth\fi}{#2}
               #4%
            \end{tabularx}%
         }%
      }%
   \fi
%    \end{macrocode}
% the |ctbl@t| box now contains the table as we want to typeset it;
% determine its width:
%    \begin{macrocode}
   \@ctblw=\wd\ctbl@t
%    \end{macrocode}
% Now find the width of the float, |\@ctblfloatwidth|; everything in it will
% be centered within that width.
% Normally we'll use the width of the table, |\@ctblw|, but if the
% mincapwidth, |\@ctblmincapwidth| was set wider than the table, that will be used:
%    \begin{macrocode}
   \@ctblfloatwidth=\ifdim\@ctblmincapwidth>\@ctblw
      \@ctblmincapwidth
   \else
      \@ctblw
   \fi
%    \end{macrocode}
% |\@ctblbegin| is now defined as something like |\begin{table}[tbp]|.
%    \begin{macrocode}
   \@ctblbegin
      \ifx\@ctblcontinued\empty\else\addtocounter{\@ctbltaborfig}{-1}\fi
      \@ctblalign
      \begin{minipage}{\@ctblfloatwidth}\parindent0pt
         \ifx\@ctblbotcap\@ctblfalse\@ctblCaption\vskip\@ctblcaptionskip\fi
         \centering{\usebox\ctbl@t} % insert the tabular
         \def\@ctblfootnotes{#3}%
         \ifx#3\empty\else{% append footnotes, if any
            \footnotesize
            \ifx\@ctblnotespar\@ctbltrue%
               \\[.2ex]
               \begin{minipage}{\hsize}%
                  #3%
               \end{minipage}%
            \else%
               \\
               \begin{tabularx}{\hsize}{r@{\,}>{\raggedright}X}
                  #3%
               \end{tabularx}%
            \fi

         }
         \fi
         \ifx\@ctblbotcap\@ctbltrue\vskip\@ctblcaptionskip\@ctblCaption\fi
      \end{minipage}
   \@ctblend
}
%    \end{macrocode}
% \Finale
\endinput
$Id: ctable.dtx,v 1.75 2011-08-24 14:01:54 wybo Exp $
