Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}  
share|improve this question
2  
I'm not sure whether I understood the problem, but try using \usepackage{placeins} in the document's preamble and \FloatBarrier between the longtable and the table. That should prevent the table from floating into the longtable (if that is indeed your problem). –  Stephen Jun 19 '12 at 13:30
    
Please, provide a fully working minimal example, i.e. just the longtable, the second float and some surrounding text, but as a whole file, starting \documentclass and ending \end{document}. –  tohecz Jun 19 '12 at 13:30
1  
Don't float the second table: put it into a center environment just after the end of the longtable and use \captionof{table}{...} for the caption (packages caption or capt-of). –  egreg Jun 19 '12 at 13:35
    
@Stephen : Thanks, the package placeins indeed solves the problem, although it makes the second table move to the next page. But that isn't that much of an issue. –  nvcleemp Jun 19 '12 at 13:44
    
OK, for information about floats I have included some more possibilities in an answer. Do not skip to look at Frank Mittelbach's answer (link at the end of my answer)! –  Stephen Jun 19 '12 at 14:07

2 Answers 2

up vote 1 down vote accepted

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}
share|improve this answer

To prevent "stuff" to float where it should not float to, there are diverse possibilities. The documentation of the morefloats package says:

  • The command \clearpage forces LaTeX to output any floating objects that occurred before this command (and go to the next page). \cleardoublepage does the same but ensures that the next page with output is one with odd page number.
  • Using different float specifiers: t top, b bottom, p page of floats.
  • Suggesting LaTeX to put the object where it was placed: h (= here) float specifier.
  • Telling LaTeX to please put the object where it was placed: h! (= here!) float specifier.
  • Forcing LaTeX to put the object where it was placed and shut up: The float package provides the “style option here, giving floating environments a [H] option which means ‘PUT IT HERE’ (as opposed to the standard [h] option which means ‘You may put it here if you like’)” (float package documentation v1.3d as of 2001/11/08). Changing e. g. \begin{figure}[tbp]... to \begin{figure}[H]... forces the figure to be placed HERE instead of floating away. The float package, 2001/11/08 v1.3d, is available at CTAN: http://ctan.org/pkg/float
  • The placeins package provides the command \FloatBarrier. Floats occurring before the \FloatBarrier are not allowed to float to a later place, and floats occurring after the \FloatBarrier are not allowed to float to a earlier place than the \FloatBarrier. (There can be more than one \FloatBarrier in a document.) The same package also provides an option to add to section headings automatically \FloatBarriers. It is further possible to make \FloatBarriers less strict (see that package’s documentation). The placeins package, 2005/04/18 v2.2, is available at CTAN: http://ctan.org/pkg/placeins

See also the following entries in the UK List of TeX Frequently Asked Questions on the Web:

and the excellent article on “How to influence the position of float environments like figure and table in LaTeX?” by Frank Mittelbach at http://tex.stackexchange.com/a/39020!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.