Cleverref 是否不仅可以引用环境的编号,还可以引用“名称”(用方括号传递给它的内容)?
在下面的例子中,我想替换此处输入代码
Now since \Cref{boringLemma} we have that \Cref{awesomeTheorem} directly follows.
有一些东西我不明白
Now since Lemma 1.1 we have that Theorem 1.2 directly follows.
但类似
Now since Boring Lemma (1.1) we have that Awesome Theorem (1.2) directly follows.
\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{cleveref}
\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}
\begin{lemma}[Boring Lemma]\label{boringLemma}
This is some boring statement.
\end{lemma}
\begin{thm}[Awesome Theorem]\label{awesomeTheorem}
This is a really awesome statement.
\end{thm}
Now since \Cref{boringLemma} we have that \Cref{awesomeTheorem} directly follows.
\end{document}
答案1
加载thmtools
和nameref
将为您提供此功能。您必须使用\nameref
而不是\cref
,但我认为大多数时候您最能决定要引用的是定理的名称还是编号。
编辑使用命令添加的代码\myref
以及\Myref
单个引用在定义时自动在括号中添加名称。
\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{thmtools}
\usepackage{nameref,cleveref}
\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
\newcommand{\myref}[1]{\cref{#1}\mynameref{#1}{\csname r@#1\endcsname}}
\newcommand{\Myref}[1]{\Cref{#1}\mynameref{#1}{\csname r@#1\endcsname}}
\def\mynameref#1#2{%
\begingroup
\edef\@mytxt{#2}%
\edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
\ifx\@mytst\empty\else
\space(\nameref{#1})\fi
\endgroup
}
\makeatother
\begin{document}
\section{Boring and Awesome stuff}
\begin{lemma}\label{noname}
Extremely important lemma that has no name.
\end{lemma}
\begin{lemma}[Boring Lemma]\label{boringLemma}
This is some boring statement.
\end{lemma}
\begin{thm}[Awesome Theorem]\label{awesomeTheorem}
This is a really awesome statement.
\end{thm}
Now since \Cref{boringLemma} we have that \Cref{awesomeTheorem}
directly follows. Alternatively, we see that \nameref{boringLemma}
leads directly to \nameref{awesomeTheorem}.
Finally we see that \Myref{boringLemma} and \Myref{awesomeTheorem}
have names whereas \Myref{noname} is unnamed despite its indisputable
importance.
\end{document}
答案2
Andrew Swann 的答案是一个很好的解决方案。如果你总是在引理名称等中包含“引理”(如“无聊引理”和“超赞定理”),那么对 Andrew 的代码进行以下修改将产生你要求的格式:
\makeatletter
\newcommand{\myref}[1]{%
\begingroup%
\edef\@mytxt{\csname r@#1\endcsname}%
\edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
\ifx\@mytst\empty\relax%
\cref{#1}%
\else%
\nameref{#1}~\labelcref{#1}%
\fi%
\endgroup}
\newcommand{\Myref}[1]{%
\begingroup%
\edef\@mytxt{\csname r@#1\endcsname}%
\edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
\ifx\@mytst\empty\relax%
\Cref{#1}%
\else%
\nameref{#1}~\labelcref{#1}%
\fi%
\endgroup}
\makeatother
如果在引理名称等中省略“引理”,这也很常见(只是称之为“Boring”而不是“Boring Lemma”和“Awesome”而不是“Awesome Theorem”),那么你需要的是类似这样的内容:
\makeatletter
\newcommand{\myref}[1]{%
\begingroup%
\edef\@mytxt{\csname r@#1\endcsname}%
\edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
\ifx\@mytst\empty\relax%
\cref{#1}%
\else%
\nameref{#1} \cref{#1}%
\fi%
\endgroup}
\newcommand{\Myref}[1]{%
\begingroup%
\edef\@mytxt{\csname r@#1\endcsname}%
\edef\@mytst{\expandafter\@thirdoffive\@mytxt}%
\ifx\@mytst\empty\relax%
\Cref{#1}%
\else%
\nameref{#1} \cref{#1}%
\fi%
\endgroup}
\makeatother
答案3
包ntheorem
和相应的标签不会将环境的名称写入辅助文件。因此您无法使用环境的标签名称。使用 hyperref 的小修复是:
\makeatletter
\def\@ythm#1#2#3[#4]{\def\@currentlabelname{#4}%
\expandafter\global\expandafter\def\csname#1name\endcsname{#4}%
\@opargbegintheorem{#3}{\csname the#2\endcsname}{#4}%
\ifx\thm@starredenv\@undefined
\thm@thmcaption{#1}{{#3}{\csname the#2\endcsname}{#4}}\fi
\ignorespaces}
\DeclareRobustCommand{\Gref}[1]{\nameref{#1}\ \ref{#1}}
\makeatother
\Gref
因此您可以照常使用该命令。我相信 Heiko 对 有更好的定义\Gref
。
\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage[amsmath]{ntheorem}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\def\@ythm#1#2#3[#4]{\def\@currentlabelname{#4}%
\expandafter\global\expandafter\def\csname#1name\endcsname{#4}%
\@opargbegintheorem{#3}{\csname the#2\endcsname}{#4}%
\ifx\thm@starredenv\@undefined
\thm@thmcaption{#1}{{#3}{\csname the#2\endcsname}{#4}}\fi
\ignorespaces}
\DeclareRobustCommand{\Gref}[1]{\nameref{#1}\ \ref{#1}}
\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{lemma}[Boring Lemma]\label{boringLemma}
This is some boring statement.
\end{lemma}
\begin{thm}[Awesome Theorem]\label{awesomeTheorem}
This is a really awesome statement.
\end{thm}
Now since \Cref{boringLemma} we have that \Cref{awesomeTheorem} directly follows.
Now since \nameref{boringLemma} we have that \nameref{awesomeTheorem} directly follows.
Now since \Gref{boringLemma} we have that \Gref{awesomeTheorem} directly follows.
\end{document}