能不能在定义 label 时指定引用时显示的文字

发布于 2021-11-02 18:41:50

下面代码中定义了“定理”和“推论”,在引用时,希望显示完整的“定理 1 推论 1”。

虽然可以使用 \hyperref 来指定显示的文字,但这样容易出错。

能不能在定义 label 时将相关信息写入 label?

\documentclass[UTF8, 11pt]{ctexart}
\usepackage{amsmath}
\usepackage{hyperref}

\newtheorem{theorem}{定理}[section]
\renewcommand\thetheorem{\arabic{theorem}}
\newtheorem{corollary}{推论}[theorem]
\renewcommand\thecorollary{\arabic{corollary}}

\begin{document}

\section{test}

\begin{theorem}\label{th1}
  S = ah
\end{theorem}

\begin{corollary}\label{co1}
  abcd
\end{corollary}


使用 \textbackslash eqref\{co1\} ,只能得到 编号 \eqref{co1}

如何实现让其显示为如下效果?

\begin{itemize}
  \item \hyperref[th1]{定理 1}
  \item \hyperref[co1]{定理 1 推论 1}
\end{itemize}

使用 hyperref 时需要手工指定显示的文字。

能不能将要显示的文字放在 label 定义的时候?直接使用 \textbackslash ref 来引用。

\end{document}

查看更多

关注者
0
被浏览
2.7k
2 个回答
雾月
雾月 2021-11-03
这家伙很懒,什么也没写!

ntheorem 宏包的 \thref 命令即提供此功能。查看文档第2.7节、3.2.1节、4.1节。
并且兼容已有的定理环境。

\documentclass[11pt]{ctexart}
\usepackage{amsmath}
\usepackage[amsmath,hyperref,thref]{ntheorem} % 注意 amsmath 和 hyperref 选项
\usepackage{hyperref}

\newtheorem{theorem}{定理}[section]
\renewcommand\thetheorem{\arabic{theorem}}
\newtheorem{corollary}{推论}[theorem]
\renewcommand\thecorollary{\arabic{corollary}}

\begin{document}

\section{test}

\begin{theorem}\label{th1}
  S = ah
\end{theorem}

\begin{corollary}\label{co1}
  abcd
\end{corollary}

% 不空白      和有空白
见\thref{th1} 和 \thref{co1}. 
% 之所以出现的是“定理1.1”和“推论1.1.1”是因为ntheorem宏包暂不支持重定义 `\the<theorem>`。

\end{document}

image.png

但是由于 ntheorem 宏包不支持重定义 \the<theorem>,此时可使用 cleveref 宏包的 \crefformat{}{} 命令修改:

% \usepackage{cleveref}
%\renewcommand\the....
\crefformat{theorem}{#2定理 #1#3}
\crefformat{corollary}{#2推论 #1#3}

完整的例子如下:注意此时 ntheorem 宏包是不需要的。

\documentclass[11pt]{ctexart}
\usepackage{amsmath}
% \usepackage[amsmath,thref,hyperref]{ntheorem}
\usepackage{hyperref}
\usepackage{cleveref}
\def\thref{\cref}

\newtheorem{theorem}{定理}[section]
\renewcommand\thetheorem{\arabic{theorem}}
\crefformat{theorem}{#2定理 #1#3}
\newtheorem{corollary}{推论}[theorem]
\renewcommand\thecorollary{\arabic{corollary}}
\crefformat{corollary}{#2推论 #1#3}

\begin{document}

\section{test}

\begin{theorem}\label{th1}
  S = ah
\end{theorem}

\begin{corollary}\label{co1}
  abcd
\end{corollary}

见\thref{th1} 和 \thref{co1}.

\hyperref[th1]{定理 1} 和 \hyperref[co1]{推论 1}

\end{document}

image.png

五行真人
五行真人 2021-11-04
这家伙很懒,什么也没写!

我找到了一个办法,思路如下:

  • 在创建 label 时将文字写入 .aux 文件;
  • 使用 \nameref 命令,从 .aux 中读取文字。(原来使用 \ref 的地方换成 \nameref

具体代码为

\documentclass[UTF8, 11pt, oneside]{ctexbook}
\usepackage{amsmath}
\usepackage{hyperref}

\newtheorem{theorem}{定理}[section]
\renewcommand\thetheorem{\arabic{theorem}}
\newtheorem{corollary}{推论}[theorem]
\renewcommand\thecorollary{\arabic{corollary}}

\makeatletter
% 创建指定名字的label
% 参数1: label的id
% 参数2: label的名字
\newcommand{\namedlabel}[2]{%
  \@bsphack
  \protected@write\@auxout{}{%
    \string\newlabel{#1}{%
        {\@currentlabel}%
        {\thepage}%
        {{#2}}%\@currentlabelname
        {\@currentHref}{}%
      }%
  }%
  \@bsphack
}%
\makeatother

\newcommand{\labeltheorem}[1]{%
  \namedlabel{#1}{定理 \thetheorem}
}

\newcommand{\labelcorollary}[1]{%
  \namedlabel{#1}{定理 \thetheorem 推论 \thecorollary}
}

\begin{document}

\section{test}

\begin{theorem}\labeltheorem{th1}
  S = ah
\end{theorem}

\begin{corollary}\labelcorollary{co1}
  abcd
\end{corollary}

\begin{theorem}\label{th2}
  S = abc
\end{theorem}

\begin{equation}
    x = y  \quad \text{\nameref{co1}}
\end{equation}

\end{document}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览