TransWikia.com

Strange indentation of tcblisting inside itemize

TeX - LaTeX Asked on May 31, 2021

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.

One Answer

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:

longdeftcbox@inner@hbox#1{%
  tcbset{breakable@false,sidebyside=false}%
  tcb@set@@phantom%
  sboxtcb@upperbox{tcb@embed@tcbox{%
    csname tcb@parbox@use@kvtcb@parboxendcsname%
    @parboxrestore%
    color{tcbcolupper}kvtcb@fontupperkvtcb@halignuppertcb@insert@before@upper#1tcb@insert@after@upper}}%
  tcbdimtotcb@val@raisebase{thedimexprdptcb@upperbox+kvtcb@bottom+kvtcb@boxsep+kvtcb@bottom@rule@stand+kvtcb@bbbottom@standrelax}%
  tcbdimtokvtcb@width{wdtcb@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
longdeftcbox@inner@hbox#1{%
  tcbset{breakable@false,sidebyside=false}%
  tcb@set@@phantom%
  sboxtcb@upperbox{tcb@embed@tcbox{%
    csname tcb@parbox@use@kvtcb@parboxendcsname%
    @parboxrestore%
    color{tcbcolupper}kvtcb@fontupperkvtcb@halignuppertcb@insert@before@upper#1tcb@insert@after@upper}}%
  tcbdimtotcb@val@raisebase{thedimexprdptcb@upperbox+kvtcb@bottom+kvtcb@boxsep+kvtcb@bottom@rule@stand+kvtcb@bbbottom@standrelax}%
  tcbdimtokvtcb@width{wdtcb@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.

Correct answer by Thomas F. Sturm on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP