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}