1

I want to make a tcolorbox with a number. I currently have created a new command

\newenvironment{mybox}{\begin{tcolorbox}\centering}{\end{tcolorbox}}

I want to display numbering inside the tcolorbox.

I find this The first style numbering tcolorbox, but i can not combine it with mine.

I only want to add a number in the box. I tried to do \newenvironment{mybox}{\begin{tcolorbox}[within=section]\centering}{\end{tcolorbox}}but it wont work.

and i also want to \ref the box in this way \ref{first box}

\begin{mybox}\label{first box}
      bla,bla,bla...
   \end{mybox}

please someone help me!!

1 Answers1

3

To build a numbered tcolorbox you should use a \newtcbolorbox command instead of a \newenvironment. This way you can use a counter within every tcolorbox. Take a look at section "5 Initialization Option Keys" in tcolorbox documentation.

Here you have an example with mybox:

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcolorbox[auto counter, number within=section]{mybox}[2][]{%
    title=Mybox~\thetcbcounter: #2, #1}

\begin{document}

\section{First section}

\begin{mybox}[label=myfirstbox]{A box with title}
This is a numbered box.
\end{mybox}

\begin{mybox}{}
This is a numbered box without title. Previous box was ~\ref{myfirstbox}.
\end{mybox}

\section{Second section}

\begin{mybox}{A box with title}
This is a numbered box. 
\end{mybox}

\begin{mybox}{}
This is a numbered box without title. First box was ~\ref{myfirstbox}.
\end{mybox}

\end{document}

enter image description here

Ignasi
  • 136,588
  • i want to label it use \label{} this way. – xiaojiuwo Feb 07 '20 at 13:10
  • The only way to make label for box is [label=] ? i really want to use the normal label way. – xiaojiuwo Feb 07 '20 at 13:28
  • What do you want to use \label? This way is the same command but a la tcolorbox style. If you insist with \label, you can use it inside title option, but the reference is just the box number without the section component. Another solution could be to use a \newtcbtheorem where labels are automatically created and you don't use \label at all. – Ignasi Feb 07 '20 at 13:40
  • after i created \newtcolorbox i want to use \label{} when i create a box \begin{mybox}\label{}, and the \ref seems not the box number, how can i format it? – xiaojiuwo Feb 07 '20 at 13:46
  • what is the option to set so that all numbering are within the current tcolorbox? – CasperYC May 30 '23 at 14:06