# How to fit the width of a tcolorbox to a long title (single line)?

Using "clip title=false" enables a tcolorbox long title to sit on a single line. However, the width of the tcolorbox itself remains fitted to the content. How can the tcolorbox width be fitted to the natural width of a long title that sits on a single line (not wrapped) ?

\documentclass[]{article}
\usepackage[skins,listings,breakable,listingsutf8,theorems,hooks,fitting]{tcolorbox}
\usepackage{calc}

\newtcbox{\LongTitleBox}[1][]{enhanced,title=\mbox{Title Larger than the Box Content},clip title=false,colbacktitle=white,coltitle=black,#1}%

\newtcbox{\LongTitleBoxResult}[1][]{enhanced,title=\mbox{Final Result},clip title=false,colbacktitle=white,coltitle=black,#1}%

\begin{document}
\LongTitleBox{Box Content}
\LongTitleBoxResult{1}
\LongTitleBoxResult{123 456 789}
\end{document}


• Is it really necessary to have such a long title? You could try width=\widthof{Title Larger than the Box Content}} as optional argument to your \LongTitleBox definition, you need \usepackage{calc} for that – user31729 May 13 '14 at 15:47
• Unfortunately, "width=\widthof{Title Larger than the Box Content}" doesn't fit the width of the box to the width of the title. The title is very long in my example to show the problem clearly. In fact, my "own" title is sometimes longer and sometimes shorter than the tcolorbox content. My title is always the same, but the tcolorbox contains a number of a variable number of digits (eg.: title = Result, content#1 = 1, content#2 = 123 456 789) – SDrolet May 13 '14 at 16:03

That's an interesting question. My solution proposal tricks the title invisibly into the box content. Thereby, the box is sized according to the maximum of the title width and the content width. Note that this solution breaks, if different fonts are applied to title and box content (but this can easily adapted).

\documentclass[]{article}
\usepackage[skins]{tcolorbox}

\tcbset{longtitle/.style={%
title={#1},
before upper={\begin{tabular}{@{}l@{}}\phantom{#1}\\[\the\dimexpr-\ht\strutbox-\dp\strutbox]},
after upper={\end{tabular}}}}

\newtcbox{\LongTitleBox}[1][]{enhanced,longtitle={Title Larger than the Box Content},colbacktitle=white,coltitle=black,#1}%

\newtcbox{\LongTitleBoxResult}[1][]{enhanced,longtitle={Final Result},colbacktitle=white,coltitle=black,#1}%

\begin{document}

\LongTitleBox{Box Content}
\LongTitleBoxResult{1}
\LongTitleBoxResult{123 456 789}
\LongTitleBoxResult{123 456 789 012}

\end{document}


• Your solution works very well, thanks ! Your tcolorbox package is the most pleasurable package I use for making my book. Thanks very much for the whole package. – SDrolet May 14 '14 at 14:55

As Thomas F. Sturm noted "this solution breaks, if different fonts are applied to title and box content". Here is "one" simple adaptation of Thomas F. Sturm solution:

\usepackage[skins]{tcolorbox}

\def\TitleFontSize{\scriptsize}
\def\ContentFontSize{\huge}

\tcbset{longtitle/.style={title={#1},before upper={\begin{tabular}{@{}l@{}}\phantom{#1}\\[\the\dimexpr-\ht\strutbox-\dp\strutbox]},after upper={\end{tabular}}}}

\tcbset{longtitleDifferentSize/.style={title={#1},before upper={\begin{tabular}{@{}l@{}}\phantom{\TitleFontSize #1}\\[\the\dimexpr-\ht\strutbox-\dp\strutbox]},after upper={\end{tabular}}}}

\newtcbox{\LongTitleBoxPhantomFontSameSizeAsTitleFont}[1][]{enhanced,longtitleDifferentSize={Title Larger than the Box Content},fonttitle=\TitleFontSize,colbacktitle=white,coltitle=black,fontupper=\ContentFontSize, #1}%

\newtcbox{\LongTitleBoxPhantomFontSameSizeAsContentFont}[1][]{enhanced,longtitle={Title Larger than the Box Content},fonttitle=\TitleFontSize,colbacktitle=white,coltitle=black, fontupper=\ContentFontSize,#1}%

\begin{document}

\LongTitleBoxPhantomFontSameSizeAsTitleFont{Box Content}
\LongTitleBoxPhantomFontSameSizeAsContentFont{Box Content}

\end{document}