如何仅显示对 tcolorbox 的 cleveref 引用的标题?

如何仅显示对 tcolorbox 的 cleveref 引用的标题?

虽然\cref{vip:1-4}---wherevip指的是tcolorbox---will typoset asAlert 1-4 ∞ is not a number\labelcrefwill typoset as 1-4 ∞ is not a number,但有时我想更进一步,只排版∞ is not a number

以下是 MWE:

\documentclass[11pt]{book}
\usepackage[hyperfootnotes=false]{hyperref}%
\usepackage{cleveref}%
\usepackage{tcolorbox}%
    \newcounter{vip}% 
    \newtcolorbox%
    [%
    use counter= vip,
    number within=chapter,
    number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitlealert},%.
    crefname={Alert}{Alert},
    Crefname={Alert}{Alerts},
    ]%
    {mportant}%
    [2][]%
    {%
    detach title,%
    before upper={\tcbtitle\quad},
    fonttitle=\bfseries,
    coltitle=black,
    code={\gdef\mytitlealert{#2}},%
    title=ALERT \thetcbcounter,%
    #1,
    }%
\begin{document}
\chapter{One}
\begin{mportant}[label={vip:1-4}]{$\infty$ is not a number}
\hspace{-3mm}and we cannot compute with $\infty$.
\end{mportant}
\chapter{Two}
We cannot input $\infty$ because \labelcref{vip:1-4}  (\Cpageref{vip:1-4})
\end{document}

并且,偶尔我不希望1.1在参考文献的开头显示参考编号:

在此处输入图片描述

答案1

我建议使用nameref=选项tcolorbox(于 2016/6/22 推出(但 CHANGES 文件显示为 2016/7/14),版本 3.94)和而\nameref不是\labelcref\cref,因为cleveref这个特殊标题不需要该功能。

我使用是nameref={#2}因为基本上只应显示标题而不显示其他内容。

\documentclass[11pt]{book}
\usepackage[hyperfootnotes=false]{hyperref}%
\usepackage{tcolorbox}%
\newcounter{vip}% 
\usepackage{cleveref}%


\newtcolorbox%
[%
use counter= vip,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitlealert},%.
crefname={Alert}{Alert},
Crefname={Alert}{Alerts},
]%
{mportant}%
[2][]%
{%
  detach title,%
  before upper={\tcbtitle\quad},
  fonttitle=\bfseries,
  coltitle=black,
  nameref={#2},
  code={\gdef\mytitlealert{#2}},
  title={ALERT \thetcbcounter},%
  #1,
}%
\begin{document}
\chapter{One}
\begin{mportant}[label={vip:1-4}]{$\infty$ is not a number}
\hspace{-3mm}and we cannot compute with $\infty$.
\end{mportant}
\chapter{Two}
We cannot input $\infty$ because \nameref{vip:1-4}  (\Cpageref{vip:1-4})
\end{document}

在此处输入图片描述

万一tcolorbox版本明显过时,可以应用以下by-pass解决方案,即在选项中定义\@currentlabelname(由使用\namerefcode={}——当然,\makeatletter...\makeatother除非框的定义写在.sty.cls文件中,否则这是必要的。

\documentclass[11pt]{book}
\usepackage[hyperfootnotes=false]{hyperref}%
\usepackage{tcolorbox}%
\newcounter{vip}% 
\usepackage{cleveref}%


\makeatletter
\newtcolorbox%
[%
use counter= vip,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitlealert},%.
crefname={Alert}{Alert},
Crefname={Alert}{Alerts},
]%
{mportant}%
[2][]%
{%
  detach title,%
  before upper={\tcbtitle\quad},
  fonttitle=\bfseries,
  coltitle=black,
  code={\gdef\mytitlealert{#2}\edef\@currentlabelname{#2}},%
  title={ALERT \thetcbcounter},%
  #1,
}%
\makeatother
\begin{document}
\chapter{One}
\begin{mportant}[label={vip:1-4}]{$\infty$ is not a number}
\hspace{-3mm}and we cannot compute with $\infty$.
\end{mportant}
\chapter{Two}
We cannot input $\infty$ because \nameref{vip:1-4}  (\Cpageref{vip:1-4})
\end{document}

相关内容