[Tex/LaTex] Last part of longtable after next float

floatslongtable

I'm having the following problem with a document I'm making. I have a table which is split over five pages. This is done with longtable and it works fine. In the next section I have another (simple) table. This second table is placed before the last part of the longtable. This gives the ugly result that the caption for table 6.8 is placed after the caption of table 6.9.

I can solve this by specifying that the second table should be placed at the bottom of the page and not the top, but ideally I would like the last part of the longtable to be placed at the top of the page and the second table immediately below that (and not the other way around as is now the case). Has anybody got a clue how this can be achieved?

Below is a 'small' example that illustrates the problem.

\documentclass[11pt]{article}

\usepackage{longtable}
\usepackage{booktabs}

\begin{document}
\section{First}

\begin{longtable}{crrrrrrrr}
\caption{Head}\\
\toprule
& \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5}\\
 & & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \\
\midrule
\endfirsthead
\caption[]{(Continued)}\\
\midrule
& \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5}\\
 & & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \\
\midrule
\endhead
\midrule  \multicolumn{9}{r}{{Continued on next page}} \\ 
\endfoot
\bottomrule
\endlastfoot
0 & 5 & N/A & 11 & N/A & N/A & N/A & N/A & N/A \\ 
1 &  & 14 & 25 & 10 & 16 & N/A & N/A & N/A \\ 
2 &  & 28 & 43 & 19 & 27 & 12 & 15 & N/A \\ 
3 &  & 46 & 65 & 30 & 40 & 18 & 21 & N/A \\ 
4 &  & 68 & 91 & 43 & 55 & 24 & 27 & N/A \\ 
5 &  & 94 & 121 & 58 & 72 & 30 & 33 & 16 \\ 
6 &  & 124 & 155 & 75 & 91 & 36 & 39 & 19 \\ 
7 &  & 158 & 193 & 94 & 112 & 42 & 45 & 22 \\ 
8 &  & 196 & 235 & 115 & 135 & 48 & 51 & 27 \\ 
9 &  & 238 & 281 & 138 & 160 & 54 & 57 & 30 \\ 
10 &  & 284 & 331 & 163 & 187 & 60 & 63 & 35 \\ 
11 &  & 334 & 385 & 190 & 216 & 66 & 69 & 38 \\ 
12 &  & 388 & 443 & 219 & 247 & 72 & 75 & 43 \\ 
13 &  & 446 & 505 & 250 & 280 & 78 & 81 & 46 \\ 
14 &  & 508 & 571 & 283 & 315 & 84 & 87 & 51 \\ 
15 &  & 574 & 641 & 318 & 352 & 90 & 93 & 54 \\ 
16 &  & 644 & 715 & 355 & 391 & 96 & 99 & 59 \\ 
17 &  & 718 & 793 & 394 & 432 & 102 & 105 & 62 \\ 
18 &  & 796 & 875 & 435 & 475 & 108 & 111 & 67 \\ 
19 &  & 878 & 961 & 478 & 520 & 114 & 117 & 70 \\ 
20 &  & 964 & 1051 & 523 & 567 & 120 & 123 & 75 \\ 
21 &  & 1054 & 1145 & 570 & 616 & 126 & 129 & 78 \\ 
22 &  & 1148 & 1243 & 619 & 667 & 132 & 135 & 83 \\ 
23 &  & 1246 & 1345 & 670 & 720 & 138 & 141 & 86 \\ 
24 &  & 1348 & 1451 & 723 & 775 & 144 & 147 & 91 \\ 
25 &  & 1454 & 1561 & 778 & 832 & 150 & 153 & 94 \\ 
26 &  & 1564 & 1675 & 835 & 891 & 156 & 159 & 99 \\ 
27 &  & 1678 & 1793 & 894 & 952 & 162 & 165 & 102 \\ 
28 &  & 1796 & 1915 & 955 & 1015 & 168 & 171 & 107 \\ 
29 &  & 1918 & 2041 & 1018 & 1080 & 174 & 177 & 110 \\ 
30 &  & 2044 & 2171 & 1083 & 1147 & 180 & 183 & 115 \\ 
31 &  & 2174 & 2305 & 1150 & 1216 & 186 & 189 & 118 \\ 
32 &  & 2308 & 2443 & 1219 & 1287 & 192 & 195 & 123 \\ 
33 &  & 2446 & 2585 & 1290 & 1360 & 198 & 201 & 126 \\ 
34 &  & 2588 & 2731 & 1363 & 1435 & 204 & 207 & 131 \\ 
35 &  & 2734 & 2881 & 1438 & 1512 & 210 & 213 & 134 \\ 
36 &  & 2884 & 3035 & 1515 & 1591 & 216 & 219 & 139 \\ 
37 &  & 3038 & 3193 & 1594 & 1672 & 222 & 225 & 142 \\ 
38 &  & 3196 & 3355 & 1675 & 1755 & 228 & 231 & 147 \\ 
39 &  & 3358 & 3521 & 1758 & 1840 & 234 & 237 & 150 \\ 
40 &  & 3524 & 3691 & 1843 & 1927 & 240 & 243 & 155 \\ 
\end{longtable}

\section{Second}

\begin{table}
\begin{center}
\begin{tabular}{cc}
\toprule
$p$ & Upper bound\\
\midrule
0 & $h\leq \frac{b^2+12}{48}$\\\addlinespace[0.75em]
1 & $h\leq \frac{b^2-25}{40}$\\\addlinespace[0.75em]
2 & $h\leq \frac{b^2-64}{32}$\\\addlinespace[0.75em]
3 & $h\leq \frac{b^2-81}{24}$\\\addlinespace[0.75em]
4 & $h\leq \frac{b^2-100}{16}$\\\addlinespace[0.75em]
5 & $h\leq \frac{b^2-113}{8}$\\
\bottomrule
\end{tabular}
\caption{The second table.}
\end{center}
\end{table}

\end{document}  

Best Answer

So this seems to work on this one test example. It implements the suggestion in the comment to turn the last fragment of the longtable to a top float. With the default float parameters the following table floated to the next page anyway, so I changed it to use [!t] to show this code does allow floats into the top area immediately following the long table.

enter image description here

\documentclass[11pt]{article}

\usepackage{longtable}
\usepackage{booktabs}

\makeatletter
\def\LT@output{%
  \ifnum\outputpenalty <-\@Mi
    \ifnum\outputpenalty > -\LT@end@pen
      \LT@err{floats and marginpars not allowed in a longtable}\@ehc
    \else
      \setbox\z@\vbox{\unvbox\@cclv}%
      \ifdim \ht\LT@lastfoot>\ht\LT@foot
        \dimen@\pagegoal
        \advance\dimen@-\ht\LT@lastfoot
        \ifdim\dimen@<\ht\z@
          \setbox\@cclv\vbox{\unvbox\z@\copy\LT@foot\vss}%
          \@makecol
          \@outputpage
          \setbox\z@\vbox{\box\LT@head}%
        \fi
      \fi
      \global\@colroom\@colht
      \global\vsize\@colht
\global\setbox\@ne\vbox{%
\unvbox\z@\box\ifvoid\LT@lastfoot\LT@foot\else\LT@lastfoot\fi}%
\gdef\zzz{%
     \begingroup\@float{table}[!t]%
\unvbox\@ne
\addvspace\LTpost
      \end@float\endgroup
}%
    \fi
  \else
    \setbox\@cclv\vbox{\unvbox\@cclv\copy\LT@foot\vss}%
    \@makecol
    \@outputpage
      \global\vsize\@colroom
    \copy\LT@head\nobreak
  \fi}

\def\endlongtable{%
  \crcr
  \noalign{%
    \let\LT@entry\LT@entry@chop
    \xdef\LT@save@row{\LT@save@row}}%
  \LT@echunk
  \LT@start
  \unvbox\z@
  \LT@get@widths
  \if@filesw
    {\let\LT@entry\LT@entry@write\immediate\write\@auxout{%
      \gdef\expandafter\noexpand
        \csname LT@\romannumeral\c@LT@tables\endcsname
          {\LT@save@row}}}%
  \fi
  \ifx\LT@save@row\LT@@save@row
  \else
    \LT@warn{Column \@width s have changed\MessageBreak
             in table \thetable}%
    \LT@final@warn
  \fi
  \endgraf\penalty -\LT@end@pen
  \endgroup
  \global\@mparbottom\z@
  \pagegoal\vsize
  \endgraf
  \zzz
  \penalty\z@
  \ifvoid\footins\else\insert\footins{}\fi}

\makeatother


\begin{document}
\section{First}

\begin{longtable}{crrrrrrrr}
\caption{Head}\\
\toprule
& \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5}\\
 & & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \\
\midrule
\endfirsthead
\caption[]{(Continued)}\\
\midrule
& \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{2} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{3} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{4} & \multicolumn{1}{c}{5}\\
 & & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \multicolumn{1}{c}{s} & \multicolumn{1}{c}{n} & \\
\midrule
\endhead
\midrule  \multicolumn{9}{r}{{Continued on next page}} \\ 
\endfoot
\bottomrule
\endlastfoot
0 & 5 & N/A & 11 & N/A & N/A & N/A & N/A & N/A \\ 
1 &  & 14 & 25 & 10 & 16 & N/A & N/A & N/A \\ 
2 &  & 28 & 43 & 19 & 27 & 12 & 15 & N/A \\ 
3 &  & 46 & 65 & 30 & 40 & 18 & 21 & N/A \\ 
4 &  & 68 & 91 & 43 & 55 & 24 & 27 & N/A \\ 
5 &  & 94 & 121 & 58 & 72 & 30 & 33 & 16 \\ 
6 &  & 124 & 155 & 75 & 91 & 36 & 39 & 19 \\ 
7 &  & 158 & 193 & 94 & 112 & 42 & 45 & 22 \\ 
8 &  & 196 & 235 & 115 & 135 & 48 & 51 & 27 \\ 
9 &  & 238 & 281 & 138 & 160 & 54 & 57 & 30 \\ 
10 &  & 284 & 331 & 163 & 187 & 60 & 63 & 35 \\ 
11 &  & 334 & 385 & 190 & 216 & 66 & 69 & 38 \\ 
12 &  & 388 & 443 & 219 & 247 & 72 & 75 & 43 \\ 
13 &  & 446 & 505 & 250 & 280 & 78 & 81 & 46 \\ 
14 &  & 508 & 571 & 283 & 315 & 84 & 87 & 51 \\ 
15 &  & 574 & 641 & 318 & 352 & 90 & 93 & 54 \\ 
16 &  & 644 & 715 & 355 & 391 & 96 & 99 & 59 \\ 
17 &  & 718 & 793 & 394 & 432 & 102 & 105 & 62 \\ 
18 &  & 796 & 875 & 435 & 475 & 108 & 111 & 67 \\ 
19 &  & 878 & 961 & 478 & 520 & 114 & 117 & 70 \\ 
20 &  & 964 & 1051 & 523 & 567 & 120 & 123 & 75 \\ 
21 &  & 1054 & 1145 & 570 & 616 & 126 & 129 & 78 \\ 
22 &  & 1148 & 1243 & 619 & 667 & 132 & 135 & 83 \\ 
23 &  & 1246 & 1345 & 670 & 720 & 138 & 141 & 86 \\ 
24 &  & 1348 & 1451 & 723 & 775 & 144 & 147 & 91 \\ 
25 &  & 1454 & 1561 & 778 & 832 & 150 & 153 & 94 \\ 
26 &  & 1564 & 1675 & 835 & 891 & 156 & 159 & 99 \\ 
27 &  & 1678 & 1793 & 894 & 952 & 162 & 165 & 102 \\ 
28 &  & 1796 & 1915 & 955 & 1015 & 168 & 171 & 107 \\ 
29 &  & 1918 & 2041 & 1018 & 1080 & 174 & 177 & 110 \\ 
30 &  & 2044 & 2171 & 1083 & 1147 & 180 & 183 & 115 \\ 
31 &  & 2174 & 2305 & 1150 & 1216 & 186 & 189 & 118 \\ 
32 &  & 2308 & 2443 & 1219 & 1287 & 192 & 195 & 123 \\ 
33 &  & 2446 & 2585 & 1290 & 1360 & 198 & 201 & 126 \\ 
34 &  & 2588 & 2731 & 1363 & 1435 & 204 & 207 & 131 \\ 
35 &  & 2734 & 2881 & 1438 & 1512 & 210 & 213 & 134 \\ 
36 &  & 2884 & 3035 & 1515 & 1591 & 216 & 219 & 139 \\ 
37 &  & 3038 & 3193 & 1594 & 1672 & 222 & 225 & 142 \\ 
38 &  & 3196 & 3355 & 1675 & 1755 & 228 & 231 & 147 \\ 
39 &  & 3358 & 3521 & 1758 & 1840 & 234 & 237 & 150 \\ 
40 &  & 3524 & 3691 & 1843 & 1927 & 240 & 243 & 155 \\ 
\end{longtable}

\section{Second}

\begin{table}[!t]
\begin{center}
\begin{tabular}{cc}
\toprule
$p$ & Upper bound\\
\midrule
0 & $h\leq \frac{b^2+12}{48}$\\\addlinespace[0.75em]
1 & $h\leq \frac{b^2-25}{40}$\\\addlinespace[0.75em]
2 & $h\leq \frac{b^2-64}{32}$\\\addlinespace[0.75em]
3 & $h\leq \frac{b^2-81}{24}$\\\addlinespace[0.75em]
4 & $h\leq \frac{b^2-100}{16}$\\\addlinespace[0.75em]
5 & $h\leq \frac{b^2-113}{8}$\\
\bottomrule
\end{tabular}
\caption{The second table.}
\end{center}
\end{table}

\end{document}