\newenvironment 带有带有可选标题的 tcolorbox

\newenvironment with a tcolorbox with an optional title

我想创建一个特殊环境,将数学模式包含在 tcolorbox 中。 tcolorbox 标题分配必须是可选的,但总的来说我有兴趣进行可选的 tcolorbox 选项分配。我正在尝试:

\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}

我得到的结果是:

我想得到的结果是:

您在 \newtcolorbox{eqbox}[1][]{} 中缺少空默认值,否则这将被视为强制参数。

\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}

(如果您需要更灵活的可选和强制参数,请在 tcolorbox 文档中搜索 \DeclareTColorBox