3

I'm trying to reference a counter without increment it. I have

\newcounter{procstep}
\newcommand{\refproc}[1]{\addtocounter{procstep}{-1}\refstepcounter{procstep}\label{#1}}

Is this the correct way to do this? I was wondering if there was a simpler way? I was sort of expecting a \refcounter macro.

  • 1
    I think you want \theprocstep – Mark Apr 11 '17 at 2:56
  • Also see if this helps: en.wikibooks.org/wiki/LaTeX/Counters#Counter_access – user103221 Apr 11 '17 at 2:56
  • @Mark: \theprocstep itself is not frozen to provide a label as a state when being referred to it. You have to write the current value to the .aux – user31729 Apr 11 '17 at 3:37
  • It would also be quite useful to know why this should be useful? – user31729 Apr 11 '17 at 4:20
  • @ChristianHupfer - I have a table that increments the first column by 10 on every new row. This keeps the number correct if I insert new rows arbitarily to the table. I would like to be able to reference the row number (procstep) from other rows - i.e. repeat rows 20-40 where 20-40 are labels to other rows that may change – Ross W Apr 11 '17 at 14:45
4

Similar to John Kormylo's answer, but a little bit shorter. (See the extended version at the end of this post)

\documentclass{article}

\newcounter{procstep}
\usepackage{hyperref}

\providecommand{\phantomsection}{}



\makeatletter
\newcommand{\statlabel}[1]{%
  \phantomsection
  \protected@edef\@currentlabel{\theprocstep}%
  \protected@edef\@currentlabelname{procstep}%
  \label{#1}%
}
\makeatother

\usepackage{blindtext}

\begin{document}

See \ref{first}

\blindtext[10]
\setcounter{procstep}{100} \fbox{Here is the label\statlabel{first}}



\end{document}

Extended version for any counter

\documentclass{article}

\newcounter{procstep}
\usepackage{xparse}
\usepackage{blindtext}
\usepackage{hyperref}

\providecommand{\phantomsection}{}

\makeatletter
\NewDocumentCommand{\statlabel}{omm}{%
  \phantomsection%
  \protected@edef\@currentlabel{\csname the#2\endcsname}%
  \IfValueT{#1}{%
    \protected@edef\@currentlabelname{#1}%
  }%
  \label{#3}%
}
\makeatother


\begin{document}

See \ref{first} or \nameref{first} or \ref{seclabel} and \nameref{seclabel} or \autoref{seclabel} 

\blindtext[10]

\setcounter{section}{17}% Just for fun
\statlabel[Some foo text]{section}{seclabel}


\setcounter{procstep}{100}\fbox{Here is the label\statlabel[Static Label to procstep]{procstep}{first}}

\end{document}

enter image description here

2

I wouldn't exactly call this simpler, unless you throw out the hyperref option.

\documentclass{article}
%\usepackage{hyperref}

\newcounter{procstep}

\makeatletter
\@ifpackageloaded{hyperref}%
    {\newcommand{\refproc}[1]{\protected@edef\@currentlabelname{procstep}%
    \Hy@MakeCurrentHrefAuto{procstep}%
    \Hy@raisedlink{\hyper@anchorstart{\@currentHref}\hyper@anchorend}%
    \protected@write\@auxout{}{\string\newlabel{#1}{{\theprocstep}{\thepage}%
      {\@currentlabelname}{\@currentHref}{}}}}}%
  {\newcommand{\refproc}[1]{\protected@write\@auxout{}{\string\newlabel{#1}{{\theprocstep}{\thepage}}}}}
\makeatother


\begin{document}
\ref{test}
\setcounter{procstep}{23}
\refproc{test}
\end{document}
  • I think, you could let do hyperref most of the things you're using \phantomsection and provide \@currentlabelname and \currentlabel only – user31729 Apr 11 '17 at 3:40
  • Thanks John - Definitely not simpler... :) The version I came up with works well - I was just surprised that there wasn't a (obvious) way of getting the reference without actual performing an operation on the counter – Ross W Apr 11 '17 at 14:42
  • OTOH, this is precisely what \refstepcounter and \label do, minus incrementing the counter. – John Kormylo Apr 11 '17 at 19:03

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.