8

I am formatting a document for someone, and he wants each section number to be three digits long, starting with 000, and with padding zeros when needed.

I found a similar question, and tried to adapt the example, but it gives me a

! TeX capacity exceeded, sorry [input stack size=5000].
\thesection ->\thesection 
                          .\three@digits {\value {section}}
l.11 \section{Synopsis}

If you really absolutely need more capacity,
you can ask a wizard to enlarge me.

My MNWE is:

 \documentclass[letterpaper]{article}

 %Padding the leading zeros \makeatletter
 \renewcommand\thesection{\thesection.\three@digits{\value{section}}}
 \makeatother

 \begin{document} \setcounter{section}{-1} \tableofcontents
 \section{Synopsis} foo

 \section{Language} bar

 \section{Legal} baz

 \end{document}
Canageek
  • 17,935
  • 2
    You actually have a recursive definition for \thesection, since \thesection contains \thesection, which contained \thesection, which contains \thesection, ... – Werner Jul 12 '13 at 18:58

1 Answers1

10

There exists no \three@digits in the kernel by default. However, you can define it, and you also need to adjust the ToC-related number spacing for sections:

enter image description here

\documentclass[letterpaper]{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

%Padding the leading zeros \makeatletter \patchcmd{\l@section}{1.5em}{2em}{}{}% Adjust section ToC number width %\def\two@digits#1{\ifnum#1<10 0\fi\number#1}% http://mirrors.ctan.org/macros/latex/unpacked/latex.ltx \def\three@digits#1{\ifnum#1>99\else\ifnum#1>9 0\else00\fi\fi\number#1} \renewcommand\thesection{\three@digits{\value{section}}} \makeatother

\begin{document} \setcounter{section}{-1} \tableofcontents \section{Synopsis} foo

\section{Language} bar

\section{Legal} baz

\end{document}

The ToC-related spacing is adjusted using an etoolbox patch. You may want to adjust the \subsection spacing as well which is defined as

\newcommand*\l@subsection{\@dottedtocline{2}{1.5em}{2.3em}}

by default (from article.cls). Here 1.5em is the indent and 2.3em is the space for the numbers. So you might go with

\renewcommand*\l@subsection{\@dottedtocline{2}{2em}{3em}}

say.


For more on padding counters with zeroes, see How to output a counter with leading zeros?

Werner
  • 603,163
  • I'd already adjusted the spacing with tocloft, I just removed that from my example to make it minimal. It seems to be nice and compatible, thank you. – Canageek Jul 12 '13 at 19:08