1

I am working with the following document and would like the red title box to sit vertically centred right in the middle of the top rule of the text box. Of course, I could do this manually by adjusting the y-shift to the appropriate value, but I'd like something more automatic, as I may decide to change the box title at some point (say, I might choose to number them as Property 1.1, Example 1.2 etc). I thought maybe shifting by half the node height might do it, though I could be wrong about this. However, I have no idea how to specify the height of the node and apply it to y-shift. How could I achieve this?

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newcommand{\eqtitle}[2]{\large \textbf{#1 #2}}

\begin{document}

\begin{tcolorbox}[
    enhanced,
    colback=white,
    colframe=black,
    arc=10pt,
    top=11pt,
    left=11pt,
    right=11pt,
    boxsep=0pt,
    bottom=11pt,
    boxrule=1pt,
    drop fuzzy shadow,
    overlay={
        \node[inner sep=3pt,anchor=north west,fill=red,text=white,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at ([xshift=11pt,yshift=0pt]frame.north west) {Property};
    }
]
This is a property.
\end{tcolorbox}

\end{document} 

PS: I'm aware that this is an unusual way of specifying a frame/outline (as an overlay) and title for a tcolorbox, but I'm doing it this way for other (unrelated) reasons that I won't go into.

Here is an image of the current (undesirable) output for reference: enter image description here

1 Answer 1

3

If I understand your desired output correctly, this can easily achieved by following setup. Make the anchor to center and node location at frame.north:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newcommand{\eqtitle}[2]{\large \textbf{#1 #2}}

\begin{document}

\begin{tcolorbox}[
    enhanced,
    colback=white,
    colframe=black,
    arc=10pt,
    top=11pt,
    left=11pt,
    right=11pt,
    boxsep=0pt,
    bottom=11pt,
    boxrule=1pt,
    drop fuzzy shadow,
    overlay={
        \node[inner sep=3pt,anchor=center,fill=red,text=white,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at (frame.north) {Property};
    }
]
This is a property.
\end{tcolorbox}

\end{document} 

enter image description here

Update: To achieve what you want, you can set the anchor to center and measure the width of the property box before the overlay drawing and add 0.5 of the box width to the xshift value.

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\newcommand{\eqtitle}[2]{\large \textbf{#1 #2}}

\begin{document}

\begin{tcolorbox}[
    enhanced,
    colback=white,
    colframe=black,
    arc=10pt,
    top=11pt,
    left=11pt,
    right=11pt,
    boxsep=0pt,
    bottom=11pt,
    boxrule=1pt,
    drop fuzzy shadow, 
    before upper=\global\sbox0{\tikz{\node[inner sep=3pt,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at (0,0) {Property have longer title};}},
    overlay={
        \node[inner sep=3pt,anchor=center,fill=red,text=white,font=\bfseries, draw=black, line width=1pt, rounded corners=4pt] at ([xshift=11pt+0.5\wd0,yshift=0pt]frame.north west) {Property have longer title};
    }
]
This is a property.
\end{tcolorbox}

\end{document} 

enter image description here

2
  • It's a good attempt, but there's a reason I'm using overlays and not boxrule for the box outline (I won't go into why here, as it isn't relevant). I did mention this at the end of my post. I wanted the property box in the same horizontal position as in my example, but shifted vertically so it sits in the middle of the top line. Your answer doesn't quite do what I asked.
    – wrb98
    Commented Feb 13 at 16:19
  • @wrb98 I updated the answer.
    – Tom
    Commented Feb 13 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .