# Custom tcolorbox and varwidth environments

I would like to combine the tcolorbox environment and varwidth environment into one single environment, but I'm having trouble. I'd like to be able to achieve the following:

\begin{tcolorbox}[flush right,title=Answer,hbox]
\begin{varwidth}{\textwidth}
Whatever text blah blah blah
\end{varwidth}
\end{tcolorbox}


by using:

\begin{myenvironment}
Whatever text blah blah blah
\end{myenvironment}


I tried defining the environment as follows:

\newenvironment{myenvironment}{
\begin{varwidth}{\textwidth}
}{
\end{tcolorbox}
\end{varwidth}
}


but I'm getting loads of errors. Any ideas?

• Welcome to TeX.SX! Can you please expand the code snippets that you have posted to a full minimal working example. It is much easier to help you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. At the moment we have to guess what you are doing because we cannot reproduce your problem from the code that you have given us. – Andrew Apr 4 '18 at 23:44

Can't you just use the tcolorbox fitting library? In more detail, the MWE

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

\newtcolorbox{myenvironment}{
}

\begin{document}

\begin{myenvironment}
Whatever text blah blah blah
\end{myenvironment}

\end{document}


produces

You can further adjust the spacing , colours etc in the usual tcolorbox ways.