# Refer to the item that follows a certain label/reference

We are creating use cases with a scenario represented by an enumeration:

1. Do step 1
2. Do step 2
3. Do step that can fail


Some steps can trigger an alternative scenario that are listed as follows:

3b. The step failed, will try something different.
4b. Notify X from the failure.


In order to conveniently add/remove steps, we use references to the steps that have alternatives. This is the source code for the main scenario:

\begin{enumerate}
\item Do step 1
\item Do step 2
\item \label{canfail} Do step that can fail
\end{enumerate}


For the alternative scenario, the following works if there is only a single step:

\begin{enumerate}
\item[\ref{canfail}b.] The step failed, will try something different.
\item[4b.] Notify X from the failure.
\end{enumerate}


You can spot the issue already: I need a way to include the enumeration number that follows the one of \ref{canfail}.

Does this exist?

• Which documentclass do you use? – Ignasi Mar 2 '15 at 10:50
• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Mar 2 '15 at 10:51
• @Ignasi article. Is that relevant/important? – Steven Roose Mar 2 '15 at 10:55
• beamer and enumitem are not very good friends. – Ignasi Mar 2 '15 at 11:09
• @StevenRoose, it is always best to post full minimal examples that people can just copy and paste and try on their own systems without having to add anything. As Ignasi mentions, the solutions provided may depend on the packages or classes being used. – daleif Mar 2 '15 at 11:59

This can be done with the help of the command \getrefnumber from the refcount package.

Define

\newcommand{\followref}[1]{\the\numexpr\getrefnumber{#1}+1\relax}


and use like in the following example:

\documentclass{article}
\usepackage{refcount,calc}

\newcommand{\followref}[1]{\the\numexpr\getrefnumber{#1}+1\relax}

\begin{document}
\begin{enumerate}
\item Do step 1
\item Do step 2
\item \label{canfail} Do step that can fail
\end{enumerate}
\begin{enumerate}
\item[\ref{canfail}b.] The step failed, will try something different.
\item[\followref{canfail}b.] Notify X from the failure.
\end{enumerate}
\end{document}


Output

## EDIT

This can be improved defining

\newcommand{\followref}[2]{\the\numexpr\getrefnumber{#2}+#1\relax}


and using

\followref{1}{canfail}


when you want to step by 1 and

\followref{2}{canfail}


when you want to step by 2.

MWE:

\documentclass{article}
\usepackage{refcount,calc}

\newcommand{\followref}[2]{\the\numexpr\getrefnumber{#2}+#1\relax}

\begin{document}
\begin{enumerate}
\item Do step 1
\item Do step 2
\item \label{canfail} Do step that can fail
\end{enumerate}
\begin{enumerate}
\item[\ref{canfail}b.] The step failed, will try something different.
\item[\followref{1}{canfail}b.] Notify X from the failure.
\item[\followref{2}{canfail}b.] Do something else.
\end{enumerate}
\end{document}


Output

You can also define it as

\newcommand{\followref}[2][1]{\the\numexpr\getrefnumber{#2}+#1\relax}


so to use an optional argument which defaults to 1, but in this case you have to enclose it in braces when used in the optional argument of \item

MWE:

\documentclass{article}
\usepackage{refcount,calc}

\newcommand{\followref}[2][1]{\the\numexpr\getrefnumber{#2}+#1\relax}

\begin{document}
\begin{enumerate}
\item Do step 1
\item Do step 2
\item \label{canfail} Do step that can fail
\end{enumerate}
\begin{enumerate}
\item[\ref{canfail}b.] The step failed, will try something different.
\item[\followref{canfail}b.] Notify X from the failure.
\item[{\followref[2]{canfail}}b.] Do something else.
\end{enumerate}
\end{document}


The output is the same as above.

• Thanks! Does this work incrementally? Like \followref{\followref{\ref{...}}}? – Steven Roose Mar 2 '15 at 11:06
• @StevenRoose See the edit – karlkoeller Mar 2 '15 at 11:49

This solution combines \getrefnumber (thanks to karlkoeller) and enumitem.

\documentclass{article}
\usepackage{enumitem,refcount}

\begin{document}

\begin{enumerate}
\item Do step 1
\item Do step 2
\item \label{canfail} Do step that can fail
\end{enumerate}

\begin{enumerate}[start=\getrefnumber{canfail}, label=\arabic*{}b.]
\item The step failed, will try something different.
\item Notify X from the failure.
\item Notify X and Y from the failure.
\end{enumerate}
\end{document}