Tcolorbox – How to Reference Boxes in Tcolorbox

cross-referencingtcolorbox

with use of tcolorbox package I define the following boxes, which are aware whether they are on even or on odd page:

enter image description here

enter image description here

the mwe for above boxes is:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}

\newcounter{task}
\newtcolorbox[use counter=task,
              number within=section,
              number freestyle={\thesection-\noexpand\arabic{\tcbcounter}},
              ]{vaja}%
   {enhanced jigsaw,
    breakable,
    toggle left and right,
    boxrule=0mm,
    colback=white, colframe=red,
    top=0mm,bottom=1mm,left=1mm,right=1mm,
    arc=0mm,
    borderline={0.5pt}{0pt}{red, sharp corners},
    fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
    rightrule=1mm,
        overlay={%
    \tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below right=0mm and \marginparsep] at (frame.north east) {Vaja
                        \thetcbcounter};}
                 {\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below left=0mm and \marginparsep]  at (frame.north west) {Vaja
                    \thetcbcounter};}
        }%end of overlay
    }% end of task

\usepackage{lipsum}

\begin{document}
    \begin{vaja}\label{task:ce-1}% label hasn't any effect
\lipsum*[11]
    \end{vaja}
\newpage
    \begin{vaja}\label{task:ce-2}% label hasn't any effect
\lipsum[11]
see \ref{task:ce-1}
    \end{vaja}
see \ref{task:ce-1} and \ref{task:ce-2}% doesn't work
\end{document}

I did not initially anticipate references, but now I realized that this would be a very useful option. naive approach of referencing in above doesn't work …

  • what should i change in definition of "vaja", that referencing be possible?

  • i suspect that in box definition i should use title, but how to insert it definition that the box design be preserved?

Best Answer

You need to use label=... option to the self-defined tcolorbox, therefore I added an optional argument to the vaja box.

If cleveref is used, say crefname=... in the init options in order to provide for the correct cleveref reference names.

Explanation:

tcolorbox has a phantom= key -- every argument to phantom will added to a list and stored in a \sbox macro -- this is true for the \refstepcounter macro for the internal counter of the tcolorbox as well, such that the redefined \@currentlabel gets lost. Any \label outside of the same phantom=... key command will not grab the correct \@currentlabel then.

The label=... option however places \label directly after the \refstepcounter in the phantom 'group' → the \@currentlabel is stored correctly and 'survives'.

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}

\newcounter{task}
\newtcolorbox[use counter=task,
              number within=section,
              number freestyle={\thesection-\noexpand\arabic{\tcbcounter}},
              ]{vaja}[1][]%
   {enhanced jigsaw,
    breakable,
    toggle left and right,
    boxrule=0mm,
    colback=white, colframe=red,
    top=0mm,bottom=1mm,left=1mm,right=1mm,
    arc=0mm,
    borderline={0.5pt}{0pt}{red, sharp corners},
    fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
    rightrule=1mm,
        overlay={%
    \tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below right=0mm and \marginparsep] at (frame.north east) {Vaja
                        \thetcbcounter};}
                 {\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below left=0mm and \marginparsep]  at (frame.north west) {Vaja
                    \thetcbcounter};}
        },%end of overlay
        #1
    }% end of task

\usepackage{lipsum}

\begin{document}
    \begin{vaja}[label=task:ce-1]% label hasn't any effect
\lipsum*[11]
    \end{vaja}
\newpage
    \begin{vaja}[label={task:ce-2}]% label hasn't any effect
\lipsum[11]
see \ref{task:ce-1}
    \end{vaja}
see \ref{task:ce-1} and \ref{task:ce-2}% doesn't work
\end{document}

enter image description here

Related Question