236

Suppose I have two tables that don't have many columns (3, for example). How can I position them side by side?

lockstep
  • 250,273
bada
  • 4,287
  • 27
    This question is quite popular, but the answers seem to have quite some overlap, but lack a coherent comparsion of the pros & cons of the different techniques used: minipage, subfig & subfloat, \quad (?), parbox, subcaption & subtable. Is there a consensus of what's considered best, if both "subtables" also have some basic (not a fully-fledged one with the "Table 2.1"-bit) caption? – nutty about natty Apr 18 '13 at 05:59

7 Answers7

213

Just put two tabular environments side by side. Add spacing as desired.

\begin{tabular}{ccc}
\hline
a&b&c\\
\hline
\end{tabular}
\quad
\begin{tabular}{ccc}
\hline
d&e&f\\
\hline
\end{tabular}

If you want to use subfig because you want them to have separate captions, then that is simple as well.

\subfloat[caption]{\begin{tabular}{...}...\end{tabular}}
\quad
\subfloat[caption]{\begin{tabular}{...}...\end{tabular}}

If you want two tables that are independent, and thus don't want to use \subfloat, you can use \parbox.

\begin{table}
\parbox{.45\linewidth}{
\centering
\begin{tabular}{ccc}
\hline
a&b&c\\
\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{ccc}
\hline
d&e&f\\
\hline
\end{tabular}
\caption{Bar}
}
\end{table}

This is basically the same as before just that I've centered each tabular in a \parbox with an included caption and wrapped the whole thing in a table.

dopexxx
  • 231
  • 2
  • 6
TH.
  • 62,639
99

If you want two tabulars next to each other in a (floating) table environment, then you can use minipages

screenshot

\begin{table}[!htb]
    \caption{Global caption}
    \begin{minipage}{.5\linewidth}
      \caption{}
      \centering
        \begin{tabular}{ll}
            1 & 2
        \end{tabular}
    \end{minipage}%
    \begin{minipage}{.5\linewidth}
      \centering
        \caption{}
        \begin{tabular}{ll}
            3 & 4
        \end{tabular}
    \end{minipage} 
\end{table}

Loading the caption package will allow easy customization, and will remove the : when the caption is empty.

If you'd like 'sub'tables, then you can use the subcaption package which introduces the subtable environment (and a lot more); the subtable environment takes the same mandatory and optional arguments as the minipage

screenshot

\documentclass{article}
\usepackage{subcaption}
\begin{document}

\begin{table}[!htb]
    \caption{Global caption}
    \begin{subtable}{.5\linewidth}
      \centering
        \caption{}
        \begin{tabular}{ll}
            1 & 2
        \end{tabular}
    \end{subtable}%
    \begin{subtable}{.5\linewidth}
      \centering
        \caption{}
        \begin{tabular}{ll}
            3 & 4
        \end{tabular}
    \end{subtable} 
\end{table}

\end{document}

Note that in both cases I've used a % at the end of the environment to stop overfull hboxes from the additional space added by the minipage.

cmhughes
  • 100,947
17

I don't know that this is the best way, but you could make a 2-column table with a single row. In each cell of the table, wrap one of your intended tables in a minipage environment. Thus:

\documentclass{article}

\begin{document}

\begin{tabular}{cc}
    \begin{minipage}{.5\linewidth}
        \begin{tabular}{ll}
            1 & 2
        \end{tabular}
    \end{minipage} &

    \begin{minipage}{.5\linewidth}
        \begin{tabular}{ll}
            3 & 4
        \end{tabular}
    \end{minipage} 
\end{tabular}

\end{document}
vanden
  • 30,891
  • 23
  • 67
  • 87
  • 23
    I'd appreciate a comment with the down vote; I might then learn something about what makes this a bad solution. – vanden Sep 07 '10 at 04:23
  • Er, it wasn't me. Fyi, your solution's more or less the same as the one set out in the subfig documentation section "Do You Need This Package?" So, except for the bells and whistles (e.g., decent toc handling), it's probably not too bad. – Geoffrey Jones Sep 07 '10 at 04:42
  • @Geoffrey: I did not assume it was you, nor does my comment suggest that I did so assume. (By the same token, yours doesn't suggest that you thought I did.) Thanks for the added info from the subfig docs. – vanden Sep 07 '10 at 04:45
  • He-he. Didn't think you did, just wanted an excuse so I could comment that your solution wasn't really all that bad :) – Geoffrey Jones Sep 07 '10 at 04:49
  • 1
    Sticking it in a minipage makes no sense to me at all. – TH. Sep 07 '10 at 05:01
  • 1
    Does this not work? My first guess was to solve it in this way... so how come it got a downvote? – Johan Sep 07 '10 at 05:03
  • 9
    I don't know why it got a downvote, but for my eyes the code has some faults: It gives an overful hbox message as the \parindent is not taken into account. It doesn't do what it probably wants to do: the tabular are not centered in the line as they are left aligned in the minipages. Also a minipage + an external tabular looks like an overkill (one at least could certainly be dropped) and makes the code difficult to read and changed. – Ulrike Fischer Sep 07 '10 at 10:19
  • @TH.: sticking single tabulars in a minipage is indeed not very useful. However, if you also have \captions then this is IMHO the way to go, if both tables are separate and not sub-tables. I prefer minipages before the very similar \parboxes because the first can e.g. contain verbatim material. – Martin Scharrer Jul 03 '11 at 21:42
  • Where should I add \caption? @vanden – alper May 27 '19 at 09:00
14

Ceteris paribus, you might consider the subfig package. It is suitable for subtables and subfigures and combinations of these. For myself, I've managed much mileage from it.

10

As the solution for tables (not subtables) side by side with the floatrow package is not mentioned here, I'll add it under the form of an answer to this duplicate question.

You can do that easily with the floatrow package. I took the opportunity to propose another layout, without vertical rules, with booktabs. For your original layout I added some vertical padding to the rows, with the makecell package.

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier, erewhon}
\usepackage{geometry}
\usepackage{array, caption, floatrow, tabularx, makecell, booktabs}%
\captionsetup{labelfont = sc}
\setcellgapes{3pt}

\begin{document}

\begin{table}[!htb]
  \floatsetup{floatrowsep=qquad, captionskip=4pt}
  \begin{floatrow}[2]
    \makegapedcells
    \ttabbox%
    {\begin{tabularx}{0.45\textwidth}{|c| *{2}{>{\centering\arraybackslash}X|}}
      \hline
      & Sensibilità [$\%$] & Specificità [$\%$] \\
      \hline
      $ϵ'$ & 53.492 & 77.906 \\ \hline
      $ϵ''$ & 46.753 & 61.587\\
      \hline
      \end{tabularx}}
    {\caption[Valori medi]{valori medi}
      \label{val1}}
    \hfill%
    \ttabbox%
    {\begin{tabularx}{0.45\textwidth}{|c| *{2}{>{\centering\arraybackslash}X|}}
      \hline
      & Sensibilità [$\%$] & Specificità [$\%$]\\
      \hline
      $ϵ'$ & 60\hphantom{.000} & 80.487 \\ \hline
      $ϵ''$ & 54.545 & 67.777\\
      \hline
      \end{tabularx}}
    {\caption[Valori medi]{valori medi}
      \label{val2}}
  \end{floatrow}
  \vspace*{1cm}
  \begin{floatrow}[2]
    \ttabbox%
    {\begin{tabularx}{0.45\textwidth}{c *{2}{>{\centering\arraybackslash}X}}
      \toprule
      & Sensibilità [$\%$] & Specificità [$\%$] \\
      \cmidrule(lr){2-2}\cmidrule(lr){3-3}
      $ϵ'$ & 53.492 & 77.906 \\
      \addlinespace
      $ϵ''$ & 46.753 & 61.587\\
      \bottomrule
      \end{tabularx}}
    {\caption[Valori medi]{valori medi}
      \label{val1a}}
    \hfill%
    \ttabbox%
    {\begin{tabularx}{0.45\textwidth}{c*{2}{>{\centering\arraybackslash}X}}
      \toprule
      & Sensibilità [$\%$] & Specificità [$\%$]\\
      \cmidrule(lr){2-2}\cmidrule(lr){3-3}
      $ϵ'$ & 60\hphantom{.000} & 80.487 \\
      \addlinespace
      $ϵ''$ & 54.545 & 67.777\\
      \bottomrule
      \end{tabularx}}
    {\caption[Valori medi]{valori medi}
      \label{val2a}}
  \end{floatrow}
\end{table}%

\end{document} 

enter image description here

Bernard
  • 271,350
8

2 tables side by side, with (or without) caption, without new packages: Just imbricate 2 tables! It is perfectly fine to have a table as a "regular" entry of another table

enter image description here

The top level table has 2 columns and 2 rows. The top row is the caption, the bottom row contains the "real" tables that you want.

See how \\ and & are used after \end{tabular}

\begin{tabular}{ cc }   % top level tables, with 2 columns
caption 1 & caption 2 \\  
% leftmost table of the top level table
\begin{tabular}{ |c|c|c| } 
\hline
a & b & c \\
a & b & c \\
\hline
\end{tabular} &  % starting rightmost sub table
% table 2
\begin{tabular}{ |c|c|c| } 
\hline
d & e & f \\
d & e & f \\
\hline
\end{tabular} \\
\end{tabular}
Thomas
  • 199
0

I will add another answer to this question because I think this solution is more simple, for simple usages like putting two table side by side.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[latin1]{inputenc}

\title{Table side by side} \author{Vincenzo Palazzo} \date{02/04/2023}

\begin{document}

\begin{table}[!htb] \caption{Input and output transaction struct of Bitcoin Code.} \begin{center} \subtable{ \begin{tabular}{|c|c|} \hline \multicolumn{2}{|c|}{\textbf{TransactionInput}} \ \hline \hline \multicolumn{1}{|c|}{Type} & \multicolumn{1}{c|}{Name} \ \hline Outpoint & outpoint \ \hline CScript & scriptSig \ \hline $uint32_t$ & nSequence \ \hline \end{tabular} \centering } \subtable{ \centering \begin{tabular}{|c|c|} \hline \multicolumn{2}{|c|}{\textbf{Outpoint}} \ \hline \hline \multicolumn{1}{|c|}{Type} & \multicolumn{1}{c|}{Name} \ \hline $uint256$ & hash \ \hline $uint32_t$ & index \ \hline \end{tabular}
} \end{center} \label{tab:tx_in_and_out} \end{table}

\end{document}