tcolorbox 未按预期工作

tcolorbox 未按预期工作

我正在尝试为示例环境创建一个 tcolorbox,但它的行为会根据所写文本的大小而有所不同。例如,当没有文本时,我希望它看起来像这样: 在此处输入图片描述

但是我的代码看起来是这样的:

在此处输入图片描述

当有文本时,它的行为有时会按预期进行:

在此处输入图片描述

事情应该是这样的:

在此处输入图片描述

但当文本很少时无法正常工作在此处输入图片描述

这是盒子的代码:

\newtcolorbox{example}[1]{parbox=false, blanker, enhanced, breakable,
before skip = 5mm, after skip = 5mm, left=3mm, right=3mm, top=10mm, bottom=3mm,
colback=white, colframe=cor1, width=162mm, toprule=1pt, 
bottomrule=1pt, rightrule=1pt, leftrule=1pt, outer arc = 8mm, arc = 8mm, 
sharp corners = northwest, sharp corners = southeast, sharp corners = southwest,
title={{\boxfont EXEMPLO}\hspace{3.5mm} 
\textcolor{gray!80!black}{\Large\extitlefont\makefirstuc{#1}}}, 
boxed title style={empty, sharp corners}, 
attach boxed title to top left={xshift=-1.3 mm , yshift=-10mm},
underlay boxed title ={
% Draw the title underlay rectangle
\fill[cor1!30] ($(frame.north west) + (0,-.5mm)$) rectangle ($(title.north east)+(-.8mm,-8.5mm)$) coordinate (R);
%)
%;
% Filldraw the darker fancy in front of the rectangle and behind the Example. The coordinates should be better chosen.
\fill[cor1] (frame.north west) -- ($(title.south west) + (1.3mm,0)$) -- ++ (18mm,0mm) arc [start angle=-90, end angle=0, x radius = 2mm, y radius = 2mm] -- ($(frame.north west) + (20mm,0)$) --  cycle;


}
}

如果有人能帮助我我会非常感激。

答案1

您可以\ifstrempty根据是否有标题文本来区分标题。

由于您没有提供完整的 MWE,我发明了命令\boxfont\extitlefont和颜色cor1

\documentclass{exam}
\usepackage{mfirstuc}
\usepackage[most]{tcolorbox}
\usetikzlibrary{calc}
\newcommand{\boxfont}{\normalfont}%<---invented
\newcommand{\extitlefont}{\bfseries}%<---invented
\colorlet{cor1}{cyan!70!green}%<---invented
\newtcolorbox{example}[1]{
    parbox=false, 
    blanker, 
    enhanced, 
    breakable,
    before skip = 5mm, 
    after skip = 5mm, 
    left=3mm, 
    right=3mm, 
    top=10mm, 
    bottom=3mm,
    colback=white, 
    colframe=cor1, 
    width=162mm, 
    toprule=1pt, 
    bottomrule=1pt, 
    rightrule=1pt, 
    leftrule=1pt, 
    outer arc = 8mm, 
    arc = 8mm, 
    sharp corners = northwest, 
    sharp corners = southeast, 
    sharp corners = southwest,
    title={{\boxfont EXEMPLO}%
        \ifstrempty{#1}{\ignorespaces}{%
            \hspace{3.5mm}%
            \textcolor{gray!80!black}{\Large\extitlefont\makefirstuc{#1}}}}, 
    boxed title style={empty, sharp corners}, 
    attach boxed title to top left={xshift=-1.3 mm , yshift=-7mm},
        underlay boxed title ={
            \ifstrempty{#1}{%
                \fill[cor1] (frame.north west) -- ($(title.south west) + (1.3mm,0)$) -- ++ (20mm,0mm) arc [start angle=-90, end angle=0, x radius = 2mm, y radius = 2mm] -- ($(frame.north west) + (22mm,0)$) --  cycle;%
                }{%
                % Draw the title underlay rectangle
                \fill[cor1!30] ([yshift=-.9pt]frame.north west) rectangle ($(title.north east)+(-.8mm,-6.77mm)$) coordinate (R);
                % Filldraw the darker fancy in front of the rectangle and behind the Example. The coordinates should be better chosen.
                \fill[cor1] (frame.north west) -- ($(title.south west) + (1.3mm,0)$) -- ++ (20mm,0mm) arc [start angle=-90, end angle=0, x radius = 2mm, y radius = 2mm] -- ($(frame.north west) + (22mm,0)$) --  cycle;
            }
        }
    }
\begin{document}
\begin{example}{}{}
An example without title.
\end{example}
\begin{example}{The title}{}
An example with title.
\end{example}
\end{document}

在此处输入图片描述

相关内容