# Adjust newtcolorbox to text width in one or multiple lines

I'm trying to make an "example" tcolorbox which adjusts to its text width. I've tried hbox but it fails when we have multiple lines.

Using hbox

Without using hbox

Expected:

Yes, I've managed to get my expected solution, but creating another "example" tcolorbox. I'd like to use only one.

Here's my compilable example (first image):

\documentclass{article}

\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\tcbuselibrary{skins}

\newtcbtheorem[]{exa}{EXAMPLE}{
hbox,
fonttitle=\bfseries, fontupper=\normalsize,
theorem style=standard,
enhanced,frame hidden,
boxrule=0pt,
left=0.2cm,top=0.2cm, toptitle=0.1cm+1pt,bottomtitle=-0.1cm+0.5em,
bottomrule=1pt,
colback=white,coltitle=orange,
title style=white,
titlerule=1pt, titlerule style=orange,
borderline south={1pt}{0pt}{orange}
}{exa}

\begin{document}

\begin{exa}{}{}
hello
\end{exa}

\begin{exa}{}{}
\lipsum[1]
\end{exa}

\end{document}

• What about defining two different environments that share the same counter? Oct 19 '20 at 19:05
• @leandriis That's the problem, I'm looking for only one newtcolorbox Oct 19 '20 at 20:00

Here

• a new command \newtcboxtheorem based on \tcbox, is provided and
• the choice key tcbox width is extended to accept value auto limited with title counted, which will make the width of output \tcbox be min("/tcb/width", max(upper, title)).

By default, \tcbox defines a new command (the \exabox in the following example), hence the environment-version is also provided, which makes the input for starred/unnumbered theorem easier.

\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{skins, theorems}

\makeatletter
\def\newtcboxtheorem{%
\let\@@newtcolorbox\newtcbox
\new@tcbtheorem}

\tcbset{
tcbox width/auto limited with title counted/.code={\tcb@set@embed@tcbox{%
\setbox\z@=\color@hbox##1\color@endbox
\setbox\@tempboxa=\color@hbox\kvtcb@fonttitle\kvtcb@before@title\kvtcb@title\kvtcb@after@title\color@endbox
\@tempdima=\wd\z@
\@tempdimb=\wd\@tempboxa
\ifdim\@tempdima<\tcb@w@upper\relax
\ifdim\@tempdima<\@tempdimb
\begin{minipage}{\@tempdimb}##1\end{minipage}%
\else
\box\z@
\fi
\else
\begin{minipage}{\tcb@w@upper}##1\end{minipage}%
\fi}}
}
\makeatother

\newtcboxtheorem{exabox}{EXAMPLE}{
tcbox width=auto limited with title counted,
fonttitle=\bfseries, fontupper=\normalsize,
theorem style=standard,
enhanced,frame hidden,
boxrule=0pt,
left=0.2cm,top=0.2cm, toptitle=0.1cm+1pt,bottomtitle=-0.1cm+0.5em,
bottomrule=1pt,
colback=white,coltitle=orange,
title style=white,
titlerule=1pt, titlerule style=orange,
borderline south={1pt}{0pt}{orange}
}{exa}

\NewDocumentEnvironment{exa}{ O{} m m +b }{%
\exabox[#1]{#2}{#3}{#4}%
}{}
\NewDocumentEnvironment{exa*}{ O{} m +b }{%
\csname exabox*\endcsname[#1]{#2}{#3}%
}{}

\begin{document}

\def\dummy{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
}

\subsection*{Using command \texttt{\textbackslash exabox[*]}}
\exabox{}{}{short}

\exabox{}{}{long long longer than title}

\exabox{}{}{\dummy}

\csname exabox*\endcsname{}{unnumbered}

\subsection*{Using environment \texttt{exa[*]}}
\begin{exa}{}{}
short
\end{exa}

\begin{exa}{}{}
long long longer than title
\end{exa}

\begin{exa}{}{}
\dummy
\end{exa}

\begin{exa*}{}
unnumbered
\end{exa*}
\end{document}


One possible way forward is to make a new theorem command based on \tcbox which can then make use of the varwidth upper option.

Something like this, where I've made a new \tcboxtheorem macro, which could be used to define \exa and \exastar for numbered and unnumered theorems:

\documentclass{article}

\usepackage{lipsum}
\usepackage{varwidth}
\usepackage{tcolorbox}
\tcbuselibrary{theorems,skins}

\makeatletter
% \newtcboxtheorem[init options]{name}{display name}{options}{prefix}
\newcommand{\newtcboxtheorem}[5][]{%
\newtcbox[auto counter,#1]{#2}[3][]{%
#4,
varwidth upper,
title={\tcb@theo@title{#3}{\thetcbcounter}{##2}},
list entry={\protect\numberline{\thetcbcounter}##2},
nameref={##2},
code={\tcb@theo@label{#5}{##3}},
##1
}%
\newtcbox[#1,no counter,list inside=]{#2star}[2][]{%
#4,
varwidth upper,
title={\tcb@theo@title{#3}{\@empty}{##2}},
##1}%
}
\makeatother

\newtcboxtheorem[]{exa}{EXAMPLE}{
fonttitle=\bfseries, fontupper=\normalsize,
theorem style=standard,
enhanced, frame hidden,
boxrule=0pt,
left=0.2cm, top=0.2cm, toptitle=0.1cm+1pt, bottomtitle=-0.1cm+0.5em,
bottomrule=1pt,
colback=white, coltitle=orange,
title style=white,
titlerule=1pt, titlerule style=orange,
borderline south={1pt}{0pt}{orange}
}{exa}

\begin{document}
\exa{Title}{label}{hello}
\exastar{Title}{\lipsum[1]}
\end{document}


• I've edited my post. The important detail in my "expected" image is not the numbered title but the size of the box @David Purton Oct 20 '20 at 13:57
• @Á.Iborra, that's what my answer does. \exa with give you big or small boxes depending on the content. Oct 20 '20 at 21:12