If/Else Statement Not Working When Trying To Reformat Subsections
Your \ifnum
is only evaluated once, precisely at a moment when subsection
has value 0.
You need the conditional to be evaluated when \thesection
is expanded:
\renewcommand{\thesection}{%
\thesection.%
\ifnum\value{subsection}<10
0%
\else
\ifnum\value{subsection}<100
0%
\fi
\fi
\arabic{subsection}%
}
A different solution with xparse
, which is easily generalizable to any (fixed) padding: we count the number of items in the expansion of \arabic{subsection}
(that is, the number of digits) and pad accordingly with zeros, in this case to three digits.
\usepackage{xparse}
\ExplSyntaxOn
\RenewExpandableDocumentCommand{\thesubsection}{}
{
\thesection.
\prg_replicate:nn { 3 - \tl_count:f { \arabic{subsection} } } { 0 }
\arabic{subsection}
}
\cs_generate_variant:Nn \tl_count:n { f }
\ExplSyntaxOff
Full example:
\documentclass[a4paper, 12pt]{article}
\usepackage{xparse}
\setcounter{section}{-1}
\renewcommand{\thesection}{Module \arabic{section}}
\ExplSyntaxOn
\RenewExpandableDocumentCommand{\thesubsection}{}
{
\thesection.
\prg_replicate:nn { 3 - \tl_count:f { \arabic{subsection} } } { 0 }
\arabic{subsection}
}
\cs_generate_variant:Nn \tl_count:n { f }
\ExplSyntaxOff
\begin{document}
\title{Notes}
\author{}
\date{}
\maketitle
\section{Introduction}
\section{A}
\subsection{A1}
\subsection{A2}
\subsection{A3}
\subsection{A4}
\subsection{A5}
\subsection{A6}
\subsection{A7}
\subsection{A8}
\subsection{A9}
\subsection{A10}
\subsection{A11}
\setcounter{subsection}{99}
\subsection{A100}
\section{B}
\subsection{B1}
\subsection{B2}
\section{C}
\section{D}
\section{E}
\end{document}
The \if
condition belongs into the macro definition of \thesubsection
:
\renewcommand\thesubsection{\thesection.\ifnum\value{subsection}<10 0\fi\arabic{subsection}}