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 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}
share|improve this question
    
Please add a minimal working example (MWE) that illustrates your problem. It is considered a lot better to put in some code that will compile, as it makes it a lot easier for us to copy it into our text editor and work with it, and see exactly what it is you are trying to do. –  Werner Nov 1 '12 at 18:38
add comment

2 Answers

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.

share|improve this answer
    
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
add comment
up vote 0 down vote accepted

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}
share|improve this answer
add comment

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.