如何获取 tcolorbox 中框的自然高度

如何获取 tcolorbox 中框的自然高度

我需要进行一些计算,其中tcolorbox必须引入 a 的实际高度(请参阅以下代码以了解我的目的)。我想知道是否有这样的宏(我的代码中的 \macro 作为示例,而不是\tcbtextheight)来存储实际框高度的值。

\documentclass[12pt,a4paper]{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{tcolorbox}[
    lefthand width=3em,
    arc=3mm,
    boxrule=0pt,sidebyside,enhanced,
    colback=green!60,
    bicolor,colbacklower=gray,
    collower=white,
    underlay={
        \path[fill=green!60]
        (segmentation.south) -- ++(0.5\macro,0.5\macro)--(segmentation.north);},
    ]
    Upper
    \tcblower
    \quad This is the lower part.
\end{tcolorbox}

\end{document}

从上面的代码,我想得到以下效果 在此处输入图片描述

答案1

在这个例子中,最干净的方式是从 中提取高度尺寸segmentation。因此,

\tcbsetmacrotoheightofnode\macro{segmentation}

就可以了。

完整代码如下:

\documentclass[12pt,a4paper]{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{tcolorbox}[
    lefthand width=3em,
    arc=3mm,
    boxrule=0pt,sidebyside,enhanced,
    colback=green!60,
    bicolor,colbacklower=gray,
    collower=white,
    underlay={
        \tcbsetmacrotoheightofnode\macro{segmentation}
        \path[fill=green!60]
        (segmentation.south) -- ++(\dimexpr0.5\dimexpr\macro,\dimexpr0.5\dimexpr\macro)--(segmentation.north);},
    ]
    Upper
    \tcblower
    \quad This is the lower part.
\end{tcolorbox}

\end{document}

当然,如果盒子太大,看起来就不太好看。Ignasi 的解决方案没有给出直角,但对于不同的高度更稳定。

答案2

像这样?

\documentclass[12pt,a4paper]{article}
\usepackage[most]{tcolorbox}
%\tcbuselibrary{skins}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{tcolorbox}[
    enhanced,
    lefthand width=3em,
    arc=3mm,
    boxrule=0pt,sidebyside,enhanced,
    colback=green!60,
    bicolor,colbacklower=gray,
    collower=white,
    underlay={
        \path[fill=green!60]
        (segmentation.south) -- ([xshift=5mm]segmentation.center)--(segmentation.north)--cycle;},
    ]
    Upper
    \tcblower
    \quad This is the lower part.
\end{tcolorbox}
\end{document}

在此处输入图片描述

更新:

可以在calctikzlibrary 的帮助下绘制与框高度成比例的箭头。

\documentclass[12pt,a4paper]{article}
\usepackage[most]{tcolorbox}
\usetikzlibrary{calc}
\usepackage{lipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newtcolorbox{mybox}[1][]{%
    enhanced,
    lefthand width=3em,
    arc=3mm,
    boxrule=0pt,sidebyside,enhanced,
    colback=green!60,
    bicolor,
    colbacklower=gray,
    collower=white,
    underlay={
        \path[fill=green!60] 
        let \p1=($(segmentation.north)-(segmentation.south)$), 
            \n2={veclen(\x1,\y1)} in
        (segmentation.south) -- 
        ([xshift=\n2/2]segmentation.center) --
        (segmentation.north)
        {[rounded corners]-|(frame.west)|-(segmentation.south)}--
        cycle;},
    #1
}

\begin{document}
\begin{mybox}
    Upper
    \tcblower
    \quad \lipsum[2]
\end{mybox}

\begin{mybox}
    Upper
    \tcblower
    \quad This is one sentence
\end{mybox}
\end{document}

在此处输入图片描述

相关内容