# If/Else Statement Not Working When Trying To Reformat Subsections

I want my LaTeX code to number sections and subsections like (FYI: I'm using article not chapter or book):

0 Introduction (section)
1 A (section)
1.01 A1 (subsection)
1.02 A2 (subsection)
...
1.10 A10
1.11 A11
2 B

However, this is spitting out:
0 Introduction (section)
1 A (section)
1.01 A1 (subsection)
1.02 A2 (subsection)
...
1.010 A10
1.011 A11
2 B


Somehow, my if/else statements aren't kicking in. Anyone knows why?

Code below:

\documentclass[a4paper, 12pt]{article}

\setcounter{section}{-1}
\renewcommand{\thesection}{Module \arabic{section}}

\ifnum\value{subsection}<10
\renewcommand\thesubsection{\thesection.0\arabic{subsection}}
\else
\ifnum\value{subsection}<100
\renewcommand\thesubsection{\thesection.\arabic{subsection}}
\fi
\fi
\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}

\section{B}

\subsection{B1}

\subsection{B2}

\section{C}

\section{D}

\section{E}

\end{document}


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} • Awesome! This is exactly what I wanted. Thanks for answering. Jul 2, 2018 at 21:37

The \if condition belongs into the macro definition of \thesubsection:

\renewcommand\thesubsection{\thesection.\ifnum\value{subsection}<10 0\fi\arabic{subsection}}
{"ft_lang_label":"__label__en","ft_lang_prob":0.5973709,"math_prob":0.8892281,"size":1111,"snap":"2022-05-2022-21","text_gpt3_token_len":376,"char_repetition_ratio":0.30713642,"word_repetition_ratio":0.1682243,"special_character_ratio":0.31233123,"punctuation_ratio":0.13368984,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9962003,"pos_list":[0,1,2],"im_url_duplicate_count":[null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-29T01:41:49Z\",\"WARC-Record-ID\":\"<urn:uuid:28b66e80-c432-413b-a57c-35355a8493a8>\",\"Content-Length\":\"237623\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:26802151-d165-47bb-b843-fe057efc5f02>\",\"WARC-Concurrent-To\":\"<urn:uuid:d39e6284-ee8d-4614-bdc4-bcc8fa69f0af>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/438971/if-else-statement-not-working-when-trying-to-reformat-subsections\",\"WARC-Payload-Digest\":\"sha1:TMDQESFGKOPXDRXZH7QP36QTND25AQ54\",\"WARC-Block-Digest\":\"sha1:HP3WNMZSEVFVORYKH2UWOMOTDNBO5T4H\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652663035797.93_warc_CC-MAIN-20220529011010-20220529041010-00519.warc.gz\"}"}