3

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?

A Doe
  • 157
  • 1
    You're missing the second mandatory argument of subtable so your example code does not compile but instead show the error message: ! Missing number, treated as zero. The correct usage is \begin{subtable}{<width>} with <width>= {.5\textwidth} or any other length of your choice. – leandriis Jan 02 '19 at 19:47
  • I had the {.5\textwidth} command but when I typeset it, it doesn't see {.5\textwidth} as a command. It just prints .5 on the screen just before the tables. I edited the question accordingly. – A Doe Jan 02 '19 at 20:05
  • 1
    Please make a minimal working example (MWE) that represents the described behaviour. If I complete the code you posted with the documentclass article and a) the above described width, the desired layout is produced. If I complete the code b) without the width the above described error occurs. That said, I am not able to reproduce either of the outputs in your screenshots. – leandriis Jan 02 '19 at 20:09
  • I am editing the question accordingly now. – A Doe Jan 02 '19 at 20:11
  • I created a small file that only has the table and its output. All my packages are on the example now. – A Doe Jan 02 '19 at 20:18
  • Thanks for posting a complete example. The code results in the following error message ! Package subcaption Error: This package can't be used in cooperation (subcaption) with the subfig package. that tells you that the packages subcaption and subfig (that you both load) are incompatible with each other. As you currently use subcaption's syntax, simply remove the subfig package from your preamble. Apart from that and unrelated to the problem, you load the geometry package twice with different options which will also lead to an error. – leandriis Jan 02 '19 at 20:22
  • Thank you. At another page, I have two figures side by side. Removing subfig package affects them. Could I use these tables for subfigures? – A Doe Jan 02 '19 at 20:28
  • The subcaption package offers the subfigure environment for this. The usage is similar to the subtable environment. – leandriis Jan 02 '19 at 20:30

1 Answers1

2

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.

Zarko
  • 296,517