# How to force \captionof{} to be not followed by a page break?

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{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.

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{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}%
\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
\expandafter\xdef\csname savefnmark\the\savefnused\endcsname{\thefootnote}%
\expandafter\xdef\csname savefntext\the\savefnused\endcsname{#2}%
\fi}

\newcommand{\flushfootnote}{\loop\ifnum\savefndone<\savefnused
\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.

• welcome to tex.sx. the only sure way i know of to keep \captionof{} from breaking away from what it is intended to identify is to keep them in the same unbreakable unit. wrapping them in a minipage should do the job, since this tabu is supposed to fit on a page. – barbara beeton Sep 8 '16 at 3:00
• I don't see your footnotes working now. A full MWE (with a complete list of packages) would be useful. – John Kormylo Sep 8 '16 at 3:28
• I have updated the original question and included a full MWE. – Marc Sep 8 '16 at 4:47
• At one time I had a solution here using \needspace. It is still on the system somewhere, although I don't know how to access it. – John Kormylo Sep 9 '16 at 0:40

I tried my solution for \parbox (Why does \parbox lose footnotes?) but it produced three copies of each footnote.

I modified the solution to ignore repeat values of the footnote counter. I should mention that this solution is incompatible with the footnote package.

\documentclass[12pt,a4paper]{article}
% Details for tables
\usepackage{longtable,tabu}
\tabulinesep=1.2mm
\usepackage{hyperref}% required
\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}%
\expandafter\xdef\csname savefnmark\the\savefnused\endcsname{\thefootnote}%
\expandafter\xdef\csname savefntext\the\savefnused\endcsname{#2}%
\expandafter\xdef\csname savefnname\the\savefnused\endcsname{\csname Hy@footnote@currentHref\endcsname}%
\fi
\else
\footnotemark[#1]% does not affect footnote counter
\expandafter\xdef\csname savefnmark\the\savefnused\endcsname{\thefootnote}%
\expandafter\xdef\csname savefntext\the\savefnused\endcsname{#2}%
\expandafter\xdef\csname savefnname\the\savefnused\endcsname{\csname Hy@footnote@currentHref\endcsname}%
\fi}

\newcommand{\flushfootnote}{\loop\ifnum\savefndone<\savefnused
\footnotetext[\csname savefnmark\the\savefndone\endcsname]%
{\hypertarget{\csname savefnname\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
\global\expandafter\let\csname savefnname\the\savefndone\endcsname\relax
\repeat
\setcounter{footnote}{\number\maxfootnote}}

\begin{document}
\rule{1pt}{20\baselineskip}

\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 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\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

\end{document}

• John, thanks for your response. I have played around with your MWE and added some text to it to see how that works. As a result, I have updated your MWE inside my original posting. I found some issues with footnote placement and hyper-links... – Marc Sep 9 '16 at 0:17
• I didn't realize footnotes had hyperlinks. I mean, how hard is it to find the bottom of the page? – John Kormylo Sep 9 '16 at 1:10
• Not hard at all. It was merely an observation. – Marc Sep 9 '16 at 3:18

I have done some more research, thanks to John's comment on the needspace package. I found that this package helps to prevent a page break being inserted between the caption and the table itself. As a result, there is no need to use the \parbox.

However, it did not address the issue of the footnote on the first page showing up too high on the page; that is to say, not at the bottom of the page. I found the footmisc package that effectively deals with this.

On the minor issue of the hyper-links for the footnotes not working properly, I found that this had to do with when the hyperref package was being loaded.

Below is the properly working MWE with all the features working properly.

\documentclass[12pt,a4paper]{article}
% Details for tables
\usepackage{longtable,tabu}
\tabulinesep=1.2mm
\usepackage{caption}
\usepackage{needspace}
\usepackage[bottom]{footmisc}
% End details for tables

\begin{document}
\rule{1pt}{20\baselineskip}\\
When I use a footnote\footnote{first footnote} on the page previous to the table, I observe that the footnote now shows up at the bottom of the page, thanks to the footmisc package.

\needspace{5\baselineskip}
\noindent
\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\footnote{second footnote} & 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{third 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}

\paragraph{}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{fourth footnote}. These do show up properly.

Another observation is that the hyper-links for the footnotes are now working properly.
\end{document}