14

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.

17

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

  • This did the trick (floating one), thanks! I turned it around though, and just used captof for the figure and put it all in a table environment (made a little more sense content-wise to get the table above the figure but I forgot to say that). – FlyLo Apr 28 '13 at 11:10
1

You can do this by setting one float on the top by using the [t] option and another one on the bottom [b], as shown in the example:

\begin{figure}[t]
   \centering
   \includegraphics[width=\textwidth]{examplefigure}
   \caption{Example figure caption}
   \label{fig:example}
\end{figure}

\begin{table}[b]
    \caption{Example Table}
    \begin{tabular}{ll}
    hello & world\\
    \end{tabular}
    \label{tab:example}
\end{table} 

Or, as stated in these two questions:

Keeping tables/figures close to where they are mentioned

Force figure placement in text

You can force them to be where you want them by using the float package along with the [H] option. However, this is not advisable.

  • I tried that but it didn't work.. – FlyLo Apr 28 '13 at 11:07
  • I can confirm that this will not always work even though I'd expect it to (that's what started my search actually). – Matthias Kauer Sep 10 '15 at 0:53
0

I had trouble getting the captions to work with the solution previously posted by @Gonzalo Medina. Specifically, my captions were in the form:

\caption[short caption for table of contents]{long caption underneath figure}

The \captionof command doesn't take a short caption. When using \caption instead, my table was captioned as a figure.

I was able to solve the problem with an alternative solution, using the afterpage package, as:

\usepackage{afterpage}

\afterpage{
\begin{table}
  \centering
  \caption[short caption]{long caption}
  \label{table_label}
    \begin{tabular}{ll}
       % table material
    \end{tabular}
\end{table}
\begin{figure}
  \begin{center}
    \includegraphics[scale=0.5]{figure_file}
    \caption[short caption]{long caption}
    \label{figure_label}
  \end{center}
\end{figure}
} % <-- last curly brace ends \afterpage environment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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