我们正在创建用枚举表示的场景的用例:
1. Do step 1
2. Do step 2
3. Do step that can fail
某些步骤可能会触发以下列出的替代方案:
3b. The step failed, will try something different.
4b. Notify X from the failure.
为了方便添加/删除步骤,我们使用对具有替代方案的步骤的引用。这是主要场景的源代码:
\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[4b.] Notify X from the failure.
\end{enumerate}
您已经可以发现这个问题了:我需要一种方法来包含跟在后面的枚举号\ref{canfail}
。
这个存在吗?
答案1
这可以借助包\getrefnumber
中的命令来完成refcount
。
定义
\newcommand{\followref}[1]{\the\numexpr\getrefnumber{#1}+1\relax}
并使用如下示例中的方法:
\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}
输出
编辑
这可以改进定义
\newcommand{\followref}[2]{\the\numexpr\getrefnumber{#2}+#1\relax}
并使用
\followref{1}{canfail}
当你想按 1 步进时
\followref{2}{canfail}
当您想以 2 为单位进行步进时。
梅威瑟:
\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}
输出
您也可以将其定义为
\newcommand{\followref}[2][1]{\the\numexpr\getrefnumber{#2}+#1\relax}
因此要使用默认为 1 的可选参数,但在这种情况下,在可选参数中使用时必须将其括在括号中\item
梅威瑟:
\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}
输出与上面相同。
答案2
该解决方案结合了\getrefnumber
(感谢 karlkoeller)和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}