[Tex/LaTex] Table numbering by section with letters

captionstables

I have a 2 part table that I wish to label as "a" and "b", that also includes the section number and the table number within the section (e.g., "Table 4.1a" and Table "4.1b").

I use the basic approach to produce a caption which includes the section number (the \counterwithin{table}{section} command). However, when I try to integrate a subcaption, I can only produce "Table 1a" and "Table 1b", which does not include the section number.

\documentclass{article}
\setcounter{totalnumber}{4}

\begin{document}

\begin{table}
\renewcommand{\thetable}{\arabic{table}a}
\caption{First caption}\label{first}
\end{table}

\begin{table}
\addtocounter{table}{-1}
\renewcommand{\thetable}{\arabic{table}b}
\caption{Second caption}\label{second}
\end{table}

\end{document}

How can I integrate the \counterwithin{table}{section} command so that section numbers are also included?

Thanks

Best Answer

\documentclass{article}

\newcounter{mysubtable}
\newcommand\modcounter{%
  \refstepcounter{mysubtable}%
  \renewcommand{\thetable}{\thesection.\arabic{table}\alph{mysubtable}}%
}

\begin{document}

\section{Test}
Some cross-references to tables~\ref{first} and~\ref{second}.

\begin{table}
\modcounter
\caption{First caption}
\label{first}
\end{table}

\begin{table}
\addtocounter{table}{-1}
\modcounter
\caption{Second caption}
\label{second}
\end{table}

\end{document}

The result:

enter image description here

Remarks

  • A new counter mysubtable was defined.

  • A helping command \modcounter was also defined to (ref)step the mysubtable counter and to redefine \thetable to preppend the section counter.

  • To restart the counter, add

    \setcounter{mysubtable}{0}
    

    at the appropriate location.