[Tex/LaTex] custom counter and cross-referencing

counterscross-referencing

I created a custom counter 'rtaskno' and a command '\rtask' as following:

\newcounter{rtaskno}
\newcommand{\rtask}{%
   \stepcounter{rtaskno}%
   \thertaskno}

I like to use them as following:

\section{Task \rtask. Blah Blah..}

\section{Task \rtask. Blah Blah..}

\section{Task \rtask. Blah Blah..}

\section{Task \rtask. Blah Blah..}

Later in the text, if I want to cross-reference 'Task 3', how can I do it? Where to put label?

Best Answer

There are tricky timing issues in your problem. If you are going to prepare a table of contents the thing as it is now will bomb out.

Probably the best thing to do is to use a two-pass strategy:

\newcounter{rtaskno}
\newcommand{\rtask}[1]{\refstepcounter{rtaskno}\label{#1}}

When you want to use the task number in your section titles you'll write

\rtask{label}
\section{Task \ref{label}. Blah}
Related Question