# cleveref counter and ref label number format with padding zeroes

I am using cleverref and a custom counter to typeset a special kind of reference, where I need the same text on the definition side and the reference side.

Currently I use \textbf{F\arabic{thefeatures}} on the definition side and \crefformat{thefeatures}{#2\textbf{F#1}#3} to format the references.

However instead of the F<number> I'd like to switch to F<number padded by zeroes>.

While I can use the fmtcount package for the definition side (and use the \padzeroes command with decimal output), this doesnt work for the reference labels.

Current code:

\documentclass{article}
\usepackage{cleveref}

\newcounter{thefeatures}
\setcounter{thefeatures}{0}

\crefname{thefeatures}{feature}{features}

\newcommand{\deffeat}[1]{
\refstepcounter{thefeatures}
\label{#1}
\textbf{F\arabic{thefeatures}}
}

\crefformat{thefeatures}{#2\textbf{F#1}#3}

\begin{document}

\deffeat{test} feature test

\deffeat{test2} feature test2

\Cref{test} test ref

\end{document}


Manual approach:

\textbf{%
F%
\ifnum\value{thefeatures}<1000 0\fi
\ifnum\value{thefeatures}<100 0\fi
\ifnum\value{thefeatures}<10 0\fi
\arabic{thefeatures}%
}%


A possible implementation that works for you is

\documentclass{article}
\usepackage{cleveref}

\newcounter{thefeatures}
\setcounter{thefeatures}{0}

\crefname{thefeatures}{feature}{features}

\newcommand{\deffeat}[1]{%
\refstepcounter{thefeatures}%
\label{#1}%
\printfeat{\arabic{thefeatures}}%
}

\crefformat{thefeatures}{#2\protect\printfeat{#1}#3}

\newcommand{\printfeat}[1]{%
\textbf{%
F%
\ifnum\value{thefeatures}<1000 0\fi
\ifnum\value{thefeatures}<100 0\fi
\ifnum\value{thefeatures}<10 0\fi
#1%
}%
}

\begin{document}

\deffeat{test} feature test

\deffeat{test2} feature test2

\Cref{test} test ref

\end{document}


Watch out for spurious spaces in your definitions.

• and how would i use that within crefformat ? where the counter value is somewhat different the only place where this would work, is where the fmtcount package would aready work – Ronny Nov 1 '12 at 16:36
• it seems you completely missed that the counter value is always the last one, no the one of the referenced item – Ronny Nov 1 '12 at 21:21
• @Ronny: Did you compile at least twice? This should not be the case. – Werner Nov 2 '12 at 3:29
• the script i test with is compiled 4 times (due to bibtex) – Ronny Nov 2 '12 at 9:01
• to be clar, i added a printing for the value of the counter using \arabic{thefeatures} in the format (so it fits his code) - and as expected the counter was on the last value, since it doesnt get a new value in the format so the noly way to get the counter in the crefformat seems to be the one that comes in as string – Ronny Nov 2 '12 at 9:03

i solved the problem using the xstrings package and thanks to controling padding using the xstring package

the code for the pad command i use is

\newcommand\padfrom[1]{%
\StrLen{#1}[\templen]%
\StrGobbleRight{000}{\templen}%
#1}