3
\documentclass{article}

\usepackage[listings]{tcolorbox}

\newtcblisting{mylisting}{
  listing only,
  size=fbox,
}

\newtcblisting{mylistingb}{
  listing only,
  size=fbox,
  hbox,
}

\begin{document}
\begin{itemize}
  \item
  \begin{mylisting}
blah
  \end{mylisting}

  \item
  \begin{mylistingb}
blah
  \end{mylistingb}
\end{itemize}
\end{document}

Outputs a strange behaviour:

enter image description here

Is it possible to overcome this or should I do what I'm doing right now which is putting my boxes outside of the itemize?

[EDIT1] It should be noticed that removing listing only makes the compilation fail and I don't understand why

[EDIT2] Actually the listing only problem only appears when hbox is set.

2
  • 1
    For the listing only problem, when hbox (a shortcut for capture=hbox) is used, the content cannot have a lower part, hence in your case listing only is mandatorily required. For the indentation problem, I've reported to github.com/T-F-S/tcolorbox/issues/113. Sep 18, 2020 at 20:54
  • Oh, well, I thought my problem could be fixed with a short answer but looks like this one is actually a bit more complicated. Thanks for reporting it on github (and thanks for the answer for hbox, I figured it was something like that but it's always better to have a confirmation :-))
    – Lhooq
    Sep 18, 2020 at 21:07

1 Answer 1

2

As @muzimuzhi Z alraedy explained, a hbox cannot have a lower part. Therefore, listing only is needed here.

The shift problem is caused by \@totalleftmargin which is increased inside an itemize environment. In a normal tcolorbox, the content is put inside a minipage where \@totalleftmargin is set to zero. For a hbox type tcolorbox, there is currently no such setting. It behaves like \fbox.

But, I think it would be useful to add some minipage features to the hbox type to solve such problems in a general way. The following patch adds \@parboxrestore from minipage to hbox:

\long\def\tcbox@inner@hbox#1{%
  \tcbset{breakable@false,sidebyside=false}%
  \tcb@set@@phantom%
  \sbox\tcb@upperbox{\tcb@embed@tcbox{%
    \csname tcb@parbox@use@\kvtcb@parbox\endcsname%
    \@parboxrestore%
    \color{tcbcolupper}\kvtcb@fontupper\kvtcb@halignupper\tcb@insert@before@upper#1\tcb@insert@after@upper}}%
  \tcbdimto\tcb@val@raisebase{\the\dimexpr\dp\tcb@upperbox+\kvtcb@bottom+\kvtcb@boxsep+\kvtcb@bottom@rule@stand+\kvtcb@bbbottom@stand\relax}%
  \tcbdimto\kvtcb@width{\wd\tcb@upperbox+\kvtcb@left@rule+\kvtcb@leftupper+\kvtcb@boxsep*2+\kvtcb@rightupper+\kvtcb@right@rule}%
  \tcb@hasLowerfalse%
  \tcb@set@@title%
  \tcb@set@@dimensions%
  \tcb@draw@color@box%
}

The full example is

\documentclass{article}

\usepackage[listings]{tcolorbox}

% patch
\makeatletter
\long\def\tcbox@inner@hbox#1{%
  \tcbset{breakable@false,sidebyside=false}%
  \tcb@set@@phantom%
  \sbox\tcb@upperbox{\tcb@embed@tcbox{%
    \csname tcb@parbox@use@\kvtcb@parbox\endcsname%
    \@parboxrestore%
    \color{tcbcolupper}\kvtcb@fontupper\kvtcb@halignupper\tcb@insert@before@upper#1\tcb@insert@after@upper}}%
  \tcbdimto\tcb@val@raisebase{\the\dimexpr\dp\tcb@upperbox+\kvtcb@bottom+\kvtcb@boxsep+\kvtcb@bottom@rule@stand+\kvtcb@bbbottom@stand\relax}%
  \tcbdimto\kvtcb@width{\wd\tcb@upperbox+\kvtcb@left@rule+\kvtcb@leftupper+\kvtcb@boxsep*2+\kvtcb@rightupper+\kvtcb@right@rule}%
  \tcb@hasLowerfalse%
  \tcb@set@@title%
  \tcb@set@@dimensions%
  \tcb@draw@color@box%
}
\makeatother


\newtcblisting{mylisting}{
  listing only,
  size=fbox,
}

\newtcblisting{mylistingb}{
  listing only,
  size=fbox,
  hbox,
}

\begin{document}
\begin{itemize}
  \item
  \begin{mylisting}
blah
  \end{mylisting}

  \item
  \begin{mylistingb}
blah
  \end{mylistingb}
\end{itemize}
\end{document}

If nothing critical comes up, I intend to add this patch to the next tcolorbox version.

1
  • Thanks to both of you!
    – Lhooq
    Sep 21, 2020 at 9:58

You must log in to answer this question.

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