2

I have four tables on one line that I would like to refer to as separate figures. I would like them to appear like this:

enter image description here

This is how the tables currently appear:

enter image description here

\begin{table}[h!]  
\medskip 
    \begin{tabular}{|c|c|c|}
        \hline
        -1 & -1  & -1 \\ \hline
        2 & 2 & 2 \\  \hline
       -1 & -1  & -1 \\
        \hline
    \end{tabular}
\hfill 
    \begin{tabular}{|c|c|c|}
        \hline
        2 & -1  & -1 \\ \hline
        -1 & 2 & -1 \\  \hline
        -1 & -1  & 2 \\
        \hline
    \end{tabular}
\hfill
    \begin{tabular}{|c|c|c|}
        \hline
        -1 & 2  & -1 \\ \hline
        -1 & 2 & -1 \\  \hline
        -1 & 2  & -1 \\
        \hline
    \end{tabular}
\hfill
    \begin{tabular}{|c|c|c|}
        \hline
        -1 & -1  & 2 \\ \hline
        -1 & 2 & 1 \\  \hline
        2 & -1  & -1 \\
        \hline
    \end{tabular}
\captionof{figure}{Masks for line detection} \label{table:mul}
\end{table}

I want to caption each of the tables with (a) text, (b) text, ect.

What is the proper way to do this, so that I can caption each of the tables, but referenced as figures?

CObject
  • 35

2 Answers2

1

I suggest you load the subcaption package and encase each of the tables (figures?) in a subfigure environment. And, while you're at it, change the table environment to a figure environment, and change the tabular environments to array environments. The latter change will assure that the the - (minus) symbols are typeset correctly.

enter image description here

\documentclass{article}
\usepackage{subcaption,array}

\begin{document}
\setcounter{figure}{2} % just for this example

\begin{figure}[h!] 
\setlength\extrarowheight{2pt} % optional
\captionsetup[subfigure]{font=footnotesize}
\begin{subfigure}[t]{0.27\textwidth} 
\centering
$\begin{array}{|r|r|r|}
        \hline
        -1 & -1 & -1 \\ \hline
         2 &  2 &  2 \\ \hline
        -1 & -1 & -1 \\
        \hline
\end{array}$ 
\caption{Horizontal line mask}
\label{fig:a}
\end{subfigure}%
\hfill 
\begin{subfigure}[t]{0.24\textwidth}
\centering
$\begin{array}{|r|r|r|}
        \hline
         2 & -1 & -1 \\ \hline
        -1 &  2 & -1 \\ \hline
        -1 & -1 &  2 \\
        \hline
\end{array}$ 
\caption{$-45^{\circ}$ line mask}
\label{fig:b}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.24\textwidth}
\centering
$\begin{array}{|r|r|r|}
        \hline
        -1 & 2 & -1 \\ \hline
        -1 & 2 & -1 \\ \hline
        -1 & 2 & -1 \\
        \hline
\end{array}$ 
\caption{Vertical line mask}
\label{fig:c}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.23\textwidth}
\centering
$\begin{array}{|r|r|r|}
        \hline
        -1 & -1 &  2 \\ \hline
        -1 &  2 & -1 \\ \hline
         2 & -1 & -1 \\
        \hline
\end{array}$ 
\caption{$45^{\circ}$ line mask}
\label{fig:d}
\end{subfigure}%

\caption{Line detection masks} 
\label{fig:mul}
\end{figure}
\end{document}
Mico
  • 506,678
0

Don't use \begin{table}...\end{table}. Use \begin{figure}...\end{figure}. It doesn't matter that you're putting a tabular environment inside a figure.

You likely want to use the subcaption package (https://ctan.org/pkg/subcaption).

There are several ways to use it. For your example, the simplest way is to use \subcaptionbox.

Example modified from package documentation:

\documentclass{article}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\subcaptionbox{A cat\label{cat}}
{ \begin{tabular}{|c|c|c|} \hline
        2 & -1  & -1 \\ \hline
        -1 & 2 & -1 \\  \hline
        -1 & -1  & 2 \\
        \hline  \end{tabular}    }
\subcaptionbox{An elephant\label{elephant}}
{ \begin{tabular}{|c|c|c|} \hline
        -1 & 2  & -1 \\ \hline
        -1 & 2 & -1 \\  \hline
        -1 & 2  & -1 \\  \hline
    \end{tabular} }
\caption{Two animals}\label{animals}
\end{figure}
\end{document}
jaspast
  • 211