# Reference a counter without incrementing

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

\newcounter{procstep}


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.

• 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

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}


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

\documentclass{article}
%\usepackage{hyperref}

\newcounter{procstep}

\makeatletter
{\newcommand{\refproc}[1]{\protected@edef\@currentlabelname{procstep}%
\Hy@MakeCurrentHrefAuto{procstep}%
\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