18

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)

17

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

  • It seems that hbox doesn't work in case of bullets inside tcolorbox. Can you please give some hint on this? – Sandeep Singh Apr 12 '15 at 14:39
  • @SandeepSingh Are you using an itemize inside? Can you post the content in your question instead of This is a tcolorbox line? – user11232 Apr 12 '15 at 14:44
  • I was trying to generalize the usage for this box, added the code for bullet list. Thanks – Sandeep Singh Apr 12 '15 at 14:51
  • 1
    @SandeepSingh No not like that. See the edited answer. Also use center title. – user11232 Apr 12 '15 at 15:13
7

Perhaps there is natural width option, I did not find it.

A small workaround: Wrap the text into a box and get the width of it:

\documentclass{article}

\usepackage[most]{tcolorbox}

\begin{document}

\newsavebox{\mybox}
\savebox{\mybox}{This is a tcolorbox}


\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},text width=\wd\mybox]
       \usebox{\mybox}  % Release it ;-)
       \end{tcolorbox}


\end{document}

Since the width of text is relevant, one should use the (new) text width option, not the full width option.

enter image description here

  • How could this be put into the beamerTheme.sty file? Where would you include the tcolorbox configuration? – Vass Sep 13 '17 at 18:46
  • @Vass: This should work basically out of the box for beamer theme file as well or use a \newtcolorbox command in order to incorporate the options given to \tcbset. – user31729 Sep 13 '17 at 20:12
  • I tried to and found new difficulties. Maybe you want to provide an answer to my question at tex.stackexchange.com/q/391241/1026 ? – Vass Sep 13 '17 at 21:00
  • 1
    @Vass: That's a beamer related question. There are real experts for beamer, but I am not one of them – user31729 Sep 13 '17 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.