7

I have a document that contains 31 figures. I'd like to configure the document such that figure numbers are printed with a leading zero, both in captions and in the list of figures (LOF). That is, I would like numbers to be written in the form

01, 02, ..., 09, 10, 11, ..., 30, 31.

I've seen this post about printing a counter with a leading zero, but I'm not sure how to (or if I should) apply that to modify the automatic numbering in the caption or LOF. I've generally been having success modifying the TOC, LOF, and LOT using tocloft, but as much as the package can do it doesn't seem to have any functionality regarding the figure or table numbers, and I don't imagine it would do anything to help with the actual captions.


I'm not sure what would be expected for a "MWE", but it would be sufficient for my purposes if someone could modify the below to have the desired leading zeros.

\documentclass[paper=letter, fontsize=11pt, twoside]{article}

\usepackage{graphicx}
\usepackage{tocloft}

\begin{document}


\listoffigures

\newpage 
\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    


    

    
    
\end{document}
5
  • Please post MWE of what you tried so far.
    – MadyYuvi
    Commented Nov 12 at 4:44
  • @Mady Ok, working on it Commented Nov 12 at 4:48
  • @MadyYuvi I'm not sure what exactly you're expecting as far as an "MWE of what I've tried so far" is concerned, since I haven't found or come up with any code that I could meaning "try". Let me know whether what I've posted is sufficient, or if not, what exactly you have in mind. Commented Nov 12 at 4:59
  • 1
    If you change \thefigure, it will change it everywhere. \ifnum\value{figure}<10\relax 0\arabic{figure}\else \arabic{figure}\fi Commented Nov 12 at 5:00
  • @JohnKormylo I see how that helps if I want to print out the figure number, but I'm not sure how I would use that to modify the figure number in the generated list of figures or captions Commented Nov 12 at 5:05

3 Answers 3

6

Maybe an edit to your document will lower or increase the number of figures; or you want the same for tables.

I define an infrastructure that records in the aux file the maximum number of digits of the counter selected with \paddedcounter, so at the next run the padding can be done with respect to the number collected in the previous run. After a couple of runs the numbering will stabilize.

For simplicity, I make the (reasonable) assumption that you have less than 10000 figures.

\documentclass[11pt, twoside]{article}

\ExplSyntaxOn

\NewDocumentCommand{\paddedcounter}{m}
 {
  % protect against a not yet examined counter
  \AtBeginDocument
   {% we won't have more than 9999 figures
    \cs_if_exist:cF {maxdigits#1} { \cs_gset:cpn {maxdigits#1} {4} }
   }
  % at end document, store the number of digits of the last value
  \AtEndDocument
   {
    \iow_now:ce {@mainaux}
     {
      \token_to_str:N \maxdigits{#1}{\fp_eval:n{logb(\value{#1}+0.1)+1}}
     }
   }
  % pad
  \cs_gset:cpn {the#1} { \padded{#1} }
 }

\NewDocumentCommand{\maxdigits}{mm}
 {
  \cs_gset:cpn {maxdigits#1}{#2}
 }

\NewExpandableDocumentCommand{\padded}{m}
 {
  \prg_replicate:nn { \use:c { maxdigits#1 } - \tl_count:e { \arabic{#1} } } { 0 }
  \arabic{#1}
 }

\ExplSyntaxOff

\paddedcounter{figure}

\begin{document}

\listoffigures

\section{Test}

Test of reference: figure~\ref{test}

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}\label{test}
\end{figure}    

\setcounter{figure}{9}

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\end{document}

In the example I artificially increase the figure counter, so I pretend there are ten of them.

output with ten figures

Commenting out the \setcounter{figure}{9} statement, the output will become

output with two figures

1
  • This is fantastic, thank you for sharing Commented Nov 12 at 15:56
7

A solution which employs LaTeX's internal \two@digits macro: Just add the instructions

\makeatletter
\renewcommand\thefigure{\two@digits{\value{figure}}}
\makeatother

to the preamble.

Addendum: The \two@digits macro, which takes 1 argument, is defined as follows in the LaTeX kernel:

\def\two@digits#1{\ifnum#1<10 0\fi\number#1}
6

As per my comment:

\documentclass[paper=letter, fontsize=11pt, twoside]{article}

\usepackage{graphicx}
\usepackage{tocloft}

\renewcommand{\thefigure}{\ifnum\value{figure}<10
  0\arabic{figure}%
\else
  \arabic{figure}%
\fi}

\begin{document}


\listoffigures

\newpage 
\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    

\begin{figure}[!ht]
    Figure
    \caption{It's a figure}
\end{figure}    
    
\end{document}
6
  • 1
    I misunderstood your comment since I didn't realize what \thefigure did, I really appreciate the answer. Thank you! Commented Nov 12 at 5:09
  • 1
    \relax is wrong, a space after 10 is the correct input.
    – egreg
    Commented Nov 12 at 7:46
  • 1
    @egreg - It works, so it can't be too wrong. % would definitely be wrong, since the next character after 10 is 0. Commented Nov 12 at 15:27
  • MIght the fact that the LaTeX kernel's definition of \two@digits features a space rather than \relax after 10 sway you? :-)
    – Mico
    Commented Nov 12 at 15:43
  • 1
    @JohnKormylo You're making \thefigure not fully expandable. Just remember to leave a space after every constant.
    – egreg
    Commented Nov 12 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .