Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\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

like image 232
Lorenzo Epifani Avatar asked Oct 19 '25 04:10

Lorenzo Epifani


1 Answers

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 )

like image 109
samcarter_is_at_topanswers.xyz Avatar answered Oct 22 '25 03:10

samcarter_is_at_topanswers.xyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!