[Tex/LaTex] Two tables side by side without losing caption

subcaptionsubfloatstables

I am trying to do two tables side by side. I have also benefited from the following thread, followed this answer and a couple of others.
Two tables side by side with minipage

My code is the following

\documentclass[preprint,12pt]{elsarticle}

\usepackage{lineno}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\graphicspath{{./Figures/}}
\usepackage{subfig}
\usepackage{floatrow}
\usepackage{dsfont}
\usepackage{float}
\usepackage[margin=1in]{geometry}

\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{algorithmic}
\usepackage{algorithm}               
\usepackage{slashbox}   
\usepackage{subcaption}   
\usepackage[showframe]{geometry}   

\def \hfillx {\hspace*{-\textwidth} \hfill} 


\begin{document}

\begin{table}[htb]
    \begin{subtable}{.5\textwidth}
    \tiny       
            \begin{tabular}{c c c c c}
        Cluster & MAE & MSE & E[over] & E[under] \\
        \hline
        1 & 0.31 &  0.48 & 2.00 & 1.38 \\
        2 & 0.32 &  0.49 & - & 1.36 \\
        3 & 0.26 &  0.44 & 1.57  & 1.50\\
        4 & 0.25 &  0.41 & 2.00  & 1.47 \\
        \hline            
        \end{tabular}    
        \caption{Caption1 that is long}
\label{subtable:pr1rnn}
 \end{subtable}%
   \begin{subtable}{.5\textwidth}
   \tiny
         \begin{tabular}{c c c c c}
        Cluster & MAE & MSE & E[over] & E[under] \\
        \hline
        1 & 0.40 &  0.59 & 1.50 & 1.28 \\
        2 & 0.51 &  0.82 & 1.44 & 1.44 \\
        3 & 0.53 &  0.77 & 1.31 & 1.31 \\
        4 & 0.41 &  0.62 & 1.88 & 1.28 \\
        \hline
        \end{tabular}
             \caption{Caption2 that is long}
    \label{subtable:pr2rfres}
    \end{subtable}
\end{table}

\end{document}

It gives me the following result:

enter image description here

I lose Caption 1 completely and the tables are not side-by-side. I tried to apply the answers from the related threads, but I can't seem to find where I am going wrong. How do I do them side by side?

Best Answer

assuming that you like to have sub captions for each table ....

\documentclass[preprint,12pt]{elsarticle}
\usepackage[showframe]{geometry}

\usepackage{subcaption}

\begin{document}

\begin{table}[htb]
\setlength\tabcolsep{0pt}
    \begin{subtable}{.5\textwidth}
    \centering
            \begin{tabular*}{0.9\linewidth}{@{\extracolsep{\fill}}c c c c c}
        Cluster & MAE & MSE & E[over] & E[under] \\
        \hline
        1 & 0.31 &  0.48 & 2.00 & 1.38 \\
        2 & 0.32 &  0.49 & - & 1.36 \\
        3 & 0.26 &  0.44 & 1.57  & 1.50\\
        4 & 0.25 &  0.41 & 2.00  & 1.47 \\
        \hline
        \end{tabular*}
        \caption{Caption1 that is long}
\label{subtable:pr1rnn}
 \end{subtable}%
   \begin{subtable}{.5\textwidth}
   \centering
           \begin{tabular*}{0.9\linewidth}{@{\extracolsep{\fill}}c c c c c}
        Cluster & MAE & MSE & E[over] & E[under] \\
        \hline
        1 & 0.40 &  0.59 & 1.50 & 1.28 \\
        2 & 0.51 &  0.82 & 1.44 & 1.44 \\
        3 & 0.53 &  0.77 & 1.31 & 1.31 \\
        4 & 0.41 &  0.62 & 1.88 & 1.28 \\
        \hline
        \end{tabular*}
             \caption{Caption2 that is long}
    \label{subtable:pr2rfres}
    \end{subtable}
\end{table}

\end{document}

gives

enter image description here

however, your mwe has problems with preamble. you cannot use in the subfig and subcaption in the same document. delete subfig package. and use the package geometry only once, if you really needed it.