这个问题补充为什么 \zifrefundefined 在 tcolorbox 引用上失败?
\documentclass[
a4paper,
11pt,
parskip=half
]{scrbook}
\usepackage[nospace]{varioref}
\usepackage[pdfencoding=unicode, psdextra]{hyperref}
\usepackage{cleveref}
\usepackage{zref}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\DeclareNewTOC[
category=float,
float,% declares floating environment eq
floatpos=ht,
nonfloat,% declares non-floating environment eq-
listname={Some Items},
name=TM,
tocentrystyle=tocline,
tocentrylevel:=table,
tocentryindent:=table,
tocentrynumwidth:=table,
type=MySpecialEnvFloat,
]{ltm}
\newtcolorbox[
auto counter,
crefname={TM}{TMs}]{MyBox}[2][]{
title={TM\,\thetcbcounter: #2},
#1}
\begin{document}
\newcounter{MySpecialEnvCounter}
\makeatletter
\newcommand{\MySpecialEnv}[4]{%
\refstepcounter{MySpecialEnvCounter}%
% UNSTABLE
\@namedef{#1@Definition}{#3}
\begin{MyBox}[label={#1}]{#2}
\addcontentsline{ltm}{section}{TM\,\theMySpecialEnvCounter: #2}
Some text
\tcblower
#4
\end{MyBox}
}%
\listofMySpecialEnvFloats
\MySpecialEnv{test-id}{Something that looks like a summary}{-}{
sdjoajhsja sijaoihs siahskah
Nameuse: \@nameuse{test-id-2@Definition}
Csname: \emph{\csname test-id-2@Definition\endcsname}
}
\MySpecialEnv{test-id-2}{Yet another summary}{-}{
asjajsaj sakjskaj sjkajs
}
\subsection{varioref test}
Varioref test: Reference to \vref{test-id} and \vref{test-id-2}
Pageref test: \vpageref{test-id-2}
\subsection{Cref Test}
\cref{test-id} \cpageref{test-id}
\subsection{zifrefundefined}
\zifrefundefined{test-id}{\nfss@text{\reset@font\bfseries??}}{GOOD}
\end{document}
产量
任何想法?
更新 1
根据 Ulrike 的评论,问题仍然是如何在使用前检查定义是否存在。我阅读了 的文档etoolbox
,但没有找到合适的宏。你能对此发表评论吗?
\documentclass[
a4paper,
11pt,
parskip=half
]{scrbook}
\usepackage[nospace]{varioref}
\usepackage[pdfencoding=unicode, psdextra]{hyperref}
\usepackage{cleveref}
\usepackage[user]{zref}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\tcbuselibrary{breakable}
\DeclareNewTOC[
category=float,
float,% declares floating environment eq
floatpos=ht,
nonfloat,% declares non-floating environment eq-
listname={Some Items},
name=TM,
tocentrystyle=tocline,
tocentrylevel:=table,
tocentryindent:=table,
tocentrynumwidth:=table,
type=MySpecialEnvFloat,
]{ltm}
\newtcolorbox[
auto counter,
crefname={TM}{TMs}]{MyBox}[2][]{
title={TM\,\thetcbcounter: #2},
#1}
\newcounter{MySpecialEnvCounter}
\begin{document}
\makeatletter
\zref@newprop{Definition}{}
\newcommand{\MySpecialEnv}[4]{%
\refstepcounter{MySpecialEnvCounter}%
\csgdef{#1@Definition}{#2}
\zref@setcurrent{Definition}{#2}
\begin{MyBox}[label={#1}]{#2}
\addcontentsline{ltm}{section}{TM\,\theMySpecialEnvCounter: #2}
Some text
\tcblower
#4
\end{MyBox}
}%
\makeatother
\listofMySpecialEnvFloats
\MySpecialEnv{test-id}{Something that looks like a summary}{-}{
sdjoajhsja sijaoihs siahskah
Nameuse: \@nameuse{test-id-2@Definition}
Csname: \emph{\csname test-id-2@Definition\endcsname}
}
\MySpecialEnv{test-id-2}{Yet another summary}{-}{
asjajsaj sakjskaj sjkajs
}
\subsection{varioref test}
\textbf{Varioref test}: Reference to \vref{test-id} and \vref{test-id-2},
\textbf{Pageref test}: \vpageref{test-id-2}
\subsection{Cref Test}
\cref{test-id} \cpageref{test-id}
\subsection{etoolbox}
\makeatletter
A \csuse{test-id-2@Definition}
% THIS DOES NOT WORK AS EXPECTED
B \ifdef{test-id-2}{\csuse{test-id-2@Definition}}{test-id-2 DOES NOT EXIST}
C \csuse{test-id-4@Definition}
% THIS DOES NOT WORK AS EXPECTED
D \ifdef{test-id-4}{\csuse{test-id-4@Definition}}{test-id-4 DOES NOT EXIST}
\makeatother
\subsection{zifrefundefined}
\makeatletter
\zifrefundefined{test-id}{\nfss@text{\reset@font\bfseries??}}{
\zref@extractdefault{#1}{Definition}{}
}
\makeatother
\end{document}
更新 2
另一个未解决的问题:\csuse
在环境内不起作用。
\MySpecialEnv{test-id}{Something that looks like a summary}{-}{
sdjoajhsja sijaoihs siahskah
Nameuse: \@nameuse{test-id-2@Definition}
Csname: \emph{\csname test-id-2@Definition\endcsname}
% THIS ONE FAILS
Etoolbox: \csuse{test-id-2@Definition}
}
答案1
@nameuse 是局部定义。如果你在环境内部使用它,它将无法在外部使用。你需要一个全局定义。
我建议使用 etoolbox,它提供了各种工具,例如\csgdef
:
\documentclass{article}
\usepackage{etoolbox}
\begin{document}
\makeatletter
{\@namedef{blub1}{something local} inside: \@nameuse{blub1}}
outside: \@nameuse{blub1}
{\csgdef{blub1}{something global} inside: \csuse{blub1}}
outside: \csuse{blub1}
\end{document}