2

I have a set of different tcolorboxes defined that I use to indicate when something is important, when something is just a comment, etc. And I have created a counter \newcounter{BoxCounter} that is global to all this boxes so they are all numerated no matter which type of box they are, in the way Box 1, Box 2, Box 3, ...

The problem is that I don't know how exactly to make a \ref to one of this boxes. I have defined things like this:

\newcounter{BoxCounter}

\newtcolorbox{Comentario}[2][] {
colback=ComentarioBoxBackgroundColor,
colframe=ComentarioBoxFrameColor,
center, 
valign=top, 
halign=left,
breakable,
enhanced,
title=\bfseries\sffamily #2 \hfill Box \refstepcounter{BoxCounter}\theBoxCounter,
#1
}

\newtcolorbox{Importante}[2][] {
colback=EcuacionImportanteBoxBackgroundColor,
colframe=EcuacionImportanteBoxFrameColor,
center, 
valign=top, 
halign=left,
before skip=1cm, % Espacio en blanco antes del recuadro.
after skip=1cm, % Espacio en blanco después del recuadro.
breakable,
enhanced,
title=\bfseries\sffamily #2 \hfill Box \refstepcounter{BoxCounter}\theBoxCounter,
#1
}

where the first argument (optional) is of the form label = some box so then I use \ref{some box. The problem is that while the numbering of the box is just 1, 2, 3, ... the reference prints in the format 1.1 and so on.

How could I make this references print in the same format they are printed in the box titles?

  • You're using a wrong approach here. The label is using something completely different as base, not the \refstepcounter information – user31729 Apr 1 '18 at 13:53
  • Related (but no duplicate: ) tex.stackexchange.com/questions/408186/… – user31729 Apr 1 '18 at 14:27
3

The label=... option of tcolorbox is only useful if the stepping of the counter is done by the tcolorbox mechanism, so use use counter=BoxCounter and not \refstepcounter, otherwise label= would grab the last \@currentlabel, which might be left over from a previous call of \section etc.

See also tcolorbox: how to reference boxes?

\documentclass{article}

\usepackage[most]{tcolorbox}




\newcounter{BoxCounter}

\newtcolorbox[use counter=BoxCounter]{Comentario}[2][] {
%colback=ComentarioBoxBackgroundColor,
%colframe=ComentarioBoxFrameColor,
center, 
valign=top, 
halign=left,
breakable,
enhanced,
title=\bfseries\sffamily #2 \hfill Box \theBoxCounter,
#1
}

\newtcolorbox[use counter={BoxCounter}]{Importante}[2][] {
%colback=EcuacionImportanteBoxBackgroundColor,
%colframe=EcuacionImportanteBoxFrameColor,
center, 
valign=top, 
halign=left,
before skip=1cm, % Espacio en blanco antes del recuadro.
after skip=1cm, % Espacio en blanco después del recuadro.
breakable,
enhanced,
title=\bfseries\sffamily #2 \hfill Box \theBoxCounter,
#1
}

\begin{document}


See \ref{foo} and \ref{foobar}

\begin{Comentario}[label=foo]{}

\end{Comentario}

\begin{Importante}[label={foobar}]{}

\end{Importante}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.