Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm a newbie in LaTeX, please forgive me if this is too stupid.

I have some code like this:

\documentclass{article}
\newcounter{qnumber}
\newcommand{\autossection}{\stepcounter{qnumber}\subsection{\theqnumber}}

\begin{document}

\section{A}
\autossection
test
\autossection
test
\autossection
test
\autossection
test

\end{document}

Used to automatically print a progressive number as subsection name.

What I want is the subsection number to be formatted like 001, 002, ... 010, 011, ... 100, 101, etc.

Now I only have 1, 2, ... 10, 11, ... 100, 101, etc.

Is this possible?

share|improve this question

3 Answers 3

up vote 11 down vote accepted
\documentclass{article}
\newcounter{qnumber}
\newcommand{\autossection}{\refstepcounter{qnumber}\subsection{\theqnumber}}
\renewcommand\theqnumber{%
\ifnum\value{qnumber}<10 0\fi
\ifnum\value{qnumber}<100 0\fi
\arabic{qnumber}}

\begin{document}

\section{A}
\autossection
test
\autossection
test
\autossection
test
\autossection
test

\end{document}
share|improve this answer

A solution using the numprint package:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{lipsum}

\usepackage{numprint}

\renewcommand\thesection{\nplpadding{3}\numprint{\arabic{section}}}

\begin{document}

\section{A First Section}
\lipsum[1-2]

\section{A Second Section}
\lipsum[4-5]

\end{document} 

enter image description here

share|improve this answer

You could use the pad - with - zero facility from siunitx package and redefine the \thesubsection command to print numbers accordingly.

\documentclass{article}
\usepackage{siunitx}%
\usepackage{blindtext}

\renewcommand{\thesubsection}{\num[minimum-integer-digits = 3]{\number\value{subsection}}}

\begin{document}
\tableofcontents
\clearpage%
\section{A}
\subsection{One}
\blindtext
\subsection{Two}
\blindtext

%And now jump to subsection 59
\setcounter{subsection}{58}
\subsection{Fifty-Eight}
\blindtext
\setcounter{subsection}{99}
\subsection{Hundred}
\blindtext
\end{document}

enter image description here

Edit: Alternative solution with \autossection command (if needed)

\documentclass{article}
\usepackage{siunitx}%
\usepackage{blindtext}

\renewcommand{\thesubsection}{\num[minimum-integer-digits = 3]{\number\value{subsection}}}

\newcommand{\autossection}{\refstepcounter{subsection}\subsection*{\thesubsection}}

\begin{document}
\tableofcontents
\clearpage%

\section{A -- with autossection command}
\autossection
\blindtext
\autossection
\blindtext
\autossection
\blindtext
\autossection
\blindtext

\section{B -- with direct subsection command}
\subsection{One}
\blindtext
\subsection{Two}
\blindtext

%And now jump to subsection 59
\setcounter{subsection}{58}
\subsection{Fifty-Eight}
\blindtext
\setcounter{subsection}{99}
\subsection{Hundred}
\blindtext



\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.