Clever Ref 仅给出定理名称。

Clever Ref 仅给出定理名称。

我尝试(部分成功)修改解决方案这里这个问题:Cleveref 和命名定理

我的想法是,我想重载 cleverref Cref(以及cref),以便在给出定理、引理等的地方给出它们的名称。但是,虽然我已经实现了这一点,但我希望方程式、表格、图形、列表、部分等的行为不做修改。

注意:我想重载\Cref,并且如果可能的话不必引入新命令。

平均能量损失

\documentclass[a4paper,10pt]{extarticle}

\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{thmtools}
\usepackage{nameref,cleveref}
\usepackage{xspace}

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

\crefname{lemma}{lemma}{lemmas}
\Crefname{lemma}{Lemma}{Lemmas}
\crefname{thm}{theorem}{theorems}
\Crefname{thm}{Theorem}{Theorems}

\makeatletter  %% My attempt at achieving the effect. 
\let\crefOrig\cref
\let\CrefOrig\Cref
\renewcommand{\cref}[1]{\mynameref{#1}{\csname r@#1\endcsname}}
\renewcommand{\Cref}[1]{\Mynameref{#1}{\csname r@#1\endcsname}}
\def\Mynameref#1#2{%
    \begingroup
    \edef\@mytxt{#2}%
    \edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
    \ifx\@mytst\empty\CrefOrig{#1}\else
    \nameref{#1}\xspace(\CrefOrig{#1})\fi
    \endgroup
}
\def\mynameref#1#2{%
    \begingroup
    \edef\@mytxt{#2}%
    \edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
    \ifx\@mytst\empty\crefOrig{#1}\else
    \nameref{#1}\xspace(\crefOrig{#1})\fi
    \endgroup
}
\makeatother


\begin{document}
    \section{Boring and Awesome stuff}
    \label{sec}

    \begin{figure}[htb]
        \centering abc
        \caption[something]{something more}
        \label{fig}
    \end{figure}

    \begin{equation}
    abc
    \label{eqt}
    \end{equation}

    \begin{table}[htb]
        \centering abc
        \caption[content]{content...}
        \label{tab}
    \end{table}

    \begin{lemma}\label{noname}
        Extremely important lemma that has no name.
    \end{lemma}

    \begin{lemma}[Fubini's lemma]\label{boringLemma}
        This is some boring statement.
    \end{lemma}

    \begin{thm}[Fataou's Theorem]\label{awesomeTheorem}
        This is a really awesome statement.
    \end{thm}

    We have \Cref{boringLemma} and \Cref{awesomeTheorem}. Whereas \Cref{noname} doesn't have anything interesting.

    We have \cref{boringLemma} and \cref{awesomeTheorem}. Whereas \cref{noname} doesn't have anything interesting.

    Other environments go wrong. \hfill \textbf{what I want}\\
    \Cref{fig} \hfill \CrefOrig{fig}\\
    \Cref{eqt} \hfill \CrefOrig{eqt}\\
    \Cref{sec} \hfill \CrefOrig{sec}\\
    \Cref{tab} \hfill \CrefOrig{tab}

\end{document}

输出

在此处输入图片描述

答案1

这是一个似乎可行的解决方案。我稍微改变了整体实现,但主要的是我们现在检查引用的类型,并且只有当它是正确的类型时才采取行动。使用内部宏提取cleveref类型\cref@gettype

\documentclass[a4paper,10pt]{extarticle}

\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{thmtools}
\usepackage{nameref,cleveref}

\usepackage{etoolbox}
\makeatletter
\renewrobustcmd{\cref}{\@osmcref{cref}}
\renewrobustcmd{\Cref}{\@osmcref{Cref}}
\def\@osmcref#1#2{%
  \begingroup
    \ifcsundef{r@#2}
      {}
      {\expandafter\expandafter\expandafter\expandafter\expandafter
         \expandafter\expandafter\def
       \expandafter\expandafter\expandafter\expandafter\expandafter
         \expandafter\expandafter\@osmcref@name
       \expandafter\expandafter\expandafter\expandafter\expandafter
         \expandafter\expandafter{%
         \expandafter\expandafter\expandafter
         \@thirdoffive\csname r@#2\endcsname}}%
    \ifcsundef{r@#2@cref}
      {}
      {\cref@gettype{#2}{\@osmcref@type}}%
    \ifboolexpr{not test {\ifdefvoid{\@osmcref@name}}
                and (test {\ifdefstring{\@osmcref@type}{thm}}
                     or test {\ifdefstring{\@osmcref@type}{lemma}})}
      {\nameref{#2} (\@cref{#1}{#2})}
      {\@cref{#1}{#2}}%
    \endgroup
}
\makeatother


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

\crefname{lemma}{lemma}{lemmas}
\Crefname{lemma}{Lemma}{Lemmas}
\crefname{thm}{theorem}{theorems}
\Crefname{thm}{Theorem}{Theorems}


\begin{document}
    \section{Boring and Awesome stuff}
    \label{sec}

    \begin{figure}[htb]
        \centering abc
        \caption[something]{something more}
        \label{fig}
    \end{figure}

    \begin{equation}
    abc
    \label{eqt}
    \end{equation}

    \begin{table}[htb]
        \centering abc
        \caption[content]{content...}
        \label{tab}
    \end{table}

    \begin{lemma}\label{noname}
        Extremely important lemma that has no name.
    \end{lemma}

    \begin{lemma}[Fubini's lemma]\label{boringLemma}
        This is some boring statement.
    \end{lemma}

    \begin{thm}[Fatou's Theorem]\label{awesomeTheorem}
        This is a really awesome statement.
    \end{thm}

    We have \Cref{boringLemma} and \Cref{awesomeTheorem}. Whereas \Cref{noname} doesn't have anything interesting.

    We have \cref{boringLemma} and \cref{awesomeTheorem}. Whereas \cref{noname} doesn't have anything interesting.

    Other environments go wrong. \hfill \textbf{what I want}\\
    \Cref{fig} \hfill \Cref{fig}\\
    \Cref{eqt} \hfill \Cref{eqt}\\
    \Cref{sec} \hfill \Cref{sec}\\
    \Cref{tab} \hfill \Cref{tab}

\end{document}

在此处输入图片描述

答案2

基于https://tex.stackexchange.com/a/319418/36296https://tex.stackexchange.com/a/336063/36296

另一种可能性是修改crefformat引理:

\documentclass[a4paper,10pt]{extarticle}

\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{thmtools}
\usepackage{nameref,cleveref}
\usepackage{xspace}
\usepackage{etoolbox}
\usepackage{xstring}

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

\crefname{lemma}{lemma}{lemmas}
\Crefname{lemma}{Lemma}{Lemmas}
\crefname{thm}{theorem}{theorems}
\Crefname{thm}{Theorem}{Theorems}

\makeatletter
\def\cref@getref#1#2{%
  \xdef\@lastusedlabel{#1}%
  \expandafter\let\expandafter#2\csname r@#1@cref\endcsname%
  \expandafter\expandafter\expandafter\def%
    \expandafter\expandafter\expandafter#2%
    \expandafter\expandafter\expandafter{%
      \expandafter\@firstoftwo#2}}%

\newcommand\enameref[1]{%
  \expandafter\ifx\csname r@#1\endcsname\relax
  ??\typeout{^^JLaTeX Warning: Reference #1 undefined on input line \the\inputlineno}%
  \else
    \expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname
  \fi
}    

\Crefformat{lemma}{%
    \IfStrEq{\enameref{\@lastusedlabel}}{}{%
        \Cref@lemma@name~#1%
    }{%
        \nameref{\@lastusedlabel} (#1)%
    }%
}

\crefformat{lemma}{%
    \IfStrEq{\enameref{\@lastusedlabel}}{}{%
        \cref@lemma@name~#1%
    }{%
        \nameref{\@lastusedlabel} (#1)%
    }%
}
\makeatother



\begin{document}

    \section{Boring and Awesome stuff}
    \label{sec}

    \begin{figure}[htb]
        \centering abc
        \caption[something]{something more}
        \label{fig}
    \end{figure}

    \begin{equation}
    abc
    \label{eqt}
    \end{equation}

    \begin{table}[htb]
        \centering abc
        \caption[content]{content...}
        \label{tab}
    \end{table}

    \begin{lemma}\label{noname}
        Extremely important lemma that has no name.
    \end{lemma}

    \begin{lemma}[Fubini's lemma]\label{boringLemma}
        This is some boring statement.
    \end{lemma}

    \begin{thm}[Fataou's Theorem]\label{awesomeTheorem}
        This is a really awesome statement.
    \end{thm}

    We have \Cref{boringLemma} and \Cref{awesomeTheorem}. Whereas \Cref{noname} doesn't have anything interesting.

    We have \cref{boringLemma} and \cref{awesomeTheorem}. Whereas \cref{noname} doesn't have anything interesting.

    Other environments go wrong. \hfill \textbf{what I want}\\
    \Cref{fig} \hfill \Cref{fig}\\
    \Cref{eqt} \hfill \Cref{eqt}\\
    \Cref{sec} \hfill \Cref{sec}\\
    \Cref{tab} \hfill \Cref{tab}

\end{document}

相关内容