9

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?

  • Using \tcbset you can define a common style. – Gonzalo Medina May 28 '14 at 18:00
7

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}
  • Perfect! I knew tcbset but thought that it was always used without names, just as general options for all defined boxes. Is it explained in tcolorbox documentation? I haven't seen it. – Ignasi May 28 '14 at 18:07
  • @Ignasi Yes. it's mentioned in Section 3 Option Keys: "For the options in tcolorbox P. 9 respectively \tcbset P. 10 the following pgf keys can be applied. The key tree path /tcb/ is not to be used inside these macros. It is easy to add your own style keys using the syntax for pgf keys, see [16, 17] or the examples starting from page 172." And some of the examples from page 172 on (and before, for example on page 48) use styles. – Gonzalo Medina May 28 '14 at 18:21
  • Next time, I'll look more carefully into the documentation. Thanks. – Ignasi May 29 '14 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.