新的 autoref 类别

新的 autoref 类别

我有一个文档,其中我为示例定义了新环境(下面的 MWE)。在其中,我定义了引用它的前缀,这很简单example:nameofexample

现在,在使用时,\autoref我可以简单地写下\autoref{eq:name}结果,其写法如下Equation 1.37(即在引用的方程编号前加上“方程”一词)。我通过定义

\usepackage{hyperref}
\def\equationautorefname{Equation}

我想用 autorefs 做类似的事情,例如 \def\exampleautorefname{Example}

我怎么做?

MWE 代码:

\documentclass{article}
\usepackage{xcolor}
\definecolor{xblue}{HTML}{4268BD}
\usepackage[most]{tcolorbox}
\tcbset{
    common/.style n args={2}{
        colframe={#1},
        colback={#1!5},
        colbacktitle={#1},
        lower separated=false,
        coltitle=white,
        boxrule=1pt,
        fonttitle=\bfseries,
        enhanced,
        breakable,
        top=8pt,
        before skip=1em,
        after skip=2em,
        attach boxed title to top left={
            yshift=-0.25cm,
            xshift=0.38cm,
        },
        boxed title style={
            boxrule=0pt,
            colframe=white,
            arc=5pt,
            outer arc=4pt,
        },
        separator sign={~~},
        overlay unbroken and last={
            \node[text=white, align=right, rounded corners, fill=#1, xshift=-4mm, minimum height=6mm, anchor=east] at (frame.south east) {#2};
        }
    },
    examplestyle/.style={
        common={xblue}{!},
    },
}
\newtcbtheorem{example}{Example}{examplestyle}{example}

\usepackage{hyperref}
\def\equationautorefname{Equation}
\def\exampleautorefname{Example}

\usepackage{blindtext}

\begin{document}
This is an example box:
\begin{example}{This is an example}{myexample}
    \blindtext[1]
\end{example}

And here is an equation:
\begin{equation}
    a+b=c
    \label{eq:myeq}
\end{equation}

\vspace{1em}
Now I try to autorefence both the equation and the example: \autoref{example:myexample} and \autoref{eq:myeq}. While in the case of the autoref to the equation, it shows ``Eqaution 1``. For the example autoref though, it only shows ``1``. How can I add new categories to autoref so that it instead shows ``Example 1``?
\end{document}

上述代码的编译结果: 在此处输入图片描述

答案1

如果你查看日志,你会看到 hyperref 会告诉你缺少了什么

Package hyperref Warning: No autoref name for `tcb@cnt@example' on input line 9

tcb@cnt@example是 tcolorbox 使用的内部计数器名称。因此添加

\makeatletter
\def\tcb@cnt@exampleautorefname{Example}
\makeatother

相关内容