0

Given this MWE

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

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

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

I yield

an erroneous result

How can this be fixed?

1
  • 2
    Because tcolorbox label option sets a \label and zref needs a \zlabel. You can load zref with user option and set if in your environment after \refstepcounter.
    – gusbrs
    Feb 13, 2022 at 3:34

1 Answer 1

3

zref uses a separate referencing system from the standard one, so you cannot set a \label (which is what the label= option of your tcolorbox does) and expect for zref to find it. However, since the systems are independent, you can have a zref label with the same name, which you can create using \zlabel, provided by zref's user module.

\documentclass[
  a4paper,
  11pt,
  parskip=half
]{scrbook}

\usepackage[nospace]{varioref}
\usepackage[pdfencoding=unicode, psdextra]{hyperref}
\usepackage{cleveref}
\usepackage[user]{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}%
    \begin{MyBox}[label={#1}]{#2}
        \zlabel{#1}
        \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
}

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

\zref{test-id}

\end{document}

enter image description here

6
  • Answers the question as asked. But I have a problem with user-defined values, see tex.stackexchange.com/questions/633679/… for more details.
    – Markus W.
    Feb 13, 2022 at 11:04
  • 1
    @MarkusW. Indeed, that's all I can do. And you already received a good answer there.
    – gusbrs
    Feb 13, 2022 at 11:05
  • I was not aware about zlabeltriggerng this. I wanted to define vars an refer to the in the GOOD section
    – Markus W.
    Feb 13, 2022 at 11:05
  • 1
    @MarkusW. I don't understand what you think \zlabel is "triggering", it just sets a label for zref, just like a \label does for the standard referencing system.
    – gusbrs
    Feb 13, 2022 at 11:07
  • The point I want to make is that I need to use \zref@setcurrent and that is obviously insufficent as demonstrated in tex.stackexchange.com/questions/633679/…. So you solved/answered the (bad formulated) question in a surprising way that does not help with \zref@setcurrent
    – Markus W.
    Feb 13, 2022 at 11:36

You must log in to answer this question.

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