我有一个多级列表。我希望有时能够将二级项目称为 1a,有时则称为 a。我该怎么做?
梅威瑟:
\documentclass{article}
\usepackage{enumitem}
\begin{document}
Here are some questions:
\begin{enumerate}
\item \label{Q:WhatIsBlue} What is blue?
\begin{enumerate}
\item \label{Q:IsBlueRed} Is blue red?
\item \label{Q:IsBlueYellow} Is blue yellow?
\end{enumerate}
\end{enumerate}
Question~\ref{Q:IsBlueRed} is a subquestion of question~\ref{Q:WhatIsBlue}. Also, question~\ref{Q:WhatIsBlue} has two parts, \ref{Q:IsBlueRed} and \ref{Q:IsBlueYellow}. It is good that I can refer to question~\ref{Q:IsBlueRed} as \ref{Q:IsBlueRed}, but sometimes I will need to refer to it as just ``a'', not \ref{Q:IsBlueRed}. How can I do this?
\end{document}
我希望有一种方法可以将划掉的项目仅称为 a 和 b,同时保留引用 1a 和 1b 的能力。
如果可能的话,我不想使用 \ref{Q:WhatIsBlue}\ref{Q:IsBlueRed},因为如果将第二级问题“蓝色是红色吗?”移至另一个第一级问题,则引用将不再有意义。我希望能够移动子问题而不会破坏引用。
答案1
你可以尝试一下zref
。它允许你为标签创建附加属性,你可以在其中存储所需的枚举子计数器的一部分,然后引用它。
\documentclass{article}
\usepackage{enumitem}
\usepackage{zref-user}
\makeatletter
\zref@newprop{enumii}{\alph{enumii}}
\AddToHook{env/enumerate/begin}{%
\zref@iflistcontainsprop{main}{enumii}{}{%
\zref@localaddprop{main}{enumii}%
}%
}
\makeatother
\begin{document}
Here are some questions:
\begin{enumerate}
\item \zlabel{Q:WhatIsBlue} What is blue?
\begin{enumerate}
\item \zlabel{Q:IsBlueRed} Is blue red?
\item \zlabel{Q:IsBlueYellow} Is blue yellow?
\end{enumerate}
\end{enumerate}
Question~\zref{Q:IsBlueRed} is a subquestion of
question~\zref{Q:WhatIsBlue}. Also, question~\zref{Q:WhatIsBlue} has two
parts, \zref[enumii]{Q:IsBlueRed} and \zref[enumii]{Q:IsBlueYellow}. It is
good that I can refer to question~\zref{Q:IsBlueRed} as \zref{Q:IsBlueRed},
but sometimes I will need to refer to it as just
``\zref[enumii]{Q:IsBlueRed}'', not \zref{Q:IsBlueRed}. How can I do this?
\end{document}