同一项目有两个 crefname

同一项目有两个 crefname

我想让这个过程更快。我有一个 ,theorem它的crefname。在这个定理中,我有一个枚举列表。我想将每个项目关联到两个不同的标签,以便产生以下结果 在此处输入图片描述

使用这样的代码

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage[colorlinks, citecolor=citegreen, %
linkcolor=red,urlcolor=blue, unicode,psdextra,hypertexnames=false]{hyperref}
\usepackage{cleveref}
\newtheorem{theorem}[section]{Theorem}
\crefname{theorem}{Theorem}{Theorems}
\crefname{enumi}{}{}
\begin{document}
\begin{theorem}\label{thm:theorem}
This theorem contains some items
\begin{enumerate}[label=(\arabic{enumi})]
    \item \label{item:theorem_1} This is the first item. %%<- It is also 
 %%ok if you have to specify the second label, 
 %%like this \item \label{item:theorem_1}\label{thm:theorem_1} This is the first item.
    \item This is the second item.
\end{enumerate}
\end{theorem}
\proof We can prove \cref{item:theorem_1}. \endproof 
In \cref{thm:theorem} we have \cref{thm:theorem_1,thm:theorem_2}. 
\end{document}

代码生成两个不同的引用,第一个包含标签theorem,第二个不包含。如果其中一个标签自动生成会更好,但这不是必须的。

答案1

我相信您需要发出两个\label命令,并带有略微不同的交叉引用。

\documentclass{article}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage[
  colorlinks,
  citecolor=citegreen,
  linkcolor=red,
  urlcolor=blue,
  unicode,
  psdextra,
  hypertexnames=false
]{hyperref}
\usepackage{cleveref}

\newtheorem{theorem}[section]{Theorem}
\crefname{theorem}{Theorem}{Theorems}

\makeatletter
\newcommand{\itemlabel}[4]{%
  % #1 = parent counter
  % #2 = prefix for item label
  % #3 = prefix for parent counter label
  % #4 = label
  \label{#2:#4}%
  \protected@edef\@currentlabel{\csname the#1\endcsname\csname the\@enumctr\endcsname}
  \protected@edef\cref@currentlabel{%
    [#1][\arabic{#1}][]{\csname the#1\endcsname\csname the\@enumctr\endcsname}%
  }%
  \label{#3:#4}%
}
\makeatother

\begin{document}

\begin{theorem}\label{thm:theorem}
This theorem contains some items
\begin{enumerate}[label=\upshape(\arabic*),ref=(\arabic*)]
    \item \itemlabel{theorem}{item}{thm}{theorem_1}
    This is the first item.

    \item \itemlabel{theorem}{item}{thm}{theorem_2}
    This is the second item.
\end{enumerate}
\end{theorem}
\begin{proof}
We can prove \cref{item:theorem_1}.
\end{proof}

In \cref{thm:theorem} we have \cref{thm:theorem_1,thm:theorem_2}.

Also \ref{thm:theorem} and \ref{thm:theorem_1}

\end{document}

在此处输入图片描述

注意:\proof...\endproof这是错误的并且可能产生难以预测的后果。

答案2

我认为这是灵活性zref可能有用的一种问题。有了它,我们可以设置一个新属性来与标签一起存储,以便以后引用它。这样做的一个很大的优点是我们不需要专门为这种情况设置一个特殊的标签命令。它也更简单。

\documentclass{article}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage{zref-clever}
% To match your options.
\zcsetup{
  nameinlink=false,
  cap=true,
}

\ExplSyntaxOn
\makeatletter
\zref@newprop{subthm}{(\arabic{\@enumctr})}
\AddToHook{env/enumerate/before}{
  \tl_if_eq:NnT \@currenvir {theorem}
    {
      \AddToHookNext{env/enumerate/begin}{
        \zcsetup{reftype=theorem}
        \zref@localaddprop{main}{subthm}
      }
    }
}
\makeatother
\ExplSyntaxOff

\usepackage[colorlinks,linkcolor=red]{hyperref}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}\zlabel{thm:theorem}
This theorem contains some items
\begin{enumerate}[label=\upshape(\arabic*),ref=\thetheorem(\arabic*)]
  \item \zlabel{thm:theorem_1}
    This is the first item.

  \item \zlabel{thm:theorem_2}
    This is the second item.
\end{enumerate}
\end{theorem}
\begin{proof}
We can prove \zcref[ref=subthm,noname]{thm:theorem_1}.
\end{proof}

In \zcref{thm:theorem} we have \zcref{thm:theorem_1,thm:theorem_2}.

Also \zcref[noname]{thm:theorem} and \zcref[noname]{thm:theorem_1}.

\end{document}

在此处输入图片描述

相关内容