在这篇文章中自定义定理编号有一个解释埃格尔显示了如何实现这一点。现在我希望能够引用,这样我就不必在\ref{}
引用某个特定内容之前键入引理/命题等\label{}
,因为使用\ref{}
它只会给出编号。但是,使用cref{}
cleveref 包中的只会在编译后在页面上给出问号(我对此并不感到惊讶,因为它是一个独立于给出的代码的包埃格尔)如何解决这个问题?
\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{xparse}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{enumerate}
\usepackage{xcolor}
\usepackage[shortlabels]{enumitem}
\usepackage{cleveref}
%===========================================================%
%The code below customises theorem numbering
%===========================================================%
\newtheorem{innercustomgeneric}{\customgenericname}
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
\newenvironment{#1}[1]
{%
\renewcommand\customgenericname{#2}%
\renewcommand\theinnercustomgeneric{##1}%
\innercustomgeneric
}
{\endinnercustomgeneric}
}
\newcustomtheorem{customdefinition}{Definition}
\newcustomtheorem{customlemma}{Lemma}
\newcustomtheorem{customproposition}{Proposition}
%===========================================================%
\begin{document}
\begin{customproposition}{5.2} \label{hello}
blah blah
\end{customproposition}
I want to Proposition \ref{hello} without
having to type proposition
at the beginning of \ref{hello}.
\end{document}
有没有一种有效而干净的方法来做到这一点?是通过 cleveref 还是其他替代方法,通过编写特定代码来引用,\label{}
而不必在引用前面写上“命题”?
答案1
添加cleveref
计数器类型和别名。
\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{xparse}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{enumerate}
\usepackage{xcolor}
\usepackage[shortlabels]{enumitem}
\usepackage{cleveref}
%===========================================================%
%The code below customises theorem numbering
%===========================================================%
\newtheorem{innercustomgeneric}{\customgenericname}
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
\crefname{#2}{#2}{#2s}%
\newenvironment{#1}[1]
{%
\renewcommand\customgenericname{#2}%
\crefalias{innercustomgeneric}{#2}%
\renewcommand\theinnercustomgeneric{##1}%
\innercustomgeneric
}
{\endinnercustomgeneric}
}
\newcustomtheorem{customdefinition}{Definition}
\newcustomtheorem{customlemma}{Lemma}
\newcustomtheorem{customproposition}{Proposition}
%===========================================================%
\begin{document}
\begin{customproposition}{5.2} \label{hello}
blah blah
\end{customproposition}
\begin{customdefinition}{12.4} \label{helloagain}
blah blah
\end{customdefinition}
I want \cref{hello} without having to type ``proposition''.
Also \cref{helloagain}.
\end{document}
答案2
像这样?定义一个new command \pref
,它“打印”“Proposition”和 \ref 。
备注:当您使用这样的标签environment:label
(如fig:drawing
)时也会起作用,当您拥有许多不同类型的环境时,它很有用。
\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
%\usepackage{xparse}
\usepackage{mathtools}
\usepackage{ulem}
\usepackage{enumerate}
\usepackage{xcolor}
\usepackage[shortlabels]{enumitem}
\usepackage{cleveref}
%===========================================================%
%The code below customises theorem numbering
%===========================================================%
\newtheorem{innercustomgeneric}{\customgenericname}
\providecommand{\customgenericname}{}
\newcommand{\newcustomtheorem}[2]{%
\newenvironment{#1}[1]
{%
\renewcommand\customgenericname{#2}%
\renewcommand\theinnercustomgeneric{##1}%
\innercustomgeneric
}
{\endinnercustomgeneric}
}
\newcustomtheorem{customdefinition}{Definition}
\newcustomtheorem{customlemma}{Lemma}
\newcustomtheorem{customproposition}{Proposition}
%~~~ new ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~% <<==
\newcommand{\pref}[1]{Proposition \ref{#1}}
%===========================================================%
\begin{document}
\begin{customproposition}{5.2} \label{hello}
blah blah
\end{customproposition}
I want to Proposition \ref{hello} without
having to type proposition
at the beginning of \ref{hello}. \\
Is this the way you want to make a call to \pref{hello}?% <<==
\end{document}
结果: