引用方程的问题

引用方程的问题

我正在尝试编写宏来呈现几种类型的引用,并且除了方程式之外,它们都可以正常工作。\namecref{eq:1}给我“??”而不是方程式,并\nameref{eq:1}给我封闭环境的描述而不是空字符串。下面的文档包含我的宏和示例文本,显示了问题。

   \documentclass{amsart}

   \usepackage{ifthen}

   % This should be the last package
   \usepackage [colorlinks] {hyperref}
   \usepackage{cleveref}

   \newcommand \fullref [1]
      { \ifthenelse {\equal{\nameref{#1}}{}}
          {\cref{#1}}
          {\cref{#1} (\nameref{#1})}
      }

   \newcommand \fullrefrange [2]
      {\namecrefs{#1}
        \ifthenelse {\equal{\nameref{#1}}{}}
          {\ref{#1}}
          {\ref{#1} (\nameref{#1})}
        to
        \ifthenelse {\equal{\nameref{#2}}{}}
          {\ref{#2}}
          {\ref{#2} (\nameref{#2})}
      }

   \newcommand \Pageref [1]
      { \ifthenelse {\equal{\nameref{#1}}{}}
          {\cref{#1} on p.~\pageref{#1}}
          {\cref{#1} (\nameref{#1}) on p.~\pageref{#1}}
      }

   \newcommand \refrange [2]
     {
       \ref{#1}
       to
       \ref{2}
     }

   \newtheorem{theorem}{Theorem}[section]
   \newtheorem{lemma}[theorem]{Lemma}

   \theoremstyle{definition}
   \newtheorem{corollary}{Corollary}
   \newtheorem{definition}[theorem]{Definition}
   \newtheorem{example}[theorem]{Example}
   \newtheorem{xca}[theorem]{Exercise}

   \theoremstyle{remark}
   \newtheorem{remark}[theorem]{Remark}

   \numberwithin{equation}{section}

   \begin{document}

   \begin{definition}[A definition]
   \label{def:1}
   This is a definition.
   \end{definition}

   \begin{lemma}[A lemma]
   \label{lem:1}
   This is a lemma.
   \begin{enumerate}
   \item
   \label{it:rishon}
   first
   \item
   \label{it:sheini}
   second
   \end{enumerate}
   \end{lemma}

   \begin{corollary}[A corollary]
   \label{cor:1}
   This is a corollary.
   \end{corollary}

   \begin{corollary}[Another corollary]
   \label{cor:2}
   This is  another corollary.
   \end{corollary}

   \begin{theorem}[A theorem]
   \label{thm:1}
   This is a theorem.
   \begin{subequations}
   \begin{equation}
   \label{eq:1}
   first
   \end{equation}
   \begin{equation}
   \label{eq:2}
   second
   \end{equation}
   \end{subequations}
   \end{theorem}

   \begin{flushleft}
   autoref\{cor:1\} = \autoref{cor:1} \\
   cref\{cor:1\} = \cref{cor:1} \\
   crefrange\{cor:1\}\{cor:2\} = \crefrange{cor:1}{cor:2} \\
   fullrefrange\{cor:1\}\{cor:2\} = \fullrefrange{cor:1}{cor:2}
   \end{flushleft}

   \begin{flushleft}
   autoref\{def:1\} = \autoref{def:1} \\
   cref\{def:1\} = \cref{def:1} \\
   fullref\{def:1\} = \fullref{def:1} \\
   namecref\{def:1\} = \namecref{def:1} \\
   namecrefs\{def:1\} = \namecrefs{def:1}
   \end{flushleft}

   \begin{flushleft}
   crefrange\{eq:1\}\{eq:2\} = \crefrange{eq:1}{eq:2} \\
   fullref\{eq:1\} = \fullref{eq:1} \\
   fullrefrange\{eq:1\}\{eq:2\} = \fullrefrange{eq:1}{eq:2} \\
   labelcref\{eq:1\} = \labelcref{eq:1} \\
   namecref\{eq:1\} = \namecref{eq:1} \\
   namecrefs\{eq:1\} = \namecrefs{eq:1} \\
   nameref\{eq:1\} = \nameref{eq:1}
   \end{flushleft}

   \begin{flushleft}
   autoref\{it:sheini\} = \autoref{it:sheini} \\
   cref\{it:sheini\} = \cref{it:sheini} \\
   cref\{it:rishon,it:sheini\} = \cref{it:rishon,it:sheini} \\
   crefrange\{it:rishon\}\{it:sheini\} = \crefrange{it:rishon}{it:sheini} \\
   labelcref\{it:rishon\} = \labelcref{it:rishon}
   \end{flushleft}

   \begin{flushleft}
   autoref\{lem:1\} = \autoref{lem:1} \\
   cref\{lem:1\} = \cref{lem:1} \\
   Pageref\{lem:1\} = \Pageref{lem:1}
   \end{flushleft}

   \begin{flushleft}
   autoref\{thm:1\} = \autoref{thm:1} \\
   cref\{thm:1\} = \cref{thm:1}
   \end{flushleft}

   \end{document}

答案1

您需要提供说明

\crefname{equation}{equation}{equations}
\crefname{subequation}{equation}{equations}

加载包后cleveref

对于定理类环境,没有必要这样做,因为它cleveref足够聪明(抱歉,无法抗拒双关语),可以从各种指令中确定theoremlemma、等环境的“名称”。由于和环境没有这样的设置,因此您需要通过指令提供所需的信息。definition\newtheoremequationsubequation\crefname

MWE:

在此处输入图片描述

\documentclass{article} % or: "amsart"
\usepackage{ifthen}
\usepackage{amsthm,amsmath} % not required if "amsart" document class is used

% These should be the last two packages to be loaded:
\usepackage [colorlinks] {hyperref}
\usepackage{cleveref}

% Further setup instructions
\crefname{equation}{equation}{equations}
\crefname{subequation}{equation}{equations}

\newtheorem{theorem}{Theorem}[section]
\numberwithin{equation}{section}

% Custom cross-referencing macros
\newcommand \fullref [1]
{ 
  \ifthenelse {\equal{\nameref{#1}}{}}{\cref{#1}}{\cref{#1} (\nameref{#1})}
}
\newcommand \fullrefrange [2]
{\namecrefs{#1}
  \ifthenelse {\equal{\nameref{#1}}{}}{\ref{#1}}{\ref{#1} (\nameref{#1})}
  to
  \ifthenelse {\equal{\nameref{#2}}{}}{\ref{#2}}{\ref{#2} (\nameref{#2})}
}
\newcommand \Pageref [1]
{ \ifthenelse {\equal{\nameref{#1}}{}}
  {\cref{#1} on p.~\pageref{#1}}{\cref{#1} (\nameref{#1}) on p.~\pageref{#1}}
}
\newcommand \refrange [2]{\ref{#1} to \ref{2}}

\begin{document}
\setcounter{section}{1}

\begin{theorem}[A theorem] \label{thm:1}
This is a theorem.
\begin{subequations}
  \begin{equation}\label{eq:1} \textnormal{first} \end{equation}
  \begin{equation}\label{eq:2} \textnormal{second}\end{equation}
\end{subequations}
\end{theorem}

\noindent
\begin{tabular}{@{}ll}
\verb+\crefrange{eq:1}{eq:2}+& \crefrange{eq:1}{eq:2} \\
\verb+\fullref{eq:1}+        & \fullref{eq:1} \\
\verb+\fullrefrange{eq:1}{eq:2}+& \fullrefrange{eq:1}{eq:2}\\
\verb+\labelcref{eq:1}+      & \labelcref{eq:1} \\
\verb+\namecref{eq:1}+       & \namecref{eq:1} \\
\verb+\namecrefs{eq:1}+      & \namecrefs{eq:1} \\
\verb+\nameref{eq:1}+        & \nameref{eq:1}
\end{tabular}
\end{document}

相关内容