[Tex/LaTex] How to assign captions for two tables in LaTeX

captionsfloatspdftextables

I want to put two tables side by side and my I have included a sample code below. How can we put separate captions for two tables in LaTeX included in one tabular environment?

\documentclass{article}
\begin{document}

\begin{tabular}{ll}
\begin{tabular}{ccc}
A & B & C \\
\cline{1-3}
1 & 2 & 3 \\
\cline{1-3}
C & B & A \\
\end{tabular}
&
\begin{tabular}{ccc}
D & E & F \\
\cline{1-3}
4 & 5 & 6 \\
\cline{1-3}
F & E & D \\
\end{tabular}
\end{tabular}
\end{document}

Best Answer

using tabularx for outer table and use table environment:

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
    \begin{table}[htb]
\begin{tabularx}{\linewidth}{CC}
\caption{The first table}
    \begin{tabular}{ccc}
    A & B & C \\
    \cline{1-3}
    1 & 2 & 3 \\
    \cline{1-3}
    C & B & A \\
    \end{tabular}
&
\caption{The second table}
    \begin{tabular}{ccc}
    D & E & F \\
    \cline{1-3}
    4 & 5 & 6 \\
    \cline{1-3}
    F & E & D \\
    \end{tabular}
\end{tabularx}
    \end{table}
\end{document}

enter image description here

addendum: in cases, when one table is wider than other and wider than the column width, you can manually accommodate columns width of the outer table. for this is more appropriate to use tabular* than tabularx table environment:

\documentclass{article}
\usepackage[skip=1ex]{caption}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\usepackage{lipsum}

\begin{document}
\lipsum[1]
    \begin{table*} 
\begin{tabular*}{\linewidth}{C{\dimexpr0.3\linewidth-2\tabcolsep}
                             C{\dimexpr0.7\linewidth-2\tabcolsep}}
\caption{The first table}
    \begin{tabular}{ccc}
    \hline
    A & B & C \\
    \hline
    1 & 2 & 3 \\
    C & B & A \\
    \hline
    \end{tabular}
&
\caption{The second table}
    \begin{tabular}{*{14}{c}}
    \hline
    D & E & F & G & H & I & J & D & E & F & G & H & I & J   \\
    \hline
    4 & 5 & 6 & 7 & 8 & 9 & 0 & 4 & 5 & 6 & 7 & 8 & 9 & 0   \\
    J & I & H & G & F & E & D & J & I & H & G & F & E & D   \\
    \hline
    \end{tabular}
\end{tabular*}
    \end{table*}
\lipsum\lipsum
\end{document}

which gives:

enter image description here