Caption and footnote width with ctable

I use, in this cases, a "poor man's ctable":

\documentclass{article}
\usepackage{booktabs}

\makeatletter
\newsavebox{\@tabnotebox}
\providecommand\tmark{} % so having ctable or not is irrelevant
\providecommand\tnote{}
\newenvironment{tabularwithnotes}[3][c]
  {\long\def\@tabnotes{#3}%
   \renewcommand\tmark[1][a]{\makebox[0pt][l]{\textsuperscript{\itshape##1}}}%
   \renewcommand\tnote[2][a]{\textsuperscript{\itshape##1}\,##2\par}
   \begin{lrbox}{\@tabnotebox}
   \begin{tabular}{#2}}
  {\end{tabular}\end{lrbox}%
   \parbox{\wd\@tabnotebox}{
     \usebox{\@tabnotebox}\par
     \smallskip\@tabnotes
   }%
  }
\makeatother

\begin{document}
\begin{table}
\centering
\caption{Somewhat long long long caption which is long}
\medskip
\begin{tabularwithnotes}{lr}
 {
  \tnote[a]{Short footnote}
  \tnote[b]{Short footnote}
 }
\toprule
Header 1 & Header 2 \\
\midrule
foo\tmark[a] & 1\tmark[b] \\
\bottomrule
\end{tabularwithnotes}
\end{table}
\end{document}

The arguments to tabularwithnotes are the same as for tabular, with the notes in an additional argument.

enter image description here


Here is a solution that adds a new key-value footerwidth (default is the width of the table):

enter image description here

\documentclass{article}
\usepackage{ctable}% http://ctan.org/pkg/ctable
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{xpatch}% http://ctan.org/pkg/etoolbox

\makeatletter
\newdimen\@ctblfooterwidth
\define@key{ctbl}{footerwidth}{\@ctblfooterwidth=#1}
\xpatchcmd{\ctable}% <cmd>
  {{\@ctblbeg}}% <search>
  {{\ifdim\@ctblfooterwidth=0pt
     \@ctblfooterwidth\@ctblw% or \@ctblfloatwidth for default as width of float
   \fi%
   \@ctblbeg}}% <replace> {\@defaultctblmincapwidth%
  {}{}% <success><failure>
\xpatchcmd{\ctable}{\hsize}{\@ctblfooterwidth}{}{}% Patch \ctable
\xpatchcmd{\ctable}{\hsize}{\@ctblfooterwidth}{}{}% Patch \ctable
\makeatother

\begin{document}
\ctable[
caption = {Somewhat long long long caption which is long},
mincapwidth = \textwidth,
]{lr}{
  \tnote[a]{Short footnote}
}{
    \toprule
    Header 1 & Header 2 \\
    \midrule
    foo\tmark[a] & 1 \\
    \bottomrule
}

\ctable[
caption = {Somewhat long long long caption which is long},
mincapwidth = \textwidth,
footerwidth = .5\textwidth
]{lr}{
  \tnote[a]{Short footnote}
}{
    \toprule
    Header 1 & Header 2 \\
    \midrule
    foo\tmark[a] & 1 \\
    \bottomrule
}
\end{document}

xpatch provides the patch to \ctable since it takes an optional argument.

The first patch inserts the appropriate definition of the newly defined dimension \@ctblfooterwidth if it has no length. The second and third patch replaces the default \hsize width of the footnote encasing in either a minipage or tabularx environment, as defined within \ctable originally.


Send a feature request – there should be an option to have the caption set outside the minipage which contains the table and notes. In the meantime, you can make this change yourself:

\makeatletter
\newcommand{\modctable}[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}
   \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
   \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
   \sbox\ctbl@t{%
      \@ctblXcolumntype{l}% temporarily make  type X = l
      \@ctblframe{\@ctblfgactual}{\@ctblbgactual}{%
         \@ctbldoinside
         \begin{tabular}{#2}
            #4%
         \end{tabular}%
      }%
   }%
   \newif\if@ctblusex\@ctblusexfalse
   \ifdim\@ctblmaxwidth=0pt
      \ifdim\@ctblwidth=0pt
      \else
         \@ctblusextrue
      \fi
   \else
      \ifdim\wd\ctbl@t>\@ctblmaxwidth
         \@ctblusextrue
      \fi
   \fi
   \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
   \@ctblw=\wd\ctbl@t
   \@ctblfloatwidth=\ifdim\@ctblmincapwidth>\@ctblw
      \@ctblmincapwidth
   \else
      \@ctblw
   \fi
   \@ctblbegin
      \ifx\@ctblcontinued\empty\else\addtocounter{\@ctbltaborfig}{-1}\fi
      \@ctblalign
      \@ctblCaption
      \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
}
\makeatother

Then the command in-text becomes:

\modctable[
caption = {Somewhat long long long caption which is long},
]{lr}{
  \tnote[a]{Short footnote}
}{
    \toprule
    Header 1 & Header 2 \\
    \midrule
    foo\tmark[a] & 1 \\
    \bottomrule
}