3

I have the following problem: using the tcolorbox package I get inconsistent results for the labelling: in this example I get that the boxes are both referred to as 1.1 :

enter image description here

The code for this is:

\documentclass{memoir}
\usepackage[breakable]{tcolorbox}
\newenvironment{Mijnbox}[1]
{\stepcounter{table}%
\tcbset{breakable,coltitle=blue!20!black,colback=black!1!white,colframe=black!10!    white,title=Box ~\thetable: #1}
\begin{tcolorbox}}
{\end{tcolorbox}}

\begin{document}
\ref{Boxone} and \ref{Boxtwo}

\section{A try}
\begin{Mijnbox}{This one is called one}\label{Boxone}
I try to refer to this box 
\end{Mijnbox}

\begin{Mijnbox}{ge}\label{Boxtwo}
This is yet another box
\end{Mijnbox}
end{document}

What am I doing wrong?

4

The tcolorbox package allows for automatic box numbering and using other counters as well. The label can be specified as an option.

It's quite useful to define a \newtcolorbox for such cases which get its own name space and does not interfere with other tcolorbox settings, which is 'polluted' by \tcbset.

  • The other nice way with \refstepcounter was answered by Torbjorn T. already, which is correct of course.
  • I changed the colours for the screenshot only, but not in the displayed code

\documentclass{memoir}
\usepackage[breakable]{tcolorbox}


\newtcolorbox[use counter=table,number within=chapter]{MijnOtherBox}[2][]{breakable,coltitle=blue!20!black,colback=black!1!white,colframe=black!10!white,title={Box~\thetcbcounter: #2},#1}


\newenvironment{Mijnbox}[1]
{\stepcounter{table}%
  \tcbset{breakable,coltitle=blue!20!black,colback=black!1!white,colframe=black!10!    white,title=Box ~\thetable: #1}
  \begin{tcolorbox}}
  {\end{tcolorbox}}

\begin{document}
\ref{Boxone} and \ref{Boxtwo} and \ref{BoxThree}

\section{A try}
\begin{Mijnbox}{This one is called one}\label{Boxone}
I try to refer to this box 
\end{Mijnbox}

\begin{Mijnbox}{ge}\label{Boxtwo}
This is yet another box
\end{Mijnbox}

\begin{MijnOtherBox}[label=BoxThree]{This is box three}
Even another box
\end{MijnOtherBox}

\end{document}

enter image description here

|improve this answer|||||
3

You need \refstepcounter{table} instead of \stepcounter{table}. The former will make the stepped counter the current/active one, which is picked up by \label.

|improve this answer|||||

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.