# Several caption problems in longtable and tabulary

I'm trying to typeset some multipage tables with tablenotes and repeating captions on each page. I found this question, which marries the longtable environment with tabulary. This is quite nice, because then I don't need to take care of the column widths myself, but can use one of the LRJC options of tabulary to do that for me.

In the following MWE, almost everything works out nicely, but there are some caption problems.

\documentclass{scrartcl}

\usepackage[hypcap=true]{caption}
\captionsetup{labelfont=bf, justification=justified, singlelinecheck=false, format=plain}

\usepackage{tabulary, booktabs, longtable, ltcaption}
\usepackage[referable]{threeparttablex}

%% define a new envrionment which marries longtable with tabulary
\makeatletter
\def\ltabulary{%
\def\endfoot{\\}%
\def\endlastfoot{\\}%
\def\tabulary{%
\def\TY@final{%
\def\endfoot{\LT@end@hd@ft\LT@foot}%
\def\endlastfoot{\LT@end@hd@ft\LT@lastfoot}%
\longtable}%
\let\endTY@final\endlongtable
\TY@tabular}%
\dimen@\columnwidth
\tabulary\dimen@}
\def\endltabulary{\endtabulary}
\makeatother
% from https://tex.stackexchange.com/questions/78075/multi-page-with-tabulary (see there for usage)

\begin{document}

\begin{table}
\caption{Nice long caption directly above the start of the table with lots of...}
\begin{threeparttable}
\begin{tabulary}{\textwidth}{LlLLL}
\toprule
top cell one with title & top cell two with title & longtop with note & top cell four with title & top cell five with title\\
\midrule
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
\bottomrule
\end{tabulary}
\begin{tablenotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{tablenotes}
\end{threeparttable}
\end{table}

\begin{ThreePartTable}
\begin{TableNotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{TableNotes}
%   \setlength\LTleft{0pt}
%   \setlength\LTright{0pt}
\captionof{table}{Example of long table over multiple pages}
\begin{ltabulary}{rLlcccc}
\toprule
cell 1 & cell with linebreaks if it gets too wide and wider and wider and wider and wider & cell 3 & cell 4 & 5 & 6 & 7 \\
\midrule
\toprule
cell 1 & cell with linebreaks if it gets too wide & cell 3 & cell 4 & 5 & 6 & 7 \\
\midrule
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\end{ltabulary}
\end{ThreePartTable}

\begin{table}
\caption{Nice long caption directly above the start of the table with lots of...}
\begin{threeparttable}
\begin{tabulary}{\textwidth}{LlLLL}
\toprule
top cell one with title & top cell two with title & longtop with note & top cell four with title & top cell five with title\\
\midrule
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
\bottomrule
\end{tabulary}
\begin{tablenotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


The above code compiles fine, but I have to use \captionof which produce problems 2 to 4. If I replace it with \caption{Example of long table over multiple pages}, I get the errors in problem 1.

These are the problems:

1. I cannot use the \caption{} command. It gives me ! LaTeX Error: \caption outside float. This is the case regardless if I use the caption package or not. Also, the placement does not matter. I can put it inside the ltabulary environment or outside, both times it gives me the same error. I suspect that, since longtable uses its own caption command and it's not included in the definition of ltabulary from the question, it does not work. If that is the case, the error makes sense, because longtable is not set inside a float which \caption needs. I worked around this by using the \captionof command from the caption package, but this comes with its own problems:
2. The spacing between the caption and the table is too big compared to the other two tables. This can be fixed with \vspace{-12pt}, but it's rather tedious to do that for each table.
3. The numbering of subsequent tables is wrong. The third table in the MWE is numbered 4 instead of 3. I suspect this is a problem with longtable and \captionof, because longtable sets a new table for each page which increases the table counter. This can be fixed with \addtocounter{table}{-1}, but this is, again, rather tedious.
4. I cannot repeat the caption for each page, because \captionof inside the ltabulary environment gives me an error: ! Missing \endgroup inserted. I could probably fix this by setting the caption manually, but this is even more tedious, because if I move a table, I have to go back to each table and adjust the numbering. It should also not repeat the whole caption for each page, but add something along the lines of "Table 2 (continued)".

Is there some wizard here who can add the \caption command to the ltabulary definition (which would presumably fix all the problems above in one go), or fix it some other way?

To my utter surprise, I actually managed to patch the code for the ltabulary environment to add support for the \caption command. The patched code is taken from the ltxtable package with very minor changes and comprises a whooping 3 lines.

The below code solves all problems in one go, as suggested in the first problem in the question. Replace the corresponding code in the MWE with this one, convert the \captionof command into a \caption command and move it inside the ltabulary environment.

%% define a new envrionment which marries longtable with tabulary
% from http://tex.stackexchange.com/questions/78075/multi-page-with-tabulary (see there for usage)
% with modifications taken from the ltxtable package to make captions work
\makeatletter
\def\ltabulary{%
\@ifstar\TY@cap@gobble\TY@cap@gobble}
\def\endfoot{\\}%
\def\endlastfoot{\\}%
\def\tabulary{%
\def\TY@final{%
\def\endfoot{\LT@end@hd@ft\LT@foot}%
\def\endlastfoot{\LT@end@hd@ft\LT@lastfoot}%
\longtable}%
\let\endTY@final\endlongtable
\TY@tabular}%
\dimen@\columnwidth
\tabulary\dimen@}
\def\endltabulary{\endtabulary}
\makeatother


For the sake of completeness, here is the corrected MWE:

\documentclass{scrartcl}

\usepackage[hypcap=true]{caption}
\captionsetup{labelfont=bf, justification=justified, singlelinecheck=false, format=plain}

\usepackage{tabulary, booktabs, longtable, ltcaption, ltabptch}
\usepackage[referable]{threeparttablex}

%% define a new envrionment which marries longtable with tabulary
% from http://tex.stackexchange.com/questions/78075/multi-page-with-tabulary (see there for usage)
% with modifications taken from the ltxtable package to make captions work
\makeatletter
\def\ltabulary{%
\@ifstar\TY@cap@gobble\TY@cap@gobble}
\def\endfoot{\\}%
\def\endlastfoot{\\}%
\def\tabulary{%
\def\TY@final{%
\def\endfoot{\LT@end@hd@ft\LT@foot}%
\def\endlastfoot{\LT@end@hd@ft\LT@lastfoot}%
\longtable}%
\let\endTY@final\endlongtable
\TY@tabular}%
\dimen@\columnwidth
\tabulary\dimen@}
\def\endltabulary{\endtabulary}
\makeatother

\begin{document}

\begin{table}
\caption{Nice long caption directly above the start of the table with lots of...}
\begin{threeparttable}
\begin{tabulary}{\textwidth}{LlLLL}
\toprule
top cell one with title & top cell two with title & longtop with note & top cell four with title & top cell five with title\\
\midrule
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
\bottomrule
\end{tabulary}
\begin{tablenotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{tablenotes}
\end{threeparttable}
\end{table}

\begin{ThreePartTable}
\begin{TableNotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{TableNotes}
%   \setlength\LTleft{0pt}
%   \setlength\LTright{0pt}
%   \captionof{table}{Example of long table over multiple pages}
\begin{ltabulary}{rLlcccc}
\caption{Example of long table over multiple pages}\\
\toprule
cell 1 & cell with linebreaks if it gets too wide and wider and wider and wider and wider & cell 3 & cell 4 & 5 & 6 & 7 \\
\midrule
\caption[]{(continued)}\\
\toprule
cell 1 & cell with linebreaks if it gets too wide & cell 3 & cell 4 & 5 & 6 & 7 \\
\midrule
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\end{ltabulary}
\end{ThreePartTable}

\begin{table}
\caption{Nice long caption directly above the start of the table with lots of...}
\begin{threeparttable}
\begin{tabulary}{\textwidth}{LlLLL}
\toprule
top cell one with title & top cell two with title & longtop with note & top cell four with title & top cell five with title\\
\midrule
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
\bottomrule
\end{tabulary}
\begin{tablenotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


And here, excerpts of the result which did not work before:

If I feel daring, I might convert this to an actual package.

Well I read your MWE code and the first two lines repeated once, I erased from the MWE, later I copied the code and compile on my machine without errors. The first time only the second table gives an error showing the footnote in very narrow column. But the second time all look good.

I fear the errors your reported come from those duplicated lines in your code, in my case this is the code I compile:

\documentclass{scrartcl}

\usepackage[hypcap=true]{caption}
\captionsetup{labelfont=bf, justification=justified, singlelinecheck=false, format=plain}

\usepackage{tabulary,booktabs,longtable,ltcaption}
\usepackage[referable]{threeparttablex}

%% define a new envrionment which marries longtable with tabulary
\makeatletter
\def\ltabulary{%
\def\endfoot{\\}%
\def\endlastfoot{\\}%
\def\tabulary{%
\def\TY@final{%
\def\endfoot{\LT@end@hd@ft\LT@foot}%
\def\endlastfoot{\LT@end@hd@ft\LT@lastfoot}%
\longtable}%
\let\endTY@final\endlongtable
\TY@tabular}%
\dimen@\columnwidth
\tabulary\dimen@}
\def\endltabulary{\endtabulary}
\makeatother
% from http://tex.stackexchange.com/questions/78075/multi-page-with-tabulary (see there for usage)

\begin{document}

\begin{table}
\caption{Nice long caption directly above the start of the table with lots of...}
\begin{threeparttable}
\begin{tabulary}{\textwidth}{LlLLL}
\toprule
top cell one with title & top cell two with title & longtop with note & top cell four with title & top cell five with title\\
\midrule
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
\bottomrule
\end{tabulary}
\begin{tablenotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{tablenotes}
\end{threeparttable}
\end{table}

\begin{ThreePartTable}
\begin{TableNotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{TableNotes}
%   \setlength\LTleft{0pt}
%   \setlength\LTright{0pt}
\captionof{table}{Example of long table over multiple pages}
\begin{ltabulary}{rLlcccc}
\toprule
cell 1 & cell with linebreaks if it gets too wide and wider and wider and wider and wider & cell 3 & cell 4 & 5 & 6 & 7 \\
\midrule
\toprule
cell 1 & cell with linebreaks if it gets too wide & cell 3 & cell 4 & 5 & 6 & 7 \\
\midrule
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\end{ltabulary}
\end{ThreePartTable}

\begin{table}
\caption{Nice long caption directly above the start of the table with lots of...}
\begin{threeparttable}
\begin{tabulary}{\textwidth}{LlLLL}
\toprule
top cell one with title & top cell two with title & longtop with note & top cell four with title & top cell five with title\\
\midrule
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
cell & cell & big long cell stuff & cell & more cell\\
\bottomrule
\end{tabulary}
\begin{tablenotes}\footnotesize
\note This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines. This is a long explanation going over multiple lines.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}


I used Sublime Text 2 with LaTeXtools package for compile it. My machine uses Ubuntu 14.04as SO and I use TeXlive 2013. If I open a terminal and I use pdflatex directly the result is the same.

• Ah, sorry, we have a misunderstanding here. I edited the question to make it clearer. Basically, The MWE compiles fine, but it uses the \captionof command to get a caption in the second table. But that produces problems 2 to 4 (see your pictures, I have the same output). I'd like to use the longtable \caption command to eliminate these, but that gives me problem 1. – justibus Aug 24 '14 at 18:34