使用 \autoref 和 tcolorbox

使用 \autoref 和 tcolorbox

有没有办法引用彩色盒子创建定理(使用 创建\newtcbtheorem)使用超链接\autoref

我试图设置\<name>autorefname,就像我对用 创建的定理所做的那样\newtheorem,但它似乎不适用于\newtcbtheorem

\documentclass{article}
\usepackage{hyperref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

% tcolorbox theorem
\newtcbtheorem{evidence}{Evidence}{}{ev}
\newcommand{\evidenceautorefname}{Evidence}

% classic theorem
\newtheorem{classicevidence}{Evidence}
\newcommand{\classicevidenceautorefname}{Evidence}

\begin{document}
Find the most important text
in~\autoref{ev:one} % Does not work correctly
and in~\autoref{ev:two}. % Works as expected

\begin{evidence}{This is the evidence}{one}
    Nothing really important to show here.
\end{evidence}

\begin{classicevidence}
    \label{ev:two}
    This is the classic evidence.
\end{classicevidence}
\end{document}

答案1

处理示例文档时,您会收到警告

Package hyperref Warning: No autoref name for
`tcb@cnt@evidence' on input line 18.

这表明autoref无法识别新环境,并且还向您显示与其关联的内部名称:tcb@cnt@evidence因此,在内部,您的环境名称不是evidencetcb@cnt@evidence,这解释了您的尝试失败的原因。您需要让autoref知道tcb@cnt@evidence

\newcommand\tcb@cnt@evidenceautorefname{Evidence}

代码:

\documentclass{article}
\usepackage{hyperref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

% tcolorbox theorem
\newtcbtheorem{evidence}{Evidence}{}{ev}
\makeatletter
\newcommand\tcb@cnt@evidenceautorefname{Evidence}
\makeatother
% classic theorem
\newtheorem{classicevidence}{Evidence}
\newcommand{\classicevidenceautorefname}{Evidence}

\begin{document}
Find the most important text
in~\autoref{ev:one} % Does not work correctly
and in~\autoref{ev:two}. % Works as expected

\begin{evidence}{This is the evidence}{one}
    Nothing really important to show here.
\end{evidence}

\begin{classicevidence}
    \label{ev:two}
    This is the classic evidence.
\end{classicevidence}
\end{document}

在此处输入图片描述

答案2

tcolorbox包提供了一些很好的钩子,使其与cleveref包协作。解决方案包括 (a) 在设置定理时提供“标签类型”,然后使用指令告诉 cleveref 如何排版该标签类型的交叉引用。最后,可以使包的\crefname命令生成与使用该选项加载包生成的交叉引用相同的交叉引用。\crefcleveref\autorefcleverefnameinlink

有关各种 LaTeX 交叉引用包和宏(包括\autoref和)的详细讨论\cref,请参阅帖子交叉引用包:使用哪个,哪些有冲突以及相关答案。

在此处输入图片描述

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

% tcolorbox theorem
\newtcbtheorem{evidence}{Evidence}{label type=evidence}{ev}

\usepackage[colorlinks=true]{hyperref}

\usepackage[nameinlink]{cleveref} % make \cref emulate look of \autoref
\crefname{evidence}{Evidence}{Evidence}

\begin{document}
\noindent
Find the most important text in~\cref{ev:one}. % Now works as expected

\begin{evidence}{This is the evidence}{one}
    Nothing really important to show here.
\end{evidence}
\end{document}

答案3

您还可以使用您喜欢的计数器\newtcbtheorem。在下面的代码中,classicevidence采用了允许两种环境混合运行的计数器。

或者,使用\newcounter{mynicecounter}并将此计数器\newtcbtheorem赋予use counter=mynicecounter

\documentclass{article}
\usepackage{hyperref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

% classic theorem
\newtheorem{classicevidence}{Evidence}
\newcommand{\classicevidenceautorefname}{Evidence}

% tcolorbox theorem
\newtcbtheorem[use counter=classicevidence]{evidence}{Evidence}{}{ev}

\begin{document}
Find the most important text
in~\autoref{ev:one} % Works as expected
and in~\autoref{ev:two}. % Works as expected

\begin{evidence}{This is the evidence}{one}
    Nothing really important to show here.
\end{evidence}

\begin{classicevidence}
    \label{ev:two}
    This is the classic evidence.
\end{classicevidence}
\end{document}

在此处输入图片描述

相关内容