49

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?

4 Answers 4

55

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}
1
  • 1
    When I am using this new command inside an amsmath's align environment the label is attached to the number of align's equation rather than the number of the new counter (showing it using \ref I get the number from align's counter). Is there a way to attach the label to the new counter even inside such an environment, correcting this behaviour?
    – et_l
    Commented Aug 14, 2018 at 15:27
29

You will need to use \refstepcounter rather than \stepcounter to make your counter "visible" to the referencing mechanism. The label can go anywhere after the \refstepcounter as long as it's before the next \refstepcounter (for example the next theorem environment, section heading etc...

Using it inside a section heading might be tricky.

10

There are two things that deserve attention: 1) you need to use \refstepcounter instead of \stepcounter, and 2) since you are going to use \rtask inside a sectionning command you will have to protect it, or even better, declare it as robust from the beginning. I also included an argument of the definition, so that you can have the necessary label for cross-references:

EDIT: this approach will fail miserably if a table of contents is created. A possible workaround would be to use the optional argument for \section, but I don't like this option so much. For a proper possible solution when a ToC is created, see egreg's answer.

\documentclass{article}

\newcounter{rtaskno}
\DeclareRobustCommand{\rtask}[1]{%
   \refstepcounter{rtaskno}%
   \thertaskno\label{#1}}

\begin{document}

\section{Task \rtask{task:one}. Blah Blah..}

\section{Task \rtask{task:two}. Blah Blah..}

\section{Task \rtask{task:three}. Blah Blah..}

\section{Task \rtask{task:four}. Blah Blah..}

Task~\ref{task:four}

\end{document}
4
  • Did you try to write the table of contents? My initial idea was the same as yours, but clearly it doesn't work.
    – egreg
    Commented Jun 21, 2011 at 20:34
  • @egreg: jeje... you're right. I din't test it with a ToC. Will delete this answer shortly. Commented Jun 21, 2011 at 20:37
  • please do not delete, when the ToC is not needed, your solution might work good enough for someone.
    – Chang
    Commented Jun 21, 2011 at 20:40
  • @Chang: I won't delete it, but I added an EDIT with a warning concerning the misbehaviour if a table of contents is created. Commented Jun 21, 2011 at 20:53
1

you could create a new command for this:

\newcommand{\newTask}[2]{\section{Task \rtask. #2}\label{#1}}

and use it as such:

\newTask{t:first}{Blah Blah..}
\newTask{t:second}{Blah Blah..}
\newTask{t:third}{Blah Blah..}
1
  • That is not going to work. This will use the value of the section counter rather than the value of the section. Commented Jul 26, 2017 at 9:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .