# Table cannot locate itself properly inside the document

Sometimes, table cannot locate itself properly inside my document and I don't know how to fix this.

Here is an example:

The current page which the table cannot locate itself properly in looks like as follows:

As you can see, there is too much space in between.

The previous and next pages look like this (left one is previous and the right one is the next page):

The documentclass is book and the table code is as follows:

\begin{center}
\begin{tabular}{ p{4cm} p{5cm}  p{6cm} }
%\hline
\cellcolor[gray]{0.9} Foo1 Title & \cellcolor[gray]{0.9}Foo2 Title & \cellcolor[gray]{0.9} Foo3 Title \\ \hline
Foo & Foo & Foo Bar bla bla bla...\\
Foo & Foo & Foo Bar bla bla bla...\\[5pt]

\hline
\end{tabular}

Table xxx: Foo Caption
\end{center}


Figures can do this very properly. If the space is not enough inside the page, then the image puts itself in the next page but the space in the current page filled with text or something else.

Any idea how can I can fix this problem so that tables can locate themselves properly?

-

The reason your figure environments behave the way you describe is that they are floats. The tabular environment is not a float.

To achieve the desired behaviour, you should wrap your tabular environments within the table environment, which does float.

\begin{table}[htbp]
\centering
\caption{Table caption goes here}
\begin{tabular}
...
\end{tabular}
\end{table}


Note that I've used \centering and not \begin{center}...\end{center} which adds extra vertical space.

You can change the htbp to suit your desired placement of floats. For further reading, have a look at

-
I am having one other problem with this one: I have also lstlisting blocks and they can sometimes go into two pages. If I follow the above style that u have suggested, table goes inside the lstlisting block if the listing block occupies two pages. Any chance I can fix this? –  tugberk May 9 '12 at 12:31
@tugberk that's really a separate question :) however, the placeins package provides the command \FloatBarrier which you could put before your lstlisting environments –  cmhughes May 9 '12 at 12:44
Thanks for the tip. I will try that. –  tugberk May 9 '12 at 12:47