1

The following MWE fails with the error message \begin{tcolorbox} on input line 10 ended by \end{foo}. Removing the option hbox leads to succes, though of course without the desired effect of reducing the box to the smallest possible width.

\documentclass{article}

\usepackage{tcolorbox}

\newenvironment{foo}{begin}{end}
\tcolorboxenvironment{foo}{hbox}

\begin{document}

\begin{foo}
  asdf
\end{foo}

\end{document}

Is this a bug or intended behavior? I could not find anything about it in the documentation. If it is not a bug, is there any way to get around this?


(In reality, my environment is a bit more complex, of course, which is why I don't just use \newtcolorbox. The main issue preventing me from doing so is that it has several optional arguments like \NewDocumentEnvironment{foo}{ O{one} O{two} }{begin with #1 and #2}{end} and I want the color box environment to also be a tabular environment. In any case, the question stands, as applications where one wants to box an environment from a package (without wanting to touch the package code) would run into the same issue.)

5
  • Environment foo is inheriting \textwidth, and tcb is just wrapping itself as minimally around foo as possible (before hbox option even starts throwing its braces around and unbalances things). Need to reduce foos width to actual text width somehow, like \mbox does. Perhaps a savebox and calculate actual width. Will foo be multi-line, multi-paragraph?
    – Cicada
    Commented Nov 10, 2021 at 10:10
  • @Cicada I'm not sure I get what you mean. The content of foo is only (attempted to be) typeset after the hbox option kicks in. The issue here is that tcolorbox tries to capture its content looking for \end{tcolorbox} which isn't in the input stream (yet). See also the corresponding bug report.
    – schtandard
    Commented Nov 10, 2021 at 13:33
  • I was thinking to get the natural width of foo's text (and not foo's width of \textwidth) and bypass hbox option altogether. Manually adjusting \textwidth before foo's use, shrinks foo, for example, and the tcb follows suite.
    – Cicada
    Commented Nov 10, 2021 at 13:41
  • @Cicada So you're saying, we should replicate what hbox usually does? (Capturing the environment content in a box before typesetting the tcolorbox around it?) Also, I'm still confused about what you mean by "foo's width of \textwidth". Nothing in foo ever gets to be \textwidth wide in this MWE. (In fact, nothing inside foo gets typeset at all.) \textwidth is just an unused value here.
    – schtandard
    Commented Nov 10, 2021 at 14:25
  • Raising the issue with the package maintainer is the best way. I was just examining the behaviour of foo and the tcb without hbox option, to get a baseline. That part is interesting but really is a different MWE, and your question specifically relates to using hbox option, so apologies for confusing matters.
    – Cicada
    Commented Nov 11, 2021 at 0:02

2 Answers 2

0

This way it works

\documentclass{article}

\usepackage{tcolorbox}

%\newenvironment{foo}{begin}{end}
%\tcolorboxenvironment{foo}{hbox}

\newtcolorbox{foo}{hbox}

\begin{document}

\begin{foo}
  asdf
\end{foo}

\end{document}

enter image description here

1
  • Well, if using \newtcolorbox were an option, this would work. I just used a trivial environment to keep the example minimal. I elaborated a bit on this in the question.
    – schtandard
    Commented Nov 9, 2021 at 14:45
0

\tcolorboxenvironment is currently only compatible with the capture mode minipage (the default), not with hbox or fitbox. An enhancement extending it to those other modes is being worked on and will likely be included in the next version (4.52).

Until then, the workaround is to collect the environment body first and then insert \begin{tcolorbox} and \end{tcolorbox} in the same expansion step (i.e. in the begin code of the environment). This can be done using xparse's b argument type (ore using environ if you want to be old-fashioned).

\documentclass{article}

\usepackage{tcolorbox}

\NewDocumentEnvironment{foo}{ +b }{%
  \begin{tcolorbox}[hbox]
    begin%
    #1%
    end%
  \end{tcolorbox}
}{}

\begin{document}

\begin{foo}
  asdf
\end{foo}

\end{document}
2
  • 1
    \NewDocumentEnvironment{wrappedtcb}{ m O{} +b }{ \begin{tcolorbox}[hbox,#2] \begin{#1} #3 \end{#1} \end{tcolorbox}}{} would be more flexible. Noted the + in +b to accept b long. Commented Nov 13, 2021 at 14:46
  • @muzimuzhiZ Thanks for that note, I made the example long and just included the tcolorbox in the definition of foo; that's what the original goal was, after all (giving foo a tcolorbox without needing to change its use in the document).
    – schtandard
    Commented Nov 13, 2021 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .