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}
  • 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 9 '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 9 '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 7:47
  • Sorry, but this is not a MWE. – egreg Jan 10 '14 at 9: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 – clemens Jan 10 '14 at 10:10
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}
  • 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... – clemens 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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