3

I am trying to create a new section that automatically updates the counter with relevant label.

newcommand-stepcounter sort of does what I am trying to do , but not quite.

When I use the code below, the counters on the sections headed "Eq" are updated as I'd like, but if I try to \label them then the section number is used rather than the counter on the new command.

How can I use the relevant counter when using label please.

\documentclass{report}    
\newcounter{mycounter}
\newcommand{\step}{\stepcounter{mycounter}\subsubsection*{Eq \themycounter}}   

\begin{document}

\section{}
\subsection{}
\step\label{eq:one}

\section{}
\subsection{}
\step

As seen in Eq~\ref{eq:one}

\end{document}

Which produces

enter image description here

  • I think that if you replace \stepcounter by \refstepcounter it'll work... – Phelype Oleinik Aug 13 '18 at 19:50
  • @PhelypeOleinik ; that works thank you. If you would like to add it as an answer i will accept it. – user20650 Aug 13 '18 at 19:53
  • The answer, as you requested :) – Phelype Oleinik Aug 13 '18 at 19:56
3

You just have to change \stepcounter to \refstepcounter.

\documentclass{report}
\newcounter{mycounter}
\newcommand{\step}{\refstepcounter{mycounter}\subsubsection*{Eq \themycounter}}

\begin{document}

\section{}
\subsection{}
\step\label{eq:one}

\section{}
\subsection{}
\step

As seen in Eq~\ref{eq:one}

\end{document}

enter image description here

\stepcounter just adds 1 to the counter but \refstepcounter makes the \@currentlabel be that counter:

\def\refstepcounter#1{\stepcounter{#1}%
    \protected@edef\@currentlabel
       {\csname p@#1\endcsname\csname the#1\endcsname}%
}

so when you are using \labels, \stepcounter alone isn't enough because LaTeX doesn't know that the counter is a label.

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.