7

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}
7

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}

enter image description here

  • Awesome! This is exactly what I wanted. Thanks for answering. – Anonymous181 Jul 2 '18 at 21:37
4

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

\renewcommand\thesubsection{\thesection.\ifnum\value{subsection}<10 0\fi\arabic{subsection}}

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.