[Tex/LaTex] Get a table and figure on the same page with captions & labels

captionscross-referencingfloatstables

Here's what I'm looking for: I need a table and a figure to stay on the same page, while keeping captions & labels fully functional. Both the table and the figure need their own caption and their own label.

The \afterpage command almost made it work (which I found in Join figure and table on the same page), but the labels to reference them in the text don't work anymore (no matter where I place my \afterpage environment. The table & figure together make up almost a full page, if that helps.

Best Answer

To guarantee that they will remain together, use either one minipage (if the object doesn't have to be treated as floating) or just one floating environment for both the figure and the table; the right type of caption can then be obtained, in both cases, using \captionof from the capt-of (or caption) packages; the example below shows both possibilities:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{capt-of}

\begin{document}

\noindent\begin{minipage}{\linewidth}
\centering
\includegraphics[width=6cm]{examplefigure}
\captionof{figure}{Example figure caption (non-floating)}
\label{fig:examplenf}

\captionof{table}{Example Table (non-floating)}
\begin{tabular}{ll}
\hline
column1a & column2a \\
column1b & column2b \\
column1c & column2c \\
\hline
\end{tabular}
\label{tab:examplenf}
\end{minipage} 

\begin{figure}
\centering
\includegraphics[width=6cm]{examplefigure}
\caption{Example figure caption (floating)}
\label{fig:examplefl}

\captionof{table}{Example Table (floating)}
\begin{tabular}{ll}
\hline
column1a & column2a \\
column1b & column2b \\
column1c & column2c \\
\hline
\end{tabular}
\label{tab:examplefl}
\end{figure} 

\end{document}

enter image description here