2

I need to have a table with a caption on top. Within the table there should be four small tables placed next to each other as they are currently placed.

\documentclass[10pt]{article}
\textwidth= 5.00in
\textheight= 7.4in
\topmargin = 30pt
\evensidemargin=0pt
\oddsidemargin=55pt
\headsep=17pt
\parskip=.5pt
\parindent=12pt


\usepackage{tabularx}
\usepackage{amssymb,latexsym,amsmath,epsfig,amsthm,enumerate} %% Add other packages as necessary
\newcolumntype{P}[1]{%
  p{\dimexpr#1\linewidth-2\tabcolsep-1.3333\arrayrulewidth}}

\begin{document}

Let us consider a few examples. The examples are shown in the tables below. The descriptions have to be below each table.

\footnotesize

\begin{tabular}{|P{0.04}P{0.04}P{0.04}P{0.045}|P{0.04}|}
\hline
$a_1$ & $a_2$ & $a_3$ & $a_4$  &  fg \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
\end{tabular}
\quad
\begin{tabular}{|P{0.04}P{0.04}P{0.04}P{0.045}|P{0.04}|}
\hline
$a_1$ & $a_2$ & $a_3$ & $a_4$  &  fg \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
\end{tabular}
\quad
\begin{tabular}{|P{0.04}P{0.04}P{0.04}P{0.045}|P{0.04}|}
\hline
$a_1$ & $a_2$ & $a_3$ & $a_4$  &  fg \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
\end{tabular}
\quad
\begin{tabular}{|P{0.04}P{0.04}P{0.04}P{0.045}|P{0.04}|}
\hline
$a_1$ & $a_2$ & $a_3$ & $a_4$  &  fg \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\hline
\end{tabular}

\bigskip 

\normalfont
Check if the font is back to normal.

\end{document}

enter image description here

My issues are

  1. If I start \begin{table} ... \end{table} and include all my tabulars inside, the tabulars got shifted. They are no longer next to each other.
  2. I need to place a comment, say "ex 1", "ex 2", "ex 3", "ex 4" under each small table, but have at the beginning word Table with a caption that would correspond to all small tables.
  3. The font doesn't go back to normal after my tables. Even though I wrote\normalfont.
3
  • 1
    Perhaps \normalsize instead of \normalfont ? Commented Oct 24, 2023 at 13:48
  • 1
    Off-topic: There's no need to load both amssymb and latexsym; please load just the former. Don't load the obsolete epsfig package; instead, please load the graphicx package.
    – Mico
    Commented Oct 24, 2023 at 14:09
  • I've taken the liberty of streamlining some of your LaTeX code. Feel free to revert.
    – Mico
    Commented Oct 24, 2023 at 19:00

2 Answers 2

4

Something like this?

enter image description here

\documentclass[10pt]{article}
\textwidth= 5.00in
\textheight= 7.4in
\topmargin = 30pt
\evensidemargin=0pt
\oddsidemargin=55pt
\headsep=17pt

\parskip=.5pt % that'll be barely noticeable...
\parindent=12pt


\usepackage{array}
\usepackage{amssymb,%latexsym,
            amsmath,
            graphicx,%epsfig,
            amsthm,
            enumitem%enumerate
           }
\usepackage{subcaption,booktabs}


\begin{document}

\begin{table}[htbp]
\setlength\tabcolsep{0pt} % let LaTeX figure out intercol. whitespace
\small % 10% linear reduction in font size

\caption{A table with four subtables} 
\label{tab:4subt}

\begin{subtable}{0.22\textwidth}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}ccccc} 
\toprule
$a_1$ & $a_2$ & $a_3$ & $a_4$ & fg \\
\midrule
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\bottomrule
\end{tabular*}
\caption{ex 1} \label{tab:sub1}
\end{subtable}%
\hfill
\begin{subtable}{0.22\textwidth}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}ccccc}
\toprule
$a_1$ & $a_2$ & $a_3$ & $a_4$ & fg \\
\midrule
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\bottomrule
\end{tabular*}
\caption{ex 2} \label{tab:sub2}
\end{subtable}%
\hfill
\begin{subtable}{0.22\textwidth}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}ccccc}
\toprule
$a_1$ & $a_2$ & $a_3$ & $a_4$ & fg \\
\midrule
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\bottomrule
\end{tabular*}
\caption{ex 3} \label{tab:sub3}
\end{subtable}%
\hfill
\begin{subtable}{0.22\textwidth}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}ccccc}
\toprule
$a_1$ & $a_2$ & $a_3$ & $a_4$ & fg \\
\midrule
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\addlinespace
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
10 &  10 &  10  & 10  &  10  \\
\bottomrule
\end{tabular*}
\caption{ex 4} \label{tab:sub4}
\end{subtable}

\end{table}

\end{document}
2
  • 1
    Yes!!! Thank you, this is great. I just have one question, probably stupid. How to remove a), b), c), d) in the captions of the subtables? I used \stackunder which seems to work. Is there a better option?
    – Vika
    Commented Oct 24, 2023 at 15:26
  • @Vika - If you don't want to subtable captions to be numbered (lettered?), use \caption* instead of \caption. If you do this, you should also omit the associated \label directives.
    – Mico
    Commented Oct 24, 2023 at 15:33
1

An alternative with \subfloat as defined in subcaption version 3.1 or later, and use of tabularray package. Using them the code is wee bit shorter:

\documentclass{article}
\textwidth= 5.00in
\textheight= 7.4in
\topmargin = 30pt
\evensidemargin=0pt
\oddsidemargin=55pt
\headsep=17pt
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage{tabularray}
\UseTblrLibrary{booktabs}
  \ExplSyntaxOn
\NewChildSelector{eachthree}
  {
    \int_step_inline:nnnn {1}{3}{\l_tblr_childs_total_tl}
      { \clist_put_right:Nn \l_tblr_childs_clist {##1} }
  }
  \ExplSyntaxOff
\usepackage[skip=1ex]{caption}
\usepackage{subcaption}


\begin{document}
    \begin{table}[htbp]
\caption{A table with four subtables}
\label{tab:4subt}
\subcaptionsetup[table]{labelformat=empty,
                        position=bottom, 
                        skip=0.5ex}
\small % 10% linear reduction in font size

\SetTblrInner{width=0.22\linewidth, % subtables common settings
              colsep  = 3pt,
              row{1}  = {mode=math},
              row{2-Z}= {rowsep=1pt},
              row{eachthree} = {belowsep=1ex},
              row{Z}  = {belowsep+=-1ex}
              }
\centering%
\subfloat[ex 1  \label{tab:sub1}]
{%
\begin{tblr}{colspec = {@{} *{5}{X[c]} @{}}}            
    \toprule
a_1& a_2 & a_3 & a_4 & fg   \\
    \midrule
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
    \bottomrule
\end{tblr}}\hfill%
\subfloat[ex 2  \label{tab:sub2}]
{%
\begin{tblr}{colspec = {@{} *{5}{X[c]} @{}}}
    \toprule
a_1& a_2 & a_3 & a_4 & fg   \\
    \midrule
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
    \bottomrule
\end{tblr}}\hfill%
\subfloat[ex 3  \label{tab:sub3}]
{%
\begin{tblr}{colspec = {@{} *{5}{X[c]} @{}}}
    \toprule
a_1& a_2 & a_3 & a_4 & fg   \\
    \midrule
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
    \bottomrule
\end{tblr}}\hfill%
\subfloat[ex 4  \label{tab:sub4}]
{%
\begin{tblr}{colspec = {@{} *{5}{X[c]} @{}}}
    \toprule
a_1& a_2 & a_3 & a_4 & fg   \\
    \midrule
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
%
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
10 & 10  & 10  & 10  & 10   \\
    \bottomrule
\end{tblr}}
    \end{table}
\end{document}

enter image description here

(red lines indicate page layout)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .