Lorenzo Epifani
Lorenzo Epifani

Reputation: 139

\newenvironment with a tcolorbox with an optional title

I would like to create a special environment that encloses a math mode inside a tcolorbox. The tcolorbox title assignment must be optional, but in general I am interested in making optional tcolorbox options assignment. I am trying with:

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}

\newtcolorbox{eqbox}[1]
{
colback=gray!10,
arc=0pt,
boxrule=0pt,
title=#1 % I would like to make this (one of these in general) assignment optional depending on #1, #2...
}

 \newenvironment{boxenv}[1][]{
    \begin{eqbox}[#1]
    \[
 }{
   \]
   \end{eqbox}
}


\begin{document}



\begin{boxenv}[Optional box title]
a+b+c+d
\end{boxenv}

\begin{boxenv}[]
a+b+c+d
\end{boxenv}

\end{document}

The result that I am getting is: enter image description here

The result that I would like to get is: enter image description here

Upvotes: 4

Views: 3905

Answers (1)

You are missing the empty default value in \newtcolorbox{eqbox}[1][]{}, otherwise this will be treated as a mandatory argument.

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}

\newtcolorbox{eqbox}[1][]
{
colback=gray!10,
arc=0pt,
boxrule=0pt,
title=#1 % I would like to make this (one of these in general) assignment optional depending on #1, #2...
}

 \newenvironment{boxenv}[1][]{
    \begin{eqbox}[#1]
    \[
 }{
   \]
   \end{eqbox}
}


\begin{document}



\begin{boxenv}[Optional box title]
a+b+c+d
\end{boxenv}

\begin{boxenv}[]
a+b+c+d
\end{boxenv}

\end{document}

enter image description here

(if you need more flexibility with optional and mandatory arguments, search the tcolorbox docu for \DeclareTColorBox )

Upvotes: 5

Related Questions