Autoref 编号与定理本身显示的编号不同

Autoref 编号与定理本身显示的编号不同

我面临的问题是,在 tcbtheorem 环境本身中,定义的 tcbtheorem 的编号是正确编号的,但 autoref 通常显示不同的数字,或卡在编号中。

一个小例子如下:

\documentclass{book}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}
    
    % hyperref & url package
    \usepackage[
    hidelinks
    ]{hyperref}
    \usepackage{bookmark}
    \usepackage{url}
    
    \usepackage[most]{tcolorbox}
    \newtcbtheorem[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{definition}{Definition}{}{def}
    \makeatletter
    \newcommand\definitionautorefname{Definition}
    \makeatother

\begin{document}

\chapter{First Chapter}
%
\section{First Section}
%
We state in \autoref{def:life} an attitude.
%
\begin{definition}{Life\label{def:life}}
    _Life is fun.
\end{definition}
%
We state in \autoref{def:work} a fact.
%
\begin{definition}{Work\label{def:work}}
    _Work is a necessity.
\end{definition}
\section{Second Section}
%
%
We state in \autoref{def:latex} a truth.
%
\begin{definition}{Latex\label{def:latex}}
    _Latex is not so much fun.
\end{definition}
%
\end{document}

编译后,示例显示 autoref 中的编号已关闭,并且似乎已粘在节上。定义环境中的编号是正确的(参见下图) 自定义环境的虚假自动引用编号

我该怎么做才能让 autoref 使用相应环境中所述的数字来引用定义?

谨致问候,亚历克斯

答案1

您使用了错误的输入语法(您应该在日志中收到有关def_您正在生成的标签的警告)。新环境有强制参数,其中第二个将构建标签:

\documentclass{book}
    % General document formatting
    \usepackage[margin=0.7in]{geometry}
    \usepackage[parfill]{parskip}
    \usepackage[utf8]{inputenc}

    % hyperref & url package
    \usepackage[
    hidelinks
    ]{hyperref}
    \usepackage{bookmark}
    \usepackage{url}

    \usepackage[most]{tcolorbox}
    \newtcbtheorem[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{definition}{Definition}{}{def}
    \makeatletter
    \newcommand\definitionautorefname{Definition}
    \makeatother

\begin{document}

\chapter{First Chapter}
%
\section{First Section}
%
We state in \autoref{def:life} an attitude.
%
\begin{definition}{Life}{life}
    Life is fun.
\end{definition}
%
We state in \autoref{def:work} a fact.
%
\begin{definition}{Work}{work}
    Work is a necessity.
\end{definition}
\section{Second Section}
%
%
We state in \autoref{def:latex} a truth.
%
\begin{definition}{Latex}{latex}
    Latex is not so much fun.
\end{definition}
%
\end{document}

相关内容