1

I want to create new numbered tcolorbox environments, so that every group has its own numbering. I can do this by following code.

\usepackage[most]{tcolorbox}
\documentclass{article}
% group of alerts
\newtcolorbox[auto counter, number within=chapter]{alertbox}[1][]{
    colback=red!10!white,
    title=\textbf{Alert~\thetcbcounter :} {[#1]~},
}
% group of notes
\newtcolorbox[auto counter, number within=chapter]{notebox}[1][]{
    colback=blue!10!white,
    title=\textbf{Note~\thetcbcounter :} {[#1]~},
}
\begin{document}
   \begin{alertbox}[Food] % Alert 1.1: Food
        Food is ready
   \end{alertbox}
   \begin{alertbox}[Drinks] % Alert 1.2: Drinks
        Drinks are ready
   \end{alertbox}
   \begin{notebox}[Food] % Note 1.1: Food
        Caution! Food is hot.
   \end{notebox}
   \begin{notebox}[Drinks] % Note 1.2: Drinks
        There is Cola in Drinks!
   \end{notebox}

\end{document}

But, this leads to code repetition. I'm trying to avoid this by creating a command that constructs tcolorbox environment.

\newcommand{\declarenumberedbox}[3] {
   \newtcolorbox[auto counter, number within=chapter]{#1}[1][]{
       colback=#2,
       title=\textbf{#3~\thetcbcounter :} {[                            ]~},
                                          %% custom title must be here   %%
                                          %% what should be filled here? %%
   }
}
%% approximately how I want my code to look like 
%% (or some equivalent ways to achieve this).
\declarenumberedbox{alertbox}{Alert}{red!10!white}
\declarenumberedbox{notebox}{Note}{blue!10!white}

5
  • 1
    Welcome to TeX.SE..Please post your MWE as in full format, i.e., from \documentclass to \end{document}
    – MadyYuvi
    Jun 2, 2021 at 16:56
  • 2
    you are probably looking for [##1]~ (you need to double the hash for "inner argument numbers"). Jun 2, 2021 at 17:02
  • @UlrikeFischer That actually works! Thank you very much!! Can also provide me with reference to where I can study more about programming/control flow in latex? Jun 2, 2021 at 17:16
  • see e.g. tex.stackexchange.com/a/42464/2388 Jun 2, 2021 at 17:22
  • 1
    That helped! Thanks again. Jun 2, 2021 at 17:32

0

Browse other questions tagged .