0

This question complements Why does \zifrefundefined fail on tcolorbox reference?

\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}

yields

enter image description here

Any idea?

Update 1

Based on Ulrike's comments the question remains how to check the existence of the definition before use. I read the docs of etoolbox, but did not find the appropriate macro. Could you please comment on that one?

\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}

Update 2

Another problem not solved: \csuse does not work **within the environment.

\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 Answer 1

3

@nameuse is a local definition. If you use it inside an environment it won't be available outside. You need a global definition.

I would suggest to use etoolbox which offers various tools for this, e.g. \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}

enter image description here

7
  • Would be nice not to lose all those zref macros that used to work outside the environment. Is this possible? At least I need a drop in replacement for \zifrefundefined. I tried \ifdef and \ifcsdef, but it seems not to be the appropriate command.
    – Markus W.
    Feb 13, 2022 at 10:16
  • See update in question.
    – Markus W.
    Feb 13, 2022 at 10:44
  • See Update 2 why I removed the solution flag
    – Markus W.
    Feb 13, 2022 at 11:56
  • The problem is that order matters here. Is Etoolbox is not made for multipass?
    – Markus W.
    Feb 13, 2022 at 13:00
  • Do not change your question by editing and adding updates. This here is not a support forum where you ask back. Ask a new question and at best only ask one question at one time. Feb 13, 2022 at 15:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .