Suppose I have two tables that don't have many columns (3, for example). How can I position them side by side?
7 Answers
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
.
-
4+1 to the the first option that is very simple and suited very well for me. Commented May 13, 2015 at 1:38
-
By the first option,
just put two tabular environments side by side
, they aren't positioned next to each other, if I'm correct? Commented Feb 24, 2017 at 22:33 -
1
-
1As @MusséRedi mentioned first solution does not work (at least for me). Solution using table and parbox worked fine. Commented Apr 17, 2022 at 4:37
-
It blows my mind that for once, there is a trivial solution (number 1) for a problem in LaTeX that works surprisingly well. Commented Jun 10 at 11:02
If you want two tabulars
next to each other in a (floating) table
environment, then you can use minipage
s
\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
\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
.
-
Why would I want subtables vs tabulars? Of the two methods you describe in your answer, which is better (and why) ? Commented Apr 18, 2013 at 6:51
-
1On a second glance: is the main (visible) difference between the two methods the way the "sub"-captions look (e.g., "Table 2" vs. "(a)") ? Commented Apr 18, 2013 at 6:57
-
2@nuttyaboutnatty yes, your second comment is accurate. It depends how you want the captions (and references) to look. It's up to you which you think is better :)– cmhughesCommented Apr 18, 2013 at 17:00
-
2Is there a way to have the tables start at the same height if they are of different lengths? Commented Aug 25, 2013 at 13:15
-
@PeterM you could use
savebox
- see my answer to Reasons for saving a box?– cmhughesCommented Aug 25, 2013 at 15:36
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}
-
23I'd appreciate a comment with the down vote; I might then learn something about what makes this a bad solution.– vandenCommented Sep 7, 2010 at 4: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., decenttoc
handling), it's probably not too bad. Commented Sep 7, 2010 at 4:42 -
1
-
1Does this not work? My first guess was to solve it in this way... so how come it got a downvote?– JohanCommented Sep 7, 2010 at 5:03
-
9I 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. Commented Sep 7, 2010 at 10:19
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.
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, withbooktabs
. For your original layout I added some vertical padding to the rows, with themakecell
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}
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
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}
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}
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?