4

I have tried (with partial success) to modify the solution here to the question: Cleveref and named theorems.

The idea is that I want to overload cleverref's Cref (and also cref) to give the name of theorems, lemmas, etc. where they are given. However, while I have achieved this, I want the behaviour unmodified for equations, tables, figures, listings, sections, etc.

NB: I would like to overload \Cref, and not have to introduce a new command if possible.

MWE

\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}

Output

enter image description here

2
  • 1
    It's probably just a typo in the example, but on the off chance that it is the same in your document: It should be 'Fatou' without the 'a'. (And I know the result as 'Fatou's lemma' and not 'theorem', but that might be a matter of taste...)
    – moewe
    Commented Jun 14, 2018 at 12:28
  • @moewe, It's just the two people I could think of with a theorem and lemma and I put them in the wrong place (with a typo). Fairly inconsequential.
    – oliversm
    Commented Jun 14, 2018 at 12:39

2 Answers 2

3

Here is a solution that seems to work. I have changed the overall implementation a bit, but the main bit is that we now check for the type of reference and only act if it is the right type. The type is extracted using the internal cleveref macro \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}

enter image description here

1
2

Based on https://tex.stackexchange.com/a/319418/36296 and https://tex.stackexchange.com/a/336063/36296

Another possibility is to modify the crefformat for lemmas:

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .