1

I'm designing a tutorial using tcolorbox, which includes multiple chapters, and each chapter contains a few questions. I use tcolorbox to contain the text of each question, for which I just need "Question X.Y" as the title of the box, where X and Y are the chapter and box numbers, respectively. For this purpose, I defined the following tcolorbox environment.

\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{questionb}[1][]{%   
  enhanced,
  breakable,
  title=\textbf{Question \thetcbcounter}
}

Later, in each chapter file, I would compose each question like:

\begin{questionb}
Here I put the text of the question.
\end{questionb}

Everything works as expected, and I get the following: enter image description here

Now what I want is to reference the question number in the rest of the document, i.e., 2.2 in this example. From reading the documentation and searched on the web, I thought I can just use the label option when composing the question. That is,

\begin{questionb}[label=q:example]
Here I put the text of the question.
\end{questionb}

and then reference it by \ref{q:example}. Unfortunately, it seems that it doesn't work, and the compiler complains "Reference q:example on page X undefined".

I also tried another approach with

\begin{questionb}\label{q:example}
Here I put the text of the question.
\end{questionb}

With this approach, the complaint is gone. However, I always get an odd number 3 when referencing it using \ref{q:example}, no matter where I put the \label command (even after another questionb block of the same chapter file....)

I must have misunderstood how the label options of tcolorbox works, and even how newtcolorbox should be used. It would be great if someone can help me through this. Thanks a lot!

BTW, I use XeLaTeX on Mac (TexLive2022).

A complete compilable example is as follows:

\documentclass[10pt,UTF8]{book}
\usepackage[centering,paperwidth=180mm,paperheight=230mm,body={390pt,530pt}]{geometry}
\usepackage{titlesec}
\usepackage[most]{tcolorbox}
\usepackage{verbatim}

% Define environment
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{questionb}[1][]{%   
  enhanced,
  breakable,
  title=\textbf{Question \thetcbcounter}
}


\begin{document}
\title{Tutorial}
\author{Name}
\date{Date}


\begin{questionb}[label=q:example]
This is my question text.
\end{questionb}

I want to reference Question \ref{q:example}
\end{document}
2
  • Always show a small but complete example. That makes it much easier to test your issue. Dec 3, 2023 at 13:26
  • Thanks for the remind. I have added a complete text @UlrikeFischer
    – leeyee
    Dec 3, 2023 at 13:41

1 Answer 1

1

You need to actually pass your optional argument to the tcolorbox:

\documentclass[10pt]{book}
\usepackage[centering,paperwidth=180mm,paperheight=230mm,body={390pt,530pt}]{geometry}
\usepackage{titlesec}
\usepackage[most]{tcolorbox}
\usepackage{verbatim}

% Define environment
\newtcolorbox[auto counter, number within=chapter]{questionb}[1][]{%   
  enhanced,
  breakable,
  title=\textbf{Question \thetcbcounter},
  #1 % <- added to pass the optional argument to the tcolorbox
}


\begin{document}
\title{Tutorial}
\author{Name}
\date{Date}


\begin{questionb}[label=q:example]
This is my question text.
\end{questionb}

I want to reference Question \ref{q:example}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .