[Tex/LaTex] Is it possible to reuse tcolorbox definitions in another tcolorbox definition

tcolorboxtikz-styles

TiKZ allows to define a .style which can be later be used in another one:

\tikzset{
      first/.style={rectangle, draw},
      second/.style={first, inner sep=2mm}
}

any change in first is inherited by second. Is it possible to do something similar with tcolorbox?

As an example, take a look at next code, where a newtcolorbox and a newtcbox are defined with exactly same options. How can be same options applied to both definitions without having to type them again?

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\tcbuselibrary{skins}

\makeatletter
\newtcolorbox{myblock}[2][]{
    enhanced,
    frame hidden, interior hidden, segmentation hidden,
    coltitle=black,
    fonttitle=\bfseries\rmfamily,
    fontupper=\tiny,
    title={#2},
    overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
    \draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
    \shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
    \shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
    #1
    }

\newtcbox{mybox}[2][]{
      enhanced,
      frame hidden, interior hidden, segmentation hidden,
    coltitle=black,
    fonttitle=\bfseries\rmfamily,
    fontupper=\tiny,
    title={#2},
    overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
    \draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
    \shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
    \shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
    #1
    }
\makeatother

\begin{document}
\begin{frame}{A nice box for beamer}

\begin{myblock}{Oups, nous avons rencontré une erreur.}
Note partagée non trouvée

L'URL fornie ne correspond pas à une note partagée valide. Cela a pu être causé par une erreur typographique dans le lien, ou le propiétaire l'a privaisée.
\end{myblock}

\mybox{Oups, this is a long title}{Note partagée non trouvée}
\mybox[left=0mm,right=1cm]{Oups}{Note partagée non trouvée}
\end{frame}
\end{document}

Another example. I would like to define some tcolorboxes like beamer's block, example block and alert block. I can do something similar with \mybox definition in previous code where an optional parameter can help to change some aspect like title color, margins, whatever.

\mybox{Oups, this is a long title}{Note partagée non trouvée}
\mybox[left=0mm,right=1cm]{Oups}{Note partagée non trouvée}

But if I would like to use \myexample, \myalert, \myblock, the only way I know is retyping all parameters in all definitions. Do you know an easier way?

Best Answer

The idea is the same; using \tcbset you can define a common style containing the settings that will be shared by your boxes:

\makeatletter
\tcbset{common/.style={
    enhanced,
    frame hidden, interior hidden, segmentation hidden,
    coltitle=black,
    fonttitle=\bfseries\rmfamily,
    fontupper=\tiny,
    title={#1},
    overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
    \draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
    \shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
    \shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
  }
}
\newtcolorbox{myblock}[2][]{common={#2},#1}
\newtcbox{mybox}[2][]{common={#2},#1}

\makeatother

A complete example

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\tcbuselibrary{skins}

\makeatletter
\tcbset{common/.style={
    enhanced,
    frame hidden, interior hidden, segmentation hidden,
    coltitle=black,
    fonttitle=\bfseries\rmfamily,
    fontupper=\tiny,
    title={#1},
    overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
    \draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
    \shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
    \shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
  }
}
\newtcolorbox{myblock}[2][]{common={#2},#1}
\newtcbox{mybox}[2][]{common={#2},#1}

\makeatother

\begin{document}
\begin{frame}{A nice box for beamer}

\begin{myblock}{test text}
test text
\end{myblock}

\mybox{Oups, this is a long title}{test text}
\mybox[left=0mm,right=1cm]{Oups}{test text}
\end{frame}
\end{document}

enter image description here

And, of course, since the styles are really TikZ styles, there's "inheritance":

\tcbset{
  style1/.style={colback=cyan!10},
  style2/.style={style1,colframe=orange},
  style3/.style={style2,outer arc=0pt,arc=0pt
  }
}
\newtcolorbox{myblockA}{style1}
\newtcolorbox{myblockB}{style2}
\newtcolorbox{myblockC}{style3}
Related Question