5

A very similar question has been asked before:

Table and Figure side-by-side with independent captions

I'm trying to accomplish this, but while using the subfig package. The image below depicts almost what I want, but as you can see, the table is not aligned correctly:

enter image description here

The TeX I'm working with is:

\begin{figure}[h]
    \caption{A figure and a table, side-by-side}
    \centering
    \subfloat[A figure left of a table]{
        \rule{6.4cm}{3.6cm}
    }
    \subfloat[A table right of a figure]{
        \begin{tabular}{cc}\hline
            Table head & Table head \\ \hline
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\ \hline
        \end{tabular}
    }
\end{figure}

I've tried throwing a few minipages into the mix, but to no avail. How can I align the top of the table to the top of the figure?

David Carlisle
  • 757,742

3 Answers3

4

The caption package version you use is too old to work fine with the actual version of hyperref.

That's explaining both issues, the wired behaviour when used with the subfig package, and the error you get when using the subcaption package. So updating the caption package (or even better: your whole TeX system) should help.

(Current versions of the hyperref package need at least caption v3.1m from 2010/01/09)

Addendum: The very next version 1.1b of the subcaption package will typeout a more reasonable error message.

  • 1
    Thanks, this instantly resolved it! I opened up the MiKTeX Package Manager and updated a few packages, and it's working perfectly now. – Paul Lammertsma Sep 16 '11 at 15:46
  • I've noticed I do have to shuffle the imports around, as cmhughes mentioned. Some orderings reproduce the original problem, but I'm delighted that this problem is resolved. – Paul Lammertsma Sep 16 '11 at 15:54
3

I had the exact same problem, the following worked for me:

\usepackage{subfigure}

\begin{figure}[h]
    \caption{A figure and a table, side-by-side}
    \centering
    \subfloat[A figure left of a table]{
        \raisebox{-.5\height}{\rule{6.4cm}{3.6cm}}
    }
    \subfloat[A table right of a figure]{
        \begin{tabular}{cc}\hline
            Table head & Table head \\ \hline
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\ \hline
        \end{tabular}
    }
\end{figure}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
Joost
  • 31
0

My specific problem appears to be caused by the hyperref package. Removing \usepackage{hyperref} resolved the layout problem, although I don't know exactly what was causing it.

With subfig, I get the following warning:

\caption will not be redefined since it's already on input line 15

\documentclass[a4paper]{article}

\usepackage{graphicx}
\usepackage{subfig}

%% Enabling this package appears to cause the problem
% \usepackage{hyperref}

\begin{document}

\begin{figure}[h]
    \caption{A figure and a table, side-by-side}
    \centering
    \subfloat[A figure left of a table]{
        \rule{6.4cm}{3.6cm}  %% <- line 15
    }
    \subfloat[A table right of a figure]{
        \begin{tabular}{cc}\hline
            Table head & Table head \\ \hline
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\ \hline
        \end{tabular}
    }
\end{figure}

\end{document}

With subcaption, I get the following compilation error:

Package caption Error: Something's wrong--perhaps a missing \caption on input line 30 in "test2.tex"

\documentclass[a4paper]{article}

\usepackage{graphicx,caption,subcaption}

%% Enabling this causes problems
%\usepackage{hyperref}

\begin{document}

\begin{figure}
    \centering
    \subcaptionbox{}{
        \rule{6.4cm}{3.6cm}
    }
    \hfill
    \subcaptionbox{}{
        \begin{tabular}{cc}\hline
            Table head & Table head \\ \hline
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\
            Some values & Some values \\ \hline
        \end{tabular}
    }
    \caption{Test images}
\end{figure}

\end{document}  %% <- line 30
David Carlisle
  • 757,742
  • Hmmm... adding hyperref to your code snippet (and loading subfig) doesn't allow me to reproduce the strange behaviour mentioned, so there must be something else going on. Can you complete your code to a proper minimal working example illustrating the problem? – Gonzalo Medina Sep 16 '11 at 13:42
  • Would another approach be useful for you? I mean, using the subcaption package instead of subfig? – Gonzalo Medina Sep 16 '11 at 13:44
  • 1
    That was really not obvious! An MWE should be included at all times... – Count Zero Sep 16 '11 at 13:44
  • Nop. With the code you just posted I still can't reproduce the problem (after uncommenting the hyperref line). – Gonzalo Medina Sep 16 '11 at 13:48
  • I've added the code I'm working with. If it's relevant, I'm using MiKTeX 2.9. Gonzalo, I've tried subcaption as well, which causes compile errors. I'll update the answer. – Paul Lammertsma Sep 16 '11 at 13:52
  • I'm completely puzzled now. Could it be that either my hyperref package or my entire installation here is botched? – Paul Lammertsma Sep 16 '11 at 13:56
  • Can you add the line \listfiles before, \begin{document} and post the output/ – cmhughes Sep 16 '11 at 14:24
  • I've pasted the file list here: http://pastebin.com/JgvWsiWs – Paul Lammertsma Sep 16 '11 at 14:38
  • 1
    I haven't been able to reproduce the unwanted output. I assume the actual document is a lot more complicated- be careful to load the hyperref package last- see this post: http://tex.stackexchange.com/questions/1863/which-packages-should-be-loaded-after-hyperref-instead-of-before – cmhughes Sep 16 '11 at 15:05
  • Yes, that works! Including hyperref as the first include in the first example resolves it. For the subcaption example, however, I still get the error. – Paul Lammertsma Sep 16 '11 at 15:35