I have been doing a lot of searches, but am not able to find a proper solution. I am using \captionof{} for a table, which is near the bottom of a page. As the table cannot break across pages (which is what I want in this case), it is placed on the next page. The problem I have is that the caption remains on the first page and does not stay properly above the table.
Although things like \parbox{} and \begin{minipage} work, the side effect they introduce is that my footnotes inside the table no longer work.
Here is an example that shows how the table's caption stays on page 1 and the table moves to page 2, however the footnotes in the table work. If you enable the \parbox, you will see that the caption stays with the table, however the footnotes no longer show.
\documentclass[12pt,a4paper]{article}
% Details for tables
\usepackage{longtable,tabu}
\tabulinesep=1.2mm
\usepackage[hidelinks]{hyperref}
\usepackage{caption}
\usepackage{footnote}
% End details for tables
\begin{document}
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
Lots of text to get to the bottom of the page.\\
\noindent
% \parbox{\textwidth}{ % enable this line to get the \parbox example working, whereby the footnotes disappear, but the caption stays on the same page as the table.
\captionof{table}{Example of non-wrapping table}
\begin{tabu} to \textwidth {X[1,c,p]X[3,c,p]X[3,l,m]X[10,r,b]}
\hline
\textbf{Time End} & \textbf{Step} & \textbf{Event} & \textbf{Description} \\
\hline
12:18 & 1 & 1 & Notice how this column is defined as ``b'', causing the text on the other rows to be bottom aligned. \\
12:18 & 1 & 1\footnote{testing} & this is a description for the event \\
12:18 & 1 & 1 & this is a description for the event \\
12:18 & 1 & Here we inserted extra text. & Note how column 3 is defined as ``m'', causing other columns to be middle aligned. \\
12:18 & 1 & 1 & Column 1 = ``p''. All at the top. \\
12:18 & 1\footnote{another footnote} & 1 & this is a description for the event \\
12:18 & 1 & 1 & this is a description for the event \\
12:18 or later & 1 & 1 & this is a description for the event \\
12:18 & 1 & 1 & this is a description for the event \\
\hline
\end{tabu}
% } % enable } to get the \parbox example working, whereby the footnotes disappear, but the caption stays on the same page as the table.
\end{document}
The reason for using the tabu environment is that I also use longtabu, which allows me to create page-wrapping tables. I want to have the ability to quickly switch between either a page-wrapping or a non-page-wrapping table.
Thanks in advance for your suggestions.
Marc.
=================UPDATE================= Following John's answer, I have copied/pasted his MWE (see below) and added some additional text in the document along with some additional footnotes. Doing so causes unexpected behaviour with respect to placement of footnotes before the \parbox starts. Another observation I made is that the hyper-links for the footnotes inside the \parbox are not linking to the correct spot.
Rather than using \parbox, would it be somehow possible to force a non-breaking new line between the \captionof and \begin{tabu} lines? I would think that this would be a much easier way to a solution.
\documentclass[12pt,a4paper]{article}
% Details for tables
\usepackage{longtable,tabu}
\tabulinesep=1.2mm
\usepackage[hidelinks]{hyperref}
\usepackage{caption}
% End details for tables
\newcount\savefnused
\newcount\savefndone
\newcount\maxfootnote
\newcommand{\savefootnote}[2][\empty]% #1=number (optional), #2=text
{\ifx\empty#1\footnotemark
\ifnum\value{footnote}>\maxfootnote
\global\maxfootnote=\value{footnote}%
\global\advance\savefnused by 1
\expandafter\xdef\csname savefnmark\the\savefnused\endcsname{\thefootnote}%
\expandafter\xdef\csname savefntext\the\savefnused\endcsname{#2}%
\fi
\else
\footnotemark[#1]% does not affect footnote counter
\global\advance\savefnused by 1
\expandafter\xdef\csname savefnmark\the\savefnused\endcsname{\thefootnote}%
\expandafter\xdef\csname savefntext\the\savefnused\endcsname{#2}%
\fi}
\newcommand{\flushfootnote}{\loop\ifnum\savefndone<\savefnused
\global\advance\savefndone by 1
\footnotetext[\csname savefnmark\the\savefndone\endcsname]%
{\csname savefntext\the\savefndone\endcsname}%
\global\expandafter\let\csname savefnmark\the\savefndone\endcsname\relax
\global\expandafter\let\csname savefntext\the\savefndone\endcsname\relax
\repeat
\setcounter{footnote}{\number\maxfootnote}}
\begin{document}
\rule{1pt}{20\baselineskip}\\
When I use a footnote\footnote{test footnote} on the page previous to the parbox, I observe that the footnote does not show up at the bottom of the page, but rather directly after this text.
\noindent\parbox{\textwidth}{%
\captionof{table}{Example of non-wrapping table}
\begin{tabu} to \textwidth {X[1,c,p]X[3,c,p]X[3,l,m]X[10,r,b]}
\hline
\textbf{Time} & \textbf{Step} & \textbf{Event} & \textbf{Description} \\
\hline
12:18 & 1 & 1 & Notice how this column is defined as ``b'', causing the text on the other rows to be bottom aligned. \\
12:18 & 1 & 1\savefootnote{testing} & this is a description for the event \\
12:18 & 1 & 1 & this is a description for the event \\
12:18 & 1 & Here we inserted extra text. & Note how column 3 is defined as ``m'', causing other columns to be middle aligned. \\
12:18 & 1 & 1 & Column 1 = ``p''. All at the top. \\
12:18 & 1\savefootnote{another footnote} & 1 & this is a description for the event \\
12:18 & 1 & 1 & this is a description for the event \\
12:18 or later & 1 & 1 & this is a description for the event \\
12:18 & 1 & 1 & this is a description for the event \\
\hline
\end{tabu}}
\flushfootnote
Now that we have reached the end of the table our normal text continues. In this normal text we can obviously also encounter footnotes\footnote{look here is another footnote}. These do show up properly.
Another observation is that the hyper-links for the footnotes inside the parbox seem to refer back to the previous page.
\end{document}
Thanks for looking into this.
\captionof{}
from breaking away from what it is intended to identify is to keep them in the same unbreakable unit. wrapping them in aminipage
should do the job, since thistabu
is supposed to fit on a page.