# How to refer back to a point in the document with custom counters?

The easiest way to explain what I would like to have is to show the desired output and I would like to get this by writing something like:

\begin{document}

Draw point B \STEP{drawb}. Connect A and B \STEP{connectab}. Draw point A \STEP{drawa}.

The correct order is \REF{drawa} then \REF{drawb} and finally
\REF{connectab}.

\end{document}


The references should be links, so I can jump there by clicking it.

How should \STEP and \REF be defined?

The example is oversimplified. The referenced parts of the text cannot be put into an enumerate environment or whatnot, they are inside some paragraphs and must remain there. I am using latex and the hyperref package.

Feel free to edit the title, I have no idea how to phrase it in consice way.

-
What are the dependencies of the numbering? The order of \REF commands? – Heiko Oberdiek Oct 1 '12 at 22:04
Yes, exactly, in the order of the \REF commands. – Ali Oct 1 '12 at 22:09

## Link from \STEP to \REF

\documentclass{article}
\usepackage{hyperref}

\newcounter{step}
\renewcommand*{\thestep}{Step~\arabic{step}}

\newcommand*{\REF}[1]{%
\begingroup
\refstepcounter{step}%
\label{#1}%
\thestep
\endgroup
}

\newcommand*{\STEP}[1]{%
(\ref{#1})%
}

\begin{document}

Draw point B \STEP{drawb}. Connect A and B \STEP{connectab}. Draw point A
\STEP{drawa}.

The correct order is \REF{drawa} then \REF{drawb} and finally
\REF{connectab}.

\end{document}


## Link from \REF to \STEP

Probably you want the links from \REF to \STEP. This solution puts anchors in both cases. Thus it can easily be extended to make links in both directions if needed.

\documentclass{article}
\usepackage[verbose]{hyperref}

\newcounter{step}
\renewcommand*{\thestep}{Step~\arabic{step}}

\newcommand*{\REF}[1]{%
\begingroup
\refstepcounter{step}%
\label{ref:#1}%
\hyperref[{step:#1}]{\thestep}%
\endgroup
}

\newcommand*{\STEP}[1]{%
\begingroup
\phantomsection
\label{step:#1}%
(\ref*{ref:#1})%
\endgroup
}
\makeatother

\begin{document}

Draw point B \STEP{drawb}. Connect A and B \STEP{connectab}. Draw point A
\STEP{drawa}.

The correct order is \REF{drawa} then \REF{drawb} and finally
\REF{connectab}.

\end{document}


## Link from \STEP to \REF with \ref

\documentclass{article}
\usepackage[verbose]{hyperref}
\usepackage{refcount}[2010/12/01]

\makeatletter
\newcounter{step}
\renewcommand*{\thestep}{Step~\arabic{step}}

\newcommand*{\REF}[1]{%
\begingroup
\refstepcounter{step}%
\IfRefUndefinedBabel{step:#1}{%
}{%
\edef\@currentHref{%
\getrefbykeydefault{step:#1}{anchor}{}%
}%
}%
\label{#1}%
\hyperref[{step:#1}]{\thestep}%
\endgroup
}

\newcommand*{\STEP}[1]{%
\begingroup
\phantomsection
\label{step:#1}%
(\ref*{#1})%
\endgroup
}
\makeatother

\begin{document}

Draw point B \STEP{drawb}. Connect A and B \STEP{connectab}. Draw point A
\STEP{drawa}.

The correct order is \REF{drawa} then \REF{drawb} and finally
\REF{connectab}.

Additional references: \ref{drawa} and \ref{drawb}.

\end{document}


-
An unfortunate thing came up: if I reference for example drawa twice, then the everything becomes messed up. Any ideas how to solve this? – Ali Oct 5 '12 at 20:00
@Ali Example added for additional \refs. It requires an additional LaTeX run, that makes three LaTeX runs. – Heiko Oberdiek Oct 5 '12 at 21:20
Thanks. Unfortunately, Ubuntu 12.04 LTS comes with an older refcount package and the document doesn't compile with that. Perhaps I could upgrade my LaTeX packages but the other co-authors, using institutional machines, can't. Is there a way to put this version of the refcount package under the \$HOME directory and tell LaTeX to use it instead of the global one? Something like here? – Ali Oct 6 '12 at 8:42
– Heiko Oberdiek Oct 6 '12 at 12:44
I have a follow-up question. The discussion was getting too long here so I posted it as a new question here. Could you look at it, please? – Ali Oct 9 '12 at 7:51

Edit: Example adjusted to new information.

\documentclass{article}

\newcounter{steps}

\newcommand*\refstep[1]{%
\newcommand*\step[1]{\hypertarget{step:#1}{step~\ref*{#1}}}

\usepackage{hyperref}
\begin{document}

Draw point B (\step{drawb}). Connect A and B (\step{connectab}).
Draw point A (\step{drawa}).

\newpage% to see that the links work

The correct order is \refstep{drawa} then \refstep{drawb} and finally
\refstep{connectab}.

\end{document}


Original: How about something like this:

\documentclass{article}

\newcounter{steps}
\newcommand*\step[1]{step~\ref{step:#1}}
\newcommand*\refstep[1]{step~\refstepcounter{steps}\thesteps\label{step:#1}}

\begin{document}

Draw point B (\step{drawb}). Connect A and B (\step{connectab}).
Draw point A (\step{drawa}).

The correct order is \refstep{drawa} then \refstep{drawb} and finally
\refstep{connectab}.

\end{document}


-
Yes, the output is correct, thanks. But I also need it to be clickable (link) that is why I am using the hyperref package. Unfortunately your code doesn't compile with hyperref. :( – Ali Oct 1 '12 at 22:19
@Ali: add braces around \ref{}: {\ref{step:#1}} – clemens Oct 1 '12 at 22:25
Almost :) Now the referenced point is clickable, I would need the \REF to be the link. – Ali Oct 1 '12 at 22:28
@Ali: see edited answer – clemens Oct 1 '12 at 22:34
Something is still wird. It seems to jump to itself and not to the referenced location. :( – Ali Oct 1 '12 at 22:38