[Tex/LaTex] How to define a custom tcolorbox environment with color as a parameter

macrostcolorbox

I want to use different colored tcolorboxes. Is there an easy way to avoid all that typing for every single box by using \newcommand or a macro?

\begin{tcolorbox}[
colframe=blue!25,
colback=blue!10,
coltitle=blue!20!black,  
title= More Test]
\begin{enumerate}
\item Test
\end{enumerate}
\end{tcolorbox}

Best Answer

The tcolorbox package provides a macro called \newtcolorbox to define custom environments; see section 2 in the manual (top of p12, in the current version). Under the assumption that you just want to change the colour, not the tints/shades, you could define a new environment that accepts three arguments (the first of which is optional):

  1. additional tcolorbox options
  2. box colour
  3. title

See below.

enter image description here

\documentclass{article}

\usepackage{tcolorbox} 

% new tcolorbox environment
% #1: tcolorbox options
% #2: color
% #3: box title
\newtcolorbox{mybox}[3][]
{
  colframe = #2!25,
  colback  = #2!10,
  coltitle = #2!20!black,  
  title    = {#3},
  #1,
}

\begin{document}

\begin{mybox}{red}{A red box}
\begin{enumerate}
\item Test
\end{enumerate}
\end{mybox}

\begin{mybox}{green}{A green box}
\begin{enumerate}
\item Test
\end{enumerate}
\end{mybox}

\end{document}
Related Question