2

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{tcolorbox}[flush right,title=Answer,hbox]
    \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
2

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

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

\newtcolorbox{myenvironment}{
  fit,flush right,title=Answer,hbox,left=0mm,right=0mm
}

\begin{document}

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

\end{document}

produces

enter image description here

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

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.