0

This question already has an answer here:

I am trying to get a figure and a table next to eachother but they aren't staying together. In my document the figure ends up somewehere else but the table stays where I want it to be, as if the h in the \begin{figure}[h] isnt working.

The caption for the table has to be above the table and the caption for the figure has to be under the figure.

My code as of now:

\begin{figure}[h]

\parbox{7cm}{

\centering

 \includegraphics[height=3.5cm, width=6.5cm{Kapitler/Metode/Linersammenhang.png}

\caption{Lineærsammenhæng mellem erstatningsgrad og cementindhold}

\label{fig:linersammenhang}

}

\begin{table}[h]

\parbox{7cm}{

\centering

\caption{Tabel over cementindhold for hver erstatningsgrad udregnet vha. den lineærsammenhæng}

\label{tab:cementindhold}

\begin{tabular}{cc}

\hline

\begin{tabular}[c]{@{}c@{}}Erstatningsgrad\\ {[}\%{]}\end{tabular} & \begin{tabular}[c]{@{}c@{}}Cementindhold\\ $[\frac{kg}{m^3}]$\end{tabular} \\ 
\hline

0 (REF)                                                            & 222,4                                                                      \\
10                                                                 & 227,8                                                                      \\
30                                                                 & 238,8                                                                      \\
100                                                                & 277,0                                                                      \\ \hline

\end{tabular}

}

\end{table}

\end{figure}

Hope someone is able to find a solution :)

marked as duplicate by Bobyandbob, TeXnician, egreg captions Jun 3 '18 at 13:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SX! It's definitely not a good idea to nest floats. Could you please show the result you expect (maybe a sketch)? – TeXnician Jun 3 '18 at 12:33
1

like this (because i liked example-image-duck very much :-) )?

enter image description here

with help of the packages siunitx, makecell and caption:

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{booktabs, makecell}

\begin{document}
    \begin{figure}
\begin{minipage}{0.4\linewidth}
  \includegraphics[height=3.5cm, width=6.5cm]{example-image-duck}
\caption{Lineærsammenhæng mellem erstatningsgrad og cementindhold}
\label{fig:linersammenhang}
\end{minipage}
\hfill
\begin{minipage}{0.4\linewidth}
\setlength\tabcolsep{0pt}
\captionof{table}{Tabel over cementindhold for hver erstatningsgrad udregnet vha. den lineærsammenhæng}
\label{tab:cementindhold}
   \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}
                    S[table-format=3.0,
                      table-space-text-post={\ (REF)}
                      ]
                    S[table-format=3.1]
                   }
    \toprule
{\makecell{Erstatningsgrad\\ \%}}
                & {\makecell{Cementindhold\\ \si{\kilo\gram\per\cubic\metre}}} \\
    \midrule
  0 {\ (REF)}   & 222,4 \\
 10             & 227,8 \\
 30             & 238,8 \\
100             & 277,0 \\
    \bottomrule
  \end{tabular*}
\end{minipage}
\end{figure}
\end{document}
0

You could do the following (first block of the code). But this is non-ideal, because by default, figure and table can overtake each other (so a later figure might be displayed earlier in the document). Therefore the tabular contained in the figure environment might be displayed earlier than a later table (or later than an earlier used table). Therefore the second option seems to be much better practise.

\documentclass[]{article}

\usepackage[utf8]{inputenc}
\usepackage{siunitx}
\usepackage[]{graphicx}
\usepackage{booktabs}
\newcommand*\onecell[2][c]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document}
Bad idea: \ref{fig:linersammenhang} and \ref{tab:cementindhold}
\begin{figure}
  \centering
  \includegraphics[height=3.5cm, width=6.5cm]{example-image-duck}
  \caption
    {%
      Lineærsammenhæng mellem erstatningsgrad og
      cementindhold\label{fig:linersammenhang}%
    }
  \expandafter\def\csname @captype\endcsname{table}% changes the caption type
  \caption
    {%
      Tabel over cementindhold for hver erstatningsgrad udregnet vha. den
      lineærsammenhæng\label{tab:cementindhold}%
    }
  \begin{tabular}{SS}
    \toprule
    {\onecell{Erstatningsgrad\\\si{\percent}}} &
    {\onecell{Cementindhold\\\si{\kilo\gram\per\cubic\metre}}} \\
  \midrule
  0 {(REF)} & 222,4 \\
  10      & 227,8 \\
  30      & 238,8 \\
  100     & 277,0 \\
  \bottomrule
  \end{tabular}
\end{figure}

Better: \ref{fig:linersammenhang2} and \ref{tab:cementindhold2}
\begin{figure}
  \centering
  \includegraphics[height=3.5cm, width=6.5cm]{example-image-duck}
  \caption
    {%
      Lineærsammenhæng mellem erstatningsgrad og
      cementindhold\label{fig:linersammenhang2}%
    }
\end{figure}
\begin{table}
  \centering
  \caption
  {%
    Tabel over cementindhold for hver erstatningsgrad udregnet vha. den
    lineærsammenhæng\label{tab:cementindhold2}%
    }

  \begin{tabular}{SS}
    \toprule
    {\onecell{Erstatningsgrad\\\si{\percent}}} &
    {\onecell{Cementindhold\\\si{\kilo\gram\per\cubic\metre}}} \\
  \midrule
  0 {(REF)} & 222,4 \\
  10      & 227,8 \\
  30      & 238,8 \\
  100     & 277,0 \\
  \bottomrule
  \end{tabular}
\end{table}
\end{document}

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