1

Hello I have 3 boxes that come from the code configured as "boxex" and the fourth of another color named "boxe", here my problem:

I want to take the reference number of the third box "boxex" to use it in the fourth box, first of "boxe" and so on for the following boxes or 4th box "boxex" with 2nd box "boxe".

Now I have 2 possible ideas and consists in that the enumeration is manually and the other is that I have to take it from the label reference.

But I do not know how to do it, can you help me?

\documentclass[12pt,a4paper]{book}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{tcolorbox}

\newtcolorbox[auto counter]{boxex}[2][]{fontupper=\footnotesize,
colback=black!40,colframe=black,fonttitle=\bfseries,sharp corners,
title=\thetcbcounter~#2}

\newtcolorbox[auto counter]{boxe}[2][]{fontupper=\footnotesize,
colback=black!20,colframe=green!30!black,fonttitle=\bfseries,sharp corners,
title=\thetcbcounter~#2}

\title{Example tcolorbox enumerate}
\begin{document}
\chapter{Introduction}
    \section{code tcbox}
    \begin{boxex}{Title one}
    Hi, it's my box one
    \end{boxex}
    \begin{boxex}{Title two}
    Hi, it's my box two
    \end{boxex}
    \begin{boxex}{Title tree}
    Hi, it's my box tree
    \end{boxex}
    \begin{boxe}{Change counter ``thee''}
    Hi, this is where I want to change the enumeration to \bf{3}
    \end{boxe}
\end{document}

enter image description here

1 Answer 1

0

As I understand correctly you want boxe environments with the number of the boxex environment before it. That can be achieved by leaving the boxe environments unnumbered (by removing auto counter from the box options), and inserting the counter of the boxex environment in the title definition of boxe.

The counter is stored as \thetcbcounter inside of the box, but inside another box this refers to the counter of the new box instead of the original box - in this case, inside boxe the counter \thetcbcounter refers to the counter of boxe but you want to refer to the counter of boxex. However, the macro \thetcbcounter is a shortcut for an internal counter name which is unique for the type of box. Example 5.1 on page 110 of the tcolorbox manual states that "the real counter name is tcb@cnt@pabox" for the box type pabox. So in your case the counter of boxex is stored as tcb@cnt@boxex.

Now, given that this counter name contains the @ symbol, it is not allowed to use this in regular code (this is by design, internal parts of the code are not intended to be used in documents, only in the package code itself). You can access this type of macros by surrounding code using the macro with \makeatletter and makeatother, i.e., make the @ symbol a normal symbol, use it as a normal symbol, make it a special symbol again. We can use this to define a new macro, without @ in the name, to refer to the boxex counter and define the title of the boxe macro using the new counter macro. A counter can be printed by putting \the in front, so the counter tcb@cnt@boxex can be printed using \thetcb@cnt@boxex.

MWE:

\documentclass[12pt,a4paper]{book}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{tcolorbox}

% define helper macro to refer to boxex counter
\makeatletter
\def\boxexcounter{\thetcb@cnt@boxex}
\makeatother

\newtcolorbox[auto counter]{boxex}[2][]{fontupper=\footnotesize,
colback=black!40,colframe=black,fonttitle=\bfseries,sharp corners,
title=\thetcbcounter~#2}

% use helper macro in title definition of boxe environment
% don't use auto counter for this environment
\newtcolorbox{boxe}[2][]{fontupper=\footnotesize,
colback=black!20,colframe=green!30!black,fonttitle=\bfseries,sharp corners,
title=\boxexcounter~#2}

\title{Example tcolorbox enumerate}
\begin{document}
\chapter{Introduction}
    \section{code tcbox}
    \begin{boxex}{Title one}
    Hi, it's my box one
    \end{boxex}
    \begin{boxex}{Title two}
    Hi, it's my box two
    \end{boxex}
    \begin{boxex}{Title tree}
    Hi, it's my box tree
    \end{boxex}
    \begin{boxe}{Change counter ``thee''}
    Hi, this is where I want to change the enumeration to \bf{3}
    \end{boxe}
    \begin{boxex}{Title four}
    Hi, it's my box tree
    \end{boxex}
    \begin{boxe}{Change counter ``thee''}
    Hi, this is where I want to change the enumeration to \bf{4}
    \end{boxe}
\end{document}

Result:

enter image description here

1
  • thanks for your reply @Marijn, is perfect
    – royer
    Jun 24, 2019 at 15:02

You must log in to answer this question.

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