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