2

The following example only seems to work if I remove the hbox option.

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{listings,skins}


\newtcblisting[auto counter,list inside=example]{example}[2][]{%
    title=#2,
    #1,
    %hbox,%hbox doesn't work?
    center,
    colback=green!20,
    colbacktitle=green!50,
    colframe=green,
    %listing only,
    lower separated=false,%remove lines inside
    fonttitle=\bfseries\sffamily\large,coltitle=black,titlerule=0pt,enhanced,drop fuzzy shadow}

\begin{document}

\begin{example}[listing side text]{An example}
\textbf{bold text}

\textit{italics text}
\end{example}

\end{document}

enter image description here

If I include hbox, tcolorbox generates an error (Undefined control sequence. \end{example}) and the content is misaligned (too little space between the left side and the right side):

enter image description here

If I use the option listing only, everything works fine.

8
  • 1
    The documentation for hbox says the following: "This is the default mode for \tcbox. The content cannot have a lower part and cannot be broken. The colored box is sized according to the dimensions of the content." AFAIK the "text" of a listing is treated as lower part.
    – TeXnician
    Commented Dec 2, 2017 at 16:41
  • @TeXnician: You mean the code output of the TeX listing is treated as lower box. listings only displays the TeX code only in the upper part and with this hbox works, of course
    – user31729
    Commented Dec 2, 2017 at 17:02
  • I see. I was hoping there as a workaround. Anyway, a box spanning the whole text width doesn't bother much, and we can still specify its width.
    – user9424
    Commented Dec 2, 2017 at 17:06
  • @TeXnician: You wrote "text" of a listing -- this could be misunderstood as the code listing itself
    – user31729
    Commented Dec 2, 2017 at 17:07
  • 1
    @Joseph: By the way, you should add #1 at the end of your code options, not right at the start, where following code options override your settings
    – user31729
    Commented Dec 2, 2017 at 17:16

1 Answer 1

3

In the documentation of tcolorbox you'll find the following aspect for capture=hbox (which hbox stands for):

This is the default mode for \tcbox. The content cannot have a lower part and cannot be broken. The colored box is sized according to the dimensions of the content.

You are using the option listing side text which typesets the output (text) as the lower part/lower box of the tcolorbox. This contradicts "cannot have a lower part".

So the only way to get accurate width adjustments is a manual one. The automated way doesn't work for your use case.

You must log in to answer this question.