2

I am typesetting a book which is divided into paragraphs (sections), which are numbered continuously through the whole book, and which contain some tables. Tables must be numbered with the same number as the section within which they are located. Thus, "Table 23" is immediately recognizable as the being located in the Section 23.

However, some sections contain more than one tables. I need them to be numbered with a sequential number added to the section number, restarting per each section.

E.g.: Tables 46.1 and Table 46.2 stand both within the Section 46, while Tables 120.1, 120.2 and 120.3 are located in the Section 120.

But, importantly, if there is only one Table in a Section, its number must remain simply the same as the Section number, without the trailing ".1".

Can I achieve this automatically?

  • 1
    Try \renewcommand{\thetable}{\arabic{section}.\arabic{table}} along with \numberwithin{table}{section}. – user121799 Jul 15 '18 at 14:31
  • @marmot Yes, but I don't need the second number in case there is only one table per section. I must have either Table 23 or Table 23.1, 23.2 etc. – Artemij Keidan Jul 15 '18 at 14:32
  • 1
    A fundamental issue is that when the first table of section 23 occurs, TeX simply doesn't know whether or not it'll be the sole table in that section. That won't be known until either another \section command occurs -- in which case the table is indeed the only table -- or if another table environment and associated \caption directives show up -- in which case the previous table was not the only one in that section. Would you be willing to accept a solution that has a separate \caption directive for tables that are singletons in their respective sections? – Mico Jul 15 '18 at 14:49
  • @marmot this is what the author wants, don't blame it on me. – Artemij Keidan Jul 15 '18 at 15:06
  • @Mico yes, I also thought about that, but it could be achieved with a double run, writing data into the aux file. – Artemij Keidan Jul 15 '18 at 15:07
3

The following seems to do the trick, at least using the amsbook document class. For other document classes it might need some tweaking (this is why it is always good to give a minimal working example).

The basic idea is that whenever a section is started (or at the end of the document) the number of tables in the previous section is saved to the aux file with a label of the form TablesInSection<section#>. Then the \thetable command uses the \setcounterref command from the refcount package to check to see if the section contains more than one table. If it does, then the table number is printed with the section number and otherwise only the section number is printed.

As the number of tables in each section is being saved in the aux file you will need to run (pdf)latex twice each time the number of tables in any section changes. I have not checked to see what happens if the \section command is used with an optional argument, so it is quite likely this will need tweaking. If you are using chapters then sections should also be numbered inside chapters, although you could hack around this.

Here is the output:

enter image description here

and here is the code:

\documentclass{amsbook}
\usepackage{etoolbox}
\usepackage{refcount}
\numberwithin{table}{section}
\makeatletter
% write a label to the aux file
\newrobustcmd{\WriteLabel}[2]{%
% use the following if hyperref is loaded
%   \immediate\write\@auxout{\string\newlabel{#1}{{#2}{}{}{#1.#2}{}}}
    \immediate\write\@auxout{\string\newlabel{#1}{{#2}{\thepage}}}%
}
\makeatother

\newcommand\savesectiontablenumber{%
  \ifnum\value{section}>0%
    \ifnum\value{table}>0%
      \WriteLabel{TablesInSection\thesection}{\arabic{table}}%
    \fi%
  \fi%
}
\preto\section\savesectiontablenumber
\AtEndDocument{\savesectiontablenumber}
\newcounter{tablechecker}
\renewcommand\thetable{%
  \setcounterref{tablechecker}{TablesInSection\thesection}
  \ifnum\thetablechecker>1
    \thesection.\arabic{table}%
  \else\thesection%
  \fi%
}

\begin{document}

\section{Section one}
  \begin{table}[h]\caption{A table}\label{single}
A table
\end{table}

\section{Section one}
  \begin{table}[h]\caption{First table}\label{one}
First table
\end{table}
\begin{table}[h]\caption{Second table}\label{two}
Second table
\end{table}

\end{document}
  • Thank you so much, this is brilliant, exactly what I meant. Sorry for not providing a minimal working example, but your suggestion is working perfectly anyway. – Artemij Keidan Jul 16 '18 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.