我正在为初学者写一篇文章,我不希望引用太多。这个问题最好从 MWE 的角度来看
以下是代码
\documentclass[11pt]{book}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{tcolorbox}
\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={alert}{alerts},
Crefname={Alert}{Alerts},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=ALERT \thetcbcounter,%
#1,%Required for references.
}%
\begin{document}
\chapter{ONE}\label{1-0-0}
\section{One}\label{1-1-0}
\subsection{one}\label{1-1-1}
\emph{Relative to the title of the tcolorbox}
\begin{greement}[label={abc:3-2}]{infinity}
is not a number
\end{greement}
\emph{cref gives}
The \cref{abc:3-2} is most important
\emph{while Cref gives}
\Cref{abc:3-2} is most important
\emph{But I would also like to control the case of the title and, while nameref gives}
We can see \nameref{abc:3-2}
\emph{Nameref gives}
\Nameref{abc:3-2} can be seen.
\emph{instead of the desired}
Infinity can be seen.
\subsection{infinity}\label{1-1-2}
\emph{Similarly, relative to the title of the subsection, nameref gives}
Since \nameref{1-1-2} is not a number, ...
\emph{but, I would also like to have it also with an upper case}
Infinity not being a number, ...
\emph{but Nameref does do it}: \Nameref{1-1-2}
\end{document}
答案1
我定义了一个名为 的新宏\NameRef
,它不同于\Nameref
提供的宏hyperref
——它使用 ,\FirstUpcase
它是一个expl3
- 定义的命令,只使用大写字母作为参数的第一个字符,用 提取\getrefkeybydefault{#1}{name}{-1}
。如果引用不存在,则-1
进行排版。
带星号的版本\NameRef*
不会添加超链接。
\documentclass[11pt]{book}
\usepackage{xparse}
\usepackage{tcolorbox}
\usepackage{hyperref}
\usepackage{cleveref}
\ExplSyntaxOn
\cs_generate_variant:Nn \str_head:n {x,o,V}
\cs_generate_variant:Nn \str_tail:n {x,o,V}
\cs_generate_variant:Nn \str_uppercase:n {x,o,V}
\cs_generate_variant:Nn \str_set:Nn {Nx,No}
\newcommand{\FirstUpcase}[1]{%
\str_set:Nx \l_tmpa_str {#1}%
\str_uppercase:x {\str_head:N \l_tmpa_str}\str_tail:V {\l_tmpa_str}%
}
\ExplSyntaxOff
\NewDocumentCommand{\NameRef}{sm}{%
\IfBooleanTF{#1}{%
\FirstUpcase{\getrefbykeydefault{#2}{name}{-1}}%
}{%
\IfRefUndefinedExpandable{#2}{}{%
% label is defined, now get hyperlink
\hyperlink{\getrefbykeydefault{#2}{anchor}{-1}}{\FirstUpcase{\getrefbykeydefault{#2}{name}{-1}}}
}%
}%
}
\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={alert}{alerts},
Crefname={Alert}{Alerts},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=ALERT \thetcbcounter,%
#1,%Required for references.
}%
\begin{document}
\chapter{ONE}\label{1-0-0}
\section{One}\label{1-1-0}
\subsection{one}\label{1-1-1}
\emph{Relative to the title of the tcolorbox}
\begin{greement}[label={abc:3-2}]{infinity}
is not a number
\end{greement}
\emph{cref gives}
The \cref{abc:3-2} is most important
\emph{while Cref gives}
\Cref{abc:3-2} is most important
\emph{But I would also like to control the case of the title and, while nameref gives}
We can see \nameref{abc:3-2}
\emph{Nameref gives}
\Nameref{abc:3-2} can be seen.
\emph{instead of the desired}
Infinity can be seen.
\subsection{infinity}\label{1-1-2}
\emph{Similarly, relative to the title of the subsection, nameref gives}
Since \nameref{1-1-2} is not a number, ...
\emph{but, I would also like to have it also with an upper case}
Infinity not being a number, ...
\emph{but Nameref does not work at all}: \NameRef{1-1-2}
\end{document}
更新没有 的版本,仅expl3
使用“通常”TeX
\FirstUpcase
\documentclass[11pt]{book}
\usepackage{xparse}
\usepackage{tcolorbox}
\usepackage{hyperref}
\usepackage{cleveref}
\makeatletter
\def\first@up#1#2\relax{%
\uppercase{#1}#2%
}
\newcommand{\FirstUpcase}[1]{%
\expandafter\expandafter\expandafter\first@up#1\relax%
}
\makeatother
\NewDocumentCommand{\NameRef}{sm}{%
\IfBooleanTF{#1}{%
\FirstUpcase{\getrefbykeydefault{#2}{name}{-1}}%
}{%
\IfRefUndefinedExpandable{#2}{}{%
% label is defined, now get hyperlink
\hyperlink{\getrefbykeydefault{#2}{anchor}{-1}}{\FirstUpcase{\getrefbykeydefault{#2}{name}{-1}}}
}%
}%
}
\newcounter{abc}%
\newtcolorbox%
[%beginForReferences
use counter= abc,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitleagree},%Used to make for LONG titles in the references.
crefname={alert}{alerts},
Crefname={Alert}{Alerts},
]%endForReferences
{greement}
[2][]%
{%
detach title,
coltitle=black,
nameref={#2},%
before upper={\tcbtitle\quad},
code={\gdef\mytitleagree{#2}},%Used to make for LONG titles in the references.
title=ALERT \thetcbcounter,%
#1,%Required for references.
}%
\begin{document}
\chapter{ONE}\label{1-0-0}
\section{One}\label{1-1-0}
\subsection{one}\label{1-1-1}
\emph{Relative to the title of the tcolorbox}
\begin{greement}[label={abc:3-2}]{infinity}
is not a number
\end{greement}
\emph{cref gives}
The \cref{abc:3-2} is most important
\emph{while Cref gives}
\Cref{abc:3-2} is most important
\emph{But I would also like to control the case of the title and, while nameref gives}
We can see \nameref{abc:3-2}
\emph{Nameref gives}
\Nameref{abc:3-2} can be seen.
\emph{instead of the desired}
Infinity can be seen.
\subsection{infinity}\label{1-1-2}
\emph{Similarly, relative to the title of the subsection, nameref gives}
Since \nameref{1-1-2} is not a number, ...
\emph{but, I would also like to have it also with an upper case}
Infinity not being a number, ...
\emph{NameRef works here}: \NameRef{1-1-2}
\end{document}