# Captions and labels for my tables, listings, images

I'm writing my master thesis and I have a lot of short tables, listings, etc. in the text.

I would use figures for them like this:

\begin{figure}[h!]
\begin{lstlisting}[mathescape]
... my listing here ...
\end{lstlisting}
\caption{... caption ...}
\label{... label ...}
\end{figure}

\begin{figure}[h!]
\begin{tabular}{|l|l|}
... my table here ...
\end{tabular}
\caption{... caption ...}
\label{... label ...}
\end{figure}


etc, but the fact that LaTeX moves them to another page (even though they are sometimes very short) makes the text hardly readable.

I would need LaTeX to keep them at exactly the same spot (if they fit there) or at the top of the next page (otherwise). It is my understanding that I should not use figures in this case. But, on the other hand, I would like to keep the captions, labels and numberings for them to still be able to address them.

How do I do that?

-

I would need LaTeX to keep them at exactly the same spot (if they fit there) or at the top of the next page (otherwise).

Given that you have many float objects, that they are usually quite small, and that they need to stay as close as possible to the respective callouts, you may want to load the float package and attach the [H] location specifier (instead of [h!]) to each figure or table environment for which the requirements you've described apply.

-

Part of package caption is the macro

\captionof{environment}[short]{long caption}


For example an table you can left out environment table and use

\captionof{table}{long caption title}


Advantage: no floating, but you have to control whether there is enouph space for your table. Same for figures.

Your table snippet:

\begin{tabular}{|l|l|}
... my table here ...
\end{tabular}
\captionof{table}{... caption ...}
\label{tab:label}

-
I'm not very familiar with the captionof macro. When you say that one has to check manually whether there's enough space remaining on the page for the quasi-float, does this mean one has to insert \clearpage instructions by hand at times or else risk getting seriously overfull pages? –  Mico Aug 24 '13 at 12:18
@Mico It depends on the space the table or figure needs. At the place you use the environment must enouph space to place the hole figure/table with caption. –  Kurt Aug 24 '13 at 12:23
Thanks for providing this clarification. So, put differently, an overfull page is created if there isn't enough space to fit the figure/table? –  Mico Aug 24 '13 at 12:34
@Mico Yes, it would result in an overful page. –  Kurt Aug 24 '13 at 12:56
Is there any advantage, then, of using the \captionof approach instead of the [H] approach of the float package (given that the latter won't create overfull pages)? –  Mico Aug 24 '13 at 12:58