Graphics, Figures & TablesDetecting zero section

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
adkdk
Posts: 9
Joined: Sun Apr 18, 2010 11:39 am

Detecting zero section

Post by adkdk »

LaTeX, by default, uses two-part style of figure, table and equation numbers (like Figure 1.3). I used to have 3-part numbers (chapter.section.object number). To achieve this in LaTeX, I use the following code (example for table):

Code: Select all

\renewcommand{\thetable}{\thesection.\arabic{table}}
However, when I try to put table before first section begins, it returns zero as section number (table number looks like 1.0.5). This is undesired behavior, I want to have two-part numbering before first section (like 1.5).

MWE:

Code: Select all

\documentclass{report}
\renewcommand{\thetable}{\thesection.\arabic{table}}
\begin{document}
\chapter{foo}
\begin{table}[htp!]
\caption{Table outside section}
\begin{tabular}{|c|}
qwerty
\end{tabular}
\end{table}

\section{First section}
\begin{table}[htp!]
\caption{Table inside section}
\begin{tabular}{|c|}
qwerty
\end{tabular}
\end{table}
\end{document}
I tried to use ifthen to detect, if I am inside or outside a section with this code:

Code: Select all

\usepackage{ifthen}
...
\ifthenelse{\equal{\arabic{section}}{0}}{we are outside}{we are inside}
This works inside text, but not within \renewcommand (undefined control sequence). What's wrong?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

adkdk
Posts: 9
Joined: Sun Apr 18, 2010 11:39 am

Detecting zero section

Post by adkdk »

I solved the problem without using ifthen package.

Put this into preamble:

Code: Select all

\makeatletter
\renewcommand{\thetable}{\ifnum \c@chapter>0
	\ifnum \c@section>0
	\thesection.\@arabic\c@table
	\else
	\thechapter.\@arabic\c@table
	\fi
\else
	\@arabic\c@table
\fi
}
\renewcommand{\thefigure}{\ifnum \c@chapter>0
	\ifnum \c@section>0
	\thesection.\@arabic\c@figure
	\else
	\thechapter.\@arabic\c@figure
	\fi
\else
	\@arabic\c@figure
\fi
}
\renewcommand\theequation{\ifnum \c@chapter>0
	\ifnum \c@section>0
	\thesection.\@arabic\c@equation
	\else
	\thechapter.\@arabic\c@equation
	\fi
\else
	\@arabic\c@equation
\fi
}
\makeatother
Now when table (figure, equation) is before chapter beginning, it has 1-part number. After \chapter, but before \section it has 2 parts. After \section are 3 parts.
Post Reply