1

I am trying to put a table and a figure side by side with a subfigure environment. I followed some resources online and the most similar is the following: Join figure and table on the same page. In particular, I am referring to the third answer, however, I cannot apply that solution as in the document I already have some subfigure environments and thus I cannot use the subcaption package.

That the code example:

% !TeX spellcheck = en_US

\documentclass[]{article}
\usepackage{graphicx}
%\usepackage{subcaption}
\usepackage{changepage}
\usepackage{subfig}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{svg}

\begin{document}

    \begin{table}
        \centering
        \subfloat[Without cleaning.]{
            \begin{tabular}{lr}
                \toprule
                \textbf{Class Label} & \textbf{Frequency} \\ \midrule
                ON                    & 54.14\%            \\ 
                OFF                    & 41.43\%            \\ 
                WEAK                 & 3.33\%             \\ 
                S/OFF                & 1.1\%              \\ \bottomrule
            \end{tabular}
            \label{tab-freq-complete}}
        \quad \quad
        \subfloat[With cleaning.]{
            \begin{tabular}{lr}
                \toprule
                \textbf{Class Label} & \textbf{Frequency} \\ \midrule
                ON                    & 56.64\%            \\ 
                OFF                    & 43.36\%            \\ \bottomrule
            \end{tabular}
            \label{tab-freq-two}}
        \caption{Label frequencies across all the dataset.}
    \end{table}


    \begin{figure}
        \centering
        \subfloat[Accuracy and F1-scores.]{
            \begin{tabular}{lr}
                \toprule
                \textbf{Metric} & \textbf{Score} \\ \midrule
                Accuracy        & 56.64\%        \\ 
                F1 for ON       & 72.32\%        \\ 
                F1 for OFF      & 0\%            \\ \bottomrule
            \end{tabular}
            \label{res-ma}}
        \subfloat[Confusion matrix.]{           
            \includegraphics[width=0.4\linewidth]{../../data-visualization/confusion_matrix_ma} 

            \label{fig:ma}          

            }
        \caption{Majority classifier results.}
    \end{figure}

\end{document}

enter image description here

As you can see, with my code the figure is not vertically aligned with the table. I try also with that solution: Figures in subfigure not aligned, but it is not working.

2 Answers 2

2

Depending on the alingment you would like to achieve (bottom, centered, top), you could choose from one of the examples liste below. They all use the valign option of the adjustbox package:

\documentclass[]{article}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{booktabs}
\usepackage[export]{adjustbox}

\begin{document}


    \begin{figure}
        \centering
        \subfloat[Accuracy and F1-scores.]{
            \adjustbox{valign=b}{\begin{tabular}{lr}
                \toprule
                \textbf{Metric} & \textbf{Score} \\ \midrule
                Accuracy        & 56.64\%        \\ 
                F1 for ON       & 72.32\%        \\ 
                F1 for OFF      & 0\%            \\ \bottomrule
            \end{tabular}
            \label{res-ma}}}
        \subfloat[Confusion matrix.]{           
            \includegraphics[width=0.4\linewidth,valign=b]{example-image} 

            \label{fig:ma}          

            }
        \caption{Majority classifier results.}
    \end{figure}

    \begin{figure}
        \centering
        \subfloat[Accuracy and F1-scores.]{
            \adjustbox{valign=c}{\begin{tabular}{lr}
                \toprule
                \textbf{Metric} & \textbf{Score} \\ \midrule
                Accuracy        & 56.64\%        \\ 
                F1 for ON       & 72.32\%        \\ 
                F1 for OFF      & 0\%            \\ \bottomrule
            \end{tabular}
            \label{res-ma}}}
        \subfloat[Confusion matrix.]{           
            \includegraphics[width=0.4\linewidth,valign=c]{example-image} 

            \label{fig:ma}          

            }
        \caption{Majority classifier results.}
    \end{figure}

    \begin{figure}
        \centering
        \subfloat[Accuracy and F1-scores.]{
            \adjustbox{valign=t}{\begin{tabular}{lr}
                \toprule
                \textbf{Metric} & \textbf{Score} \\ \midrule
                Accuracy        & 56.64\%        \\ 
                F1 for ON       & 72.32\%        \\ 
                F1 for OFF      & 0\%            \\ \bottomrule
            \end{tabular}
            \label{res-ma}}}
        \subfloat[Confusion matrix.]{           
            \includegraphics[width=0.4\linewidth,valign=t]{example-image} 

            \label{fig:ma}          

            }
        \caption{Majority classifier results.}
    \end{figure}

\end{document}

enter image description here

1

I met a similar problem when trying to align a figure and a table side-by-side.

And here is a successful sample using \subfigure:

\usepackage{subcaption}

\begin{figure*}
\begin{subfigure}[b]{0.5\textwidth}
\centering
\includegraphics[scale=0.75]{xxx.pdf}
\caption{A Figure}
\label{fig}
\end{subfigure}%
\begin{subfigure}[b]{0.5\textwidth}
\centering
\begin{tabular}{c|c}
     xx & xx \\ 
     xx & xx \\ 
\end{tabular}
\caption{A Table}
\label{table}
\end{subfigure}
\caption{A Table & A Figure} 
\end{figure*}

Note that the align position in \begin{subfigure}[b]{0.5\textwidth} needs to be [b] instead of [t] for both subfigures. (I don't know why but in my case, when setting them to [t], the figure and the table cannot be correctly aligned).

1
  • Welcome to TeX.SE!
    – Mensch
    Sep 26, 2021 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .