[Tex/LaTex] How to restrict width of tcolorbox according to the length of the text it contains

formattingpdftextcolorboxwidth

I have created a tcolorbox (as given in tcolorbox version 3.40 manual – page 155)

LaTeX Code:

\tcbset{
        enhanced,
        colback=red!5!white,
        boxrule=0.1pt,
        colframe=red!75!black,
        fonttitle=\bfseries
       }
       My own shadow
       \begin{tcolorbox}[title=My own shadow,
       lifted shadow={1mm}{-2mm}{3mm}{0.1mm}%
       {black!50!white}]
       This is a tcolorbox.
       \end{tcolorbox}

Current Output:
enter image description here

Expected Output (produced by explicitly setting: width=\linewidth/3):

enter image description here

My problem is:

This box contains only a little text, but occupies the whole textwidth

I have referred a related question here:
How to set fit height and width for tcbox according to the text inside?

but the options like: minimal,tight distort the box.

Is there any option through which this box can grow automatically in width depending on the width of the text contained in it?

Note: Adding \hbox solves this problem for normal text, but leads to a compilation error in case of bullet list.

Example:

\tcbset
    {
        enhanced,
        left=8mm,
        right=8mm,
        boxrule=0.4pt,
        colback=red!5!white,
        boxrule=0.1pt,
        colframe=red!75!black,fonttitle=\bfseries,
    }
    \begin{tcolorbox}[
                        title=\begin{center}Sample Title\end{center},
                        hbox,
                        lifted shadow={1mm}{-2mm}{3mm}{0.1mm}{black!50!white}
                     ]
    \begin{itemize}
    \item First Line
    \item Second Line
    \end{itemize}
    \end{tcolorbox}

Compilation Error: LaTeX Error: Something's wrong–perhaps a missing \item

Just for reference — The updated answer shows how to resolve this issue as well (using varwidth)

Best Answer

You can use hbox

\documentclass{article}

\usepackage[most]{tcolorbox}

\begin{document}


\tcbset{
        enhanced,
        colback=red!5!white,
        boxrule=0.1pt,
        colframe=red!75!black,
        fonttitle=\bfseries
       }
       My own shadow
       \begin{tcolorbox}[title=My own shadow,hbox,    %%<<---- here
       lifted shadow={1mm}{-2mm}{3mm}{0.1mm}%
       {black!50!white}]
       This is a tcolor box
       \end{tcolorbox}


\end{document}

enter image description here

For revised question, you may need varwidth package to insert the itemize. Also to center the title, use the option center title instead of \begin{center}.

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{varwidth}   %% provides varwidth environment

\begin{document}


\tcbset{
        enhanced,
        colback=red!5!white,
        boxrule=0.1pt,
        colframe=red!75!black,
        fonttitle=\bfseries
       }
       My own shadow
       \begin{tcolorbox}[title=My own shadow,center title,hbox,    %%<<---- here
       lifted shadow={1mm}{-2mm}{3mm}{0.1mm}%
       {black!50!white}]
       \begin{varwidth}{\textwidth}
       This is a tcolor box.
       \begin{itemize}
         \item First Line
         \item Second Line
       \end{itemize}
       \end{varwidth}
       \end{tcolorbox}


\end{document}

enter image description here

Related Question