with use of tcolorbox
package I define the following boxes, which are aware whether they are on even or on odd page:
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-definedtcolorbox
, therefore I added an optional argument to thevaja
box.If
cleveref
is used, saycrefname=...
in the init options in order to provide for the correctcleveref
reference names.Explanation:
tcolorbox
has aphantom=
key -- every argument tophantom
will added to a list and stored in a\sbox
macro -- this is true for the\refstepcounter
macro for the internal counter of thetcolorbox
as well, such that the redefined\@currentlabel
gets lost. Any\label
outside of the samephantom=...
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'.