4

I need to do some calculation in which the actural height of a tcolorbox has to be introduced(Please see the following code for my purpose). I wonder if there is such a macro(\macro in my code as an example, not \tcbtextheight) to store the value of the actual box height.

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

From above code, I want to get the following effect enter image description here

3

In this examples, the cleanest way is to extract the height dimension from segmentation. Therefore,

\tcbsetmacrotoheightofnode\macro{segmentation}

will do the trick.

The full code is:

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

Of course, this will not look good, if the box becomes too large. The solution of Ignasi gives no rectangular angle, but is more stable for different heights.

  • Many thanks for your simple and perfect soluntion. Yet, I change "-- ++(\dimexpr0.5\dimexpr\macro,\dimexpr0.5\dimexpr\macro)" by " --([xshift=0.5\dimexpr\macro]segmentation.center)", the typeset gives no rectangle angle which I think it should be (as the height and width are equal). Could you tell me the cause? – lyl Sep 18 '17 at 8:35
  • And If the upper/lower parts are not sidebyside, how to get the natual height of a box? – lyl Sep 18 '17 at 8:46
  • For your changed code, you missed a \dimexpr before 0.5. – Thomas F. Sturm Sep 18 '17 at 11:12
  • The node frame can alternatively used for the height of the whole box (even without lower part), i.e. \tcbsetmacrotoheightofnode\macro{frame} – Thomas F. Sturm Sep 18 '17 at 11:13
  • Could you tell me why \dimexpr must be used before 0.5 and what the unit is for \tcbsetmacrotoheightofnode\macro{frame,segmentation etc},only a value? or a length? – lyl Sep 18 '17 at 13:14
7

Like this?

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

enter image description here

Update:

It's possible to draw an arrow proportional to box height with calc tikzlibrary help.

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

enter image description here

  • Thank you Ignasi, your solution does well In my example where the content of the lower part contains only one line of text. Howerver, the angle formed at the segmentation.center becomes larger if multilines of text are typeset in the lower part. This is because of the fixed value of xshift=5mm. So I think 0.5 height of the box should solve the problem which I don't know how to get it. Anyone could give me a clue to it(how to get the natual height of a tcolorbox)? – lyl Sep 17 '17 at 8:53
  • @lyl Please, see my update. – Ignasi Sep 18 '17 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.