9

The easiest way to explain what I would like to have is to show the desired output 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.

Ali
  • 883

2 Answers2

9

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 STEP to REF

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 REF to STEP

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}.

Additional reference: \ref{connectab}.

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

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

\end{document}

With additional \ref calls

Heiko Oberdiek
  • 271,626
  • 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 05 '12 at 20:00
  • @Ali Example added for additional \refs. It requires an additional LaTeX run, that makes three LaTeX runs. – Heiko Oberdiek Oct 05 '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 06 '12 at 08:42
  • 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 09 '12 at 07:51
5

Edit: Example adjusted to new information.

\documentclass{article}

\newcounter{steps}

\newcommand*\refstep[1]{%
  \refstepcounter{steps}\label{#1}\hyperlink{step:#1}{step~\arabic{steps}}}
\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}

enter image description here

cgnieder
  • 66,645
  • 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 01 '12 at 22:19
  • @Ali: add braces around \ref{}: {\ref{step:#1}} – cgnieder Oct 01 '12 at 22:25
  • Almost :) Now the referenced point is clickable, I would need the \REF to be the link. – Ali Oct 01 '12 at 22:28
  • @Ali: see edited answer – cgnieder Oct 01 '12 at 22:34
  • Something is still wird. It seems to jump to itself and not to the referenced location. :( – Ali Oct 01 '12 at 22:38
  • @Ali: the jump's just hidden… actually it works – cgnieder Oct 01 '12 at 22:47
  • Unfortunately it jumps to the line below the referenced location. I find it's very confusing that after clicking the link I cannot see the referenced place. – Ali Oct 02 '12 at 08:34
  • @Ali you can change the definition of \step to \newcommand*\step[1]{\raisebox{1em}{\hypertarget{step:#1}{}}step~\ref*{#1}} then you get the jump above the line instead of to the baseline. – cgnieder Oct 02 '12 at 08:47