如何有条件地在 tcolorbox 中定义新样式

如何有条件地在 tcolorbox 中定义新样式

我定义了一个新的 tcolorbox 选项(键),名为background color。如果给它的数值是小数,则 tcolorbox 的背景将具有透明效果——例如background color=0,否则框将具有背景颜色——例如background=red

除在自定义的 tcolorbox ( )中使用外,它都可以正常工作(在Test 1和中) 。Test 2\myboxTest 3

我怀疑问题出在 的定义上bacground color。那么,在 tcolorbox 中,如何正确定义一个值为 的样式?

梅威瑟:

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{xstring}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tcbset{%
  background color/.code={%
    \IfDecimal{#1}
    {\tcbset{standard jigsaw,opacityback=#1}}
    {\tcbset{standard jigsaw,opacityback=1,colback=#1}}
  }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NewDocumentCommand{\mybox}{sO{}}{%
  \begin{tcolorbox}[
        enhanced,
        breakable,
    before skip=1ex,after skip=1em,
    colback=black!5,colframe=black!50,
    boxrule=0.2mm,
    attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight},
    boxed title style={%
            frame code={%
            \path[fill=tcbcolback!30!black]
            ([yshift=-1mm,xshift=-1mm]frame.north west)
            arc[start angle=0,end angle=180,radius=1mm]
            ([yshift=-1mm,xshift=1mm]frame.north east)
            arc[start angle=180,end angle=0,radius=1mm];
            \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
            middle color=tcbcolback!80!black]
            ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
            [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
            -- (frame.south east) -- (frame.south west)
            -- ([xshift=-1mm,yshift=-1mm]frame.north west)
            [sharp corners]-- cycle;
            },
            interior engine=empty,
    },
        drop fuzzy shadow southwest,
        colbacktitle=green,
    fonttitle=\bfseries\IfBooleanTF{#1}{\normalsize}{\Large},
    title=\IfBooleanTF{#1}{Abstract}{Contents},
        #2
    ]
  \tableofcontents%
\end{tcolorbox}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}
%Test 1:
\begin{tcolorbox}[background color=0]
  Here the new option ``background color'' works fine
\end{tcolorbox}

%Test 2:
\mybox% This also does work

%Test 3:
\mybox[background color=0]% Introducing the new option "background color" causes error.

\section{first section}
\section{second section}
\end{document}

相关内容