3

I have a custom counter and output it with leading zeros. When I reference it, I get some real strange output, but the enumeration itself works.

Preamble:

\newcounter{reqcount}
\setcounter{reqcount}{3}

\newcommand{\threedigits}[1]{%
    \ifnum #1<100 0\fi%
    \ifnum #1<10 0\fi%
    \number#1}

Document

\refstepcounter{reqcount}\label{req}
[DR-\threedigits{\arabic{reqcount}}] % Works, outputs [DR-001]
[DR-{\ref{req}}] % Outputs [DR-4<100 04<10 004]

Someone posted this answer but it was removed. I don't know why, but the code below works and the above does not.

\documentclass{article}

\newcounter{reqcount}
\setcounter{reqcount}{0}

\newcommand{\threedigits}[1]%
{%
    \ifnum #1<10 00%
    \else%
        \ifnum #1<100 0%
        \fi%
    \fi%
    \number#1%
}

\newcommand\DR[1]% macro to typeset a new DR item
{%
    \refstepcounter{reqcount}\label{DR:#1}%
    [DR-\threedigits{\value{reqcount}}]%
}

\makeatletter
\newcommand\refDR[1]% macro to cross-reference to a particular DR item
{%
        [DR-\expandafter\threedigits\ref{DR:#1}]%
}
\makeatother

\begin{document}

%.... 88 DR items later
\setcounter{reqcount}{1}

\DR{foo} blah blah

As seen in~\refDR{foo}, blah blah blah

\end{document}
Oscar
  • 33
  • Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – egreg Jan 09 '14 at 11:01
  • You got the code with 4 as argument #1 instead of the calculated result. I guess we should see more of your code, how you actually use it, as @egreg said. – Stefan Kottwitz Jan 09 '14 at 11:06
  • This is basically an MWE. Feel free to add the three lines of codes as necessary. There were an answer here yesterday that I got working, but it was removed before I was able to mark it as an answer. If the user adds the answer again, I will happily accept it. – Oscar Jan 10 '14 at 07:47
  • Sorry, but this is not a MWE. – egreg Jan 10 '14 at 09:57
  • Important information is missing like how you redefined \thereqcount (which you obviously have: otherwise [DR-{\ref{req}}] should print something like »[DR-4]«) so this is not an MWE – cgnieder Jan 10 '14 at 10:10
  • Never claimed it to be an MWE either (and it's definitely not working). Anyway, added the anonymous (slightly modified) solution, hope it helps someone. – Oscar Jan 10 '14 at 13:15
  • @Oscar yes you did (»This is basically an MWE«). Working doesn't mean it shouldn't produce errors but to allow others to reproduce the issues. Have you even followed the link (http://meta.tex.stackexchange.com/q/228)? – cgnieder Jan 10 '14 at 13:18
  • It was a joke. Also please note the basically. Thanks for the input anyway! – Oscar Jan 10 '14 at 13:24

1 Answers1

3

While I don't completely understand what you actually need I still want to show an alternative to the accepted solution you added to your question.

I'd define a counter representation command (like \arabic) that prints the value of a counter with at least three digits and redefine the corresponding \the<counter> macro to use it. For this two commands need to be defined (see How can you make custom counter display types?):

\makeatletter
\newcommand*\threedigits[1]{\expandafter\@threedigits\csname c@#1\endcsname}
\newcommand*\@threedigits[1]{%
  \ifnum#1<100 0\fi
  \ifnum#1<10 0\fi
  \number#1%
}
\makeatother

With this you for example say \threedigits{section} or in your case \threedigits{reqcount}. If you then redefine \thereqcount

\renewcommand*\thereqcount{[DR-\threedigits{reqcount}]}

You can simply use the standard \ref to get the desired output:

\documentclass{article}

\makeatletter
\newcommand*\threedigits[1]{\expandafter\@threedigits\csname c@#1\endcsname}
\newcommand*\@threedigits[1]{%
  \ifnum#1<100 0\fi
  \ifnum#1<10 0\fi
  \number#1%
}
\makeatother

\newcounter{reqcount}
\renewcommand*\thereqcount{[DR-\threedigits{reqcount}]}
\newcommand*\DR{\refstepcounter{reqcount}\thereqcount}

\begin{document}

\setcounter{reqcount}{37}

\DR\label{DR:foo} blah blah

As seen in~\ref{DR:foo}, blah blah blah

\end{document}
cgnieder
  • 66,645
  • Thanks for the answer! Works as well, and is more elegant than the other suggestion. – Oscar Jan 10 '14 at 13:52
  • I noted that adding % after the \fi's removed some compilation errors I got in (rare) cases. – Oscar Jan 10 '14 at 15:34
  • @Oscar spaces after the \fi's are ignored anyway (as they are after each control sequence word) so adding % is equivalent to not adding them. The cause of the errors you get must be something else... – cgnieder Jan 10 '14 at 15:39
  • I believe the extra %causes the compiler to ignore the row break, which in some cases may or may not cause compilation errors. I believe this might be relevant. I think I had some package that caused this. – Oscar Jan 10 '14 at 16:46
  • @Oscar see also http://tex.stackexchange.com/q/19922/5049 and http://tex.stackexchange.com/q/7453/5049 and http://tex.stackexchange.com/q/34844/5049 – cgnieder Jan 10 '14 at 16:50
  • would you explain newbies what does \expandafter and \csname are and why are they used here? I read docs and I'm still unclear... – igorsantos07 Dec 12 '14 at 13:17
  • @Igoru there are a numbers of questions with answers dealing with those, e.g., http://tex.stackexchange.com/questions/451/ and http://tex.stackexchange.com/questions/39380 probably more – cgnieder Dec 12 '14 at 13:26
  • I read those but that still seems a little bit complicated. I asked that because I was interested in not creating two custom padding commands, but rather using \padzeroes from package fmtcount... But I was unable to understand how to mix that command with the answer you gave :( – igorsantos07 Dec 12 '14 at 13:58
  • I understood as follows: \csname c@#1\endcsname would turn the string into the counter name; then you pass that into @threedigits, that would turn the counter into a real number by using \ifnum and \number and pad accordingly, right? – igorsantos07 Dec 12 '14 at 14:00
  • @Igoru I explained it in a littel more detail im my linked answer: http://tex.stackexchange.com/a/124983/ It is important that the internal counter representation command (\@threedigits in this example) is expandable. I don't know if that is the case for \padzeroes... – cgnieder Dec 12 '14 at 14:03