4

I am trying to use tcolorboxes to highlight exercises in a math tutorial (book document class) for a course I teach. I use a \label in the box hoping to reference the exercise by number in the solutions manual. However, the label does not "attach" to the colorbox; instead it returns the section number. Thus, Exercise "17" is referenced as "2.2.3" because that is the chapter section it is in. This behavior is different to that of a normal LaTeX environment and I am going in circles trying to resolve the issue. The tcolorbox manual is very detailed, but I cannot sort out this (seemingly) simple issue.

What I want to accomplish is this:

(1) create a box and put a \label{mylabel} in it.

(2) \ref{mylabel} returns the box (exercise) number.

(3) I want to reset the box numbering at the start of each chapter, with no "cross-talk" between the similarly numbered boxes in each chapter.

I hope somebody can help restore my sanity and reveal the secret to doing this!

Here is a MWE:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{most}

\begin{document}

\section{Introduction}
This document contains labeled tcolorboxes in Section \ref{sec:example}. The first box is numbered as \ref{box:label1}.

\section{An Example Section}
\label{sec:example}

\newcounter{myboxcounter}
\newtcolorbox[auto counter]{mybox}[1][]{
    enhanced,
    colback=blue!5!white,
    colframe=blue!75!black,
    fonttitle=\bfseries,
    before title={\refstepcounter{myboxcounter}},
    title={Exercise~\arabic{myboxcounter}\ifx#1\empty\else\ (#1)\fi},
    fonttitle=\bfseries,
    breakable
}

\begin{mybox}[]
    \label{box:label1}
    This is box \ref{box:label1}.
\end{mybox}

\begin{mybox}[]
    \label{box:label2}
    This is box \ref{box:label2}.
\end{mybox}

\subsection{An Example Section}
\begin{mybox}[]
    \label{box:label3}
    This is box \ref{box:label3}. The first two are boxes \ref{box:label1} and \ref{box:label2}.
\end{mybox}

\end{document}

This is my output showing the labels refers to the section number

1

2 Answers 2

4

Maybe something like this?

  • You could add label=<mylabel> in mybox's optional arguments, with #2 parameter I added.
  • You don't need to manually maintained the mytcbcounter, and \refstepcounter before title, since you have assigned auto counter, tcolorbox is clever enough to deal with.
  • the documentclass you shown is article, don't have chapter, I use number within=section instead to make the counter updated at new \section.
  • I also add hyperref package to provide clickable hyperlinks.
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{hyperref}%<-

% \newcounter{myboxcounter}[section]%<-
\newtcolorbox[auto counter,number within=section]{mybox}[2][]{
    #2,enhanced,breakable,
    colback=blue!5!white,
    colframe=blue!75!black,
    fonttitle=\bfseries,
    % before title={\refstepcounter{myboxcounter}},
    % title={Exercise~\arabic{myboxcounter}\ifx#1\empty\else\ (#1)\fi},
    title={Exercise~\thetcbcounter\ifx#1\empty\else\ (#1)\fi},
}

\begin{document}

\section{Introduction}
This document contains labeled tcolorboxes in Section \ref{sec:example}. The first box is numbered as \ref{box:label1}.

\begin{mybox}[]{label={box:label0}}
    This is box \ref{box:label3}. The first two are boxes \ref{box:label1} and \ref{box:label2}.
\end{mybox}

\section{An Example Section}
\label{sec:example}

\begin{mybox}[]{label={box:label1}}
    This is box \ref{box:label2}.
\end{mybox}

\begin{mybox}[]{label={box:label2}}
    This is box \ref{box:label1}.
\end{mybox}

\subsection{An Example Section}
\begin{mybox}[Title]{label={box:label3}}
    This is box \ref{box:label0}. The first two are boxes \ref{box:label1} and \ref{box:label2}.
\end{mybox}

\end{document}

result

2
  • Thank you, "Explorer" – you solved it. My sanity is restored. I ended up using the "number within=chapter" flag instead to get "Exercise 2.17", as opposed to "Exercise 2.2.14" when using "number within=section". Other course materials refer to the Exercise as "Exercise 17", so "Exercise 2.17" is hopefully not going to confuse students. Commented Jan 11 at 16:41
  • @mmjt Ummm.... you also want to update when start a new chapter, than if both "chapter1" and "chapter2" have "Exercise17", how to distinguish them? It's okay to change to have two "17"s, but I don't think that is a good idea, use "2.17" and "3.17" is common to refer the exercise number. Commented Jan 11 at 17:03
3

mwe

  1. Is not a good idea use numbered labels instead of some meaningful key. What if you have 50 boxes but you want insert o remove the third box?

  2. As pointed @Explorer's comment is also a issue reset the counter in each chapter. If you want and number independent of the section level but for all the document, then you only need a auto counter.

  3. You may want use a \NewTcbTheorem to simplify a bit your code:

\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage[colorlinks,linkcolor=blue!75!black]{hyperref}%<-
\usepackage{nameref}%<-
\NewTcbTheorem[auto counter]{mybox}{Exercise}%
{colback=blue!5!white,
colframe=blue!75!black,
fonttitle=\bfseries\sffamily}{box}
\begin{document}

\chapter{Whatever}
\section{Introduction}

This document contains exercises \ref{box:bar} and \ref{box:baz} in Section \ref{sec:example}. The first exercise is  \ref{box:foo} and the last exercise is \ref{box:end} (\emph{\nameref{box:end}}) in page \pageref{box:end} and chapter \ref{sec:foo}.

\begin{mybox}{This is My Title}{foo}
This exercise is numbered with \ref{box:foo} and it is titled \emph{\nameref{box:foo}}.
\end{mybox}
\section{An Example Section}
\label{sec:example}
\begin{mybox}{}{bar}
    This is box \ref{box:bar}. 
\end{mybox}
\begin{mybox}{}{baz}
    This is box \ref{box:baz}. 
\end{mybox}

\subsection{An Example Section}

This is subsection there is only the box \ref{box:puf}. In main section \ref{sec:example} there are the exercises \ref{box:bar} and \ref{box:baz}. 

\begin{mybox}{Not the last Exercise}{puf}
This is the box \ref{box:puf}. In main section \ref{sec:example} there are the exercises \ref{box:bar} and \ref{box:baz}. 
\end{mybox}


\chapter{Anything else}\label{sec:foo}

This document contains exercises \ref{box:bar} and \ref{box:baz} in Section \ref{sec:example}. The first exercise is  \ref{box:foo} and the last is   \ref{box:end} 

\begin{mybox}{The Last Exercise}{end}
This exercise is numbered with 
\ref{box:end}, it is given on page~\pageref{box:end},
and it is titled \emph{\nameref{box:foo}}.
\end{mybox}


\end{document}
2
  • Thank you Fran, that is a clean solution. I was never able to get the "label is label" flag to work, so the label is now passed as an argument, as you and @Explorer propose. I decided to go with advice and allow exercises to be numbered by chapter, as in "Exercise 2.17", where it is clear we are talking about the exercise 17 in chapter 2. It creates a bit of work for my other class materials where I need to update the homework assignment numbers, but it is a straightforward task that I can spread over the semester,. Commented Jan 12 at 21:17
  • @mmjt I also think that is better. In this case, you only need add , number within=chapter after auto counter to full adapt the MWE. Commented Jan 13 at 0:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.