# tcolorbox: Vertically center title box

I'd like to vertically center the title of a box on the top rule of the box.

Unfortunatley I can't seem to find a way to calculate the height of the title box dopending on the title text and margins.

Is there a good way to do this?

Any advise as much appreciated :)

What I tried so far:

In the Quick reference, all lengths that play a role are listed and looking at the code, I also got the values

• /tcp/titlerule: \kvtcb@title@rule
• /tcb/toptitle: \kvtcb@toptitle
• /tcb/bottomtitle: \kvtcb@bottomtitle
• /tcb/boxsep: \kvtcb@boxsep

Also the Title text seems to be stored in \tcbtitletext.

However, \kvtcb@… doesn't seem to act as lenght, i.e. 0.5\kvtcb… gives an error, and I can only access \tcbtitletext inside the box env.

MWE:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{many}
\tcbset{
enhanced,
breakable,
attach boxed title to top left={
xshift=0.5cm,
yshift= -3.5mm, % What do I put here? I'd like to have something like:
%       yshift= -0.5\titleboxheight
},
top=4mm,
coltitle=black,
beforeafter skip=\baselineskip,
}

\newenvironment{mybox}[1]{%
\begin{tcolorbox}[title={A Box~--~#1}]%
}{
\end{tcolorbox}
}

\begin{document}
\begin{mybox}{This title box aligned correct}
Some text.
\end{mybox}

\begin{mybox}{This title box is too low}
Some text.
\end{mybox}

\begin{mybox}{\huge This title box is too high}
Some text.
\end{mybox}
\end{document}


Produces:

• Welcome to TeX.SX! You can insert a \strut in the title box to make the titles in box 1 and 2 have the same height. What is the result you expect for box 3? The text is much higher, consequently the box has to be higher too. – siracusa Mar 29 at 12:37
• That does indeed help a bit, thanks :) However, with \strut, the yshift is still depending on the fontsize: In box 3 I'd like the title box to be vertically centered on the top rule without manually changing the yshift (I added a line in the code to clarify the issue) – Hirsch Mar 29 at 13:08

see if yshift=-\tcboxedtitleheight/2 solve your problem (its use is described in 10.2.2 Options for the Boxed Title Placement, tcolorbox documentation, page 159):

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

\tcbset{
enhanced,
breakable,
attach boxed title to top left={
xshift=0.5cm,
yshift=-\tcboxedtitleheight/2},  % <---
top=4mm,
coltitle=black,
beforeafter skip=\baselineskip,
}
\newenvironment{mybox}[1]{%
\begin{tcolorbox}[title={A Box~--~#1}]%
}{
\end{tcolorbox}
}

\begin{document}
\begin{mybox}{This title box aligned correct}
Some text.
\end{mybox}

\begin{mybox}{This title box is too low}
Some text.
\end{mybox}

\begin{mybox}{\huge This title box is too high}
Some text.
\end{mybox}
\end{document}


• Thanks a lot! Looks like I was just too stupid to read the docs correctly … – Hirsch Mar 29 at 14:30
• no, in such huge documentation is not simply find an information which you need ... – Zarko Mar 29 at 15:04