[Tex/LaTex] Table and figure side by side with minipages

floatsminipage

How do I put a table and a figure side by side with minipage?
I have made it work, but then the figure's caption is table, because table was the surrounding tag.

\begin{table}[ht]
\begin{minipage}[b]{0.45\linewidth}
\centering
\begin{tabular}{ | l | r | r | r |}
    \hline
    Student & Hours/week & Grade \\ \hline \hline   
    Ada Lovelace & 2 & A \\ \hline
    Linus Thorvalds & 8 & A \\ \hline
    Bruce Willis & 12 & F \\ \hline
    Richard Stallman & 10 & B \\ \hline
    Grace Hopper & 12 & A \\ \hline
    Alan Turing & 8 & C \\ \hline
    Bill Gates & 6 & D \\ \hline
    Steve Jobs & 4 & E \\ \hline
   \end{tabular}
    \caption{Student Database}
    \label{table:student}
\end{minipage}
\end{table}

\begin{figure}[ht]
\begin{minipage}[b]{0.45\linewidth}
\centering
\includegraphics[width=40mm]{figures/studentdatabasegraph.png}
\caption{2-D scatterplot of the Student Database}
\label{ }
\end{minipage}
\end{figure}

Best Answer

Table and figure caption can be put in the same floating environment, but there is a big but: LaTeX places figures and tables independently and does not synchronize the numbering of the figure caption in the table float with the other figures. Therefore, the numbering of the figures can be out of order.

Example file:

\documentclass{article}
\usepackage{graphicx}
\usepackage{capt-of}% or \usepackage{caption}

\begin{document}

\begin{table}[ht]
\begin{minipage}[b]{0.56\linewidth}
\centering
\begin{tabular}{ | l | r | r | r |}
    \hline
    Student & Hours/week & Grade \\ \hline \hline
    Ada Lovelace & 2 & A \\ \hline
    Linus Thorvalds & 8 & A \\ \hline
    Bruce Willis & 12 & F \\ \hline
    Richard Stallman & 10 & B \\ \hline
    Grace Hopper & 12 & A \\ \hline
    Alan Turing & 8 & C \\ \hline
    Bill Gates & 6 & D \\ \hline
    Steve Jobs & 4 & E \\ \hline
   \end{tabular}
    \caption{Student Database}
    \label{table:student}
\end{minipage}\hfill
\begin{minipage}[b]{0.4\linewidth}
\centering
\includegraphics[width=40mm]{example-image}
\captionof{figure}{2-D scatterplot of the Student Database}
\label{fig:image}
\end{minipage}
\end{table}
\end{document}

Result

Nicer table

\documentclass{article}
\usepackage{graphicx}
\usepackage{capt-of}% or \usepackage{caption}
\usepackage{booktabs}
\usepackage{varwidth}

\begin{document}
\begin{table}[ht]
  \begin{varwidth}[b]{0.6\linewidth}
    \centering
    \begin{tabular}{ l r r r }
      \toprule
      Student & Hours/week & Grade \\
      \midrule
      Ada Lovelace & 2 & A \\
      Linus Thorvalds & 8 & A \\
      Bruce Willis & 12 & F \\
      Richard Stallman & 10 & B \\
      Grace Hopper & 12 & A \\
      Alan Turing & 8 & C \\
      Bill Gates & 6 & D \\
      Steve Jobs & 4 & E \\
      \bottomrule
    \end{tabular}
    \caption{Student Database}
    \label{table:student}
  \end{varwidth}%
  \hfill
  \begin{minipage}[b]{0.4\linewidth}
    \centering
    \includegraphics[width=40mm]{example-image}
    \captionof{figure}{2-D scatterplot of the Student Database}
    \label{fig:image}
  \end{minipage}
\end{table}
\end{document}

Result

Different alignment

\documentclass{article}
\usepackage{graphicx}
\usepackage{capt-of}% or \usepackage{caption}
\usepackage{booktabs}
\usepackage{varwidth}
\newsavebox\tmpbox

\begin{document}
\begin{table}[ht]
  \sbox\tmpbox{%
    \begin{tabular}[b]{ l r r r }
      \toprule
      Student & Hours/week & Grade \\
      \midrule
      Ada Lovelace & 2 & A \\
      Linus Thorvalds & 8 & A \\
      Bruce Willis & 12 & F \\
      Richard Stallman & 10 & B \\
      Grace Hopper & 12 & A \\
      Alan Turing & 8 & C \\
      Bill Gates & 6 & D \\
      Steve Jobs & 4 & E \\
      \bottomrule
    \end{tabular}%
  }%
  \renewcommand*{\arraystretch}{0}
  \begin{tabular*}{\linewidth}{@{\extracolsep\fill}p{\wd\tmpbox}p{40mm}@{}}
    \usebox\tmpbox &
    \includegraphics[width=\linewidth]{example-image} \\
    \caption{Student Database}
    \label{table:student}
    &
    \captionof{figure}{2-D scatterplot of the Student Database}
    \label{fig:image}
  \end{tabular*}
\end{table}
\end{document}

Result