6

tcolorbox

I am using this package over mdframed because it offers a plethora of features to create robust boxes (handles single and multipage situations).

How can I embed lists e.g. description lists within tcolorbox environments, yet have the right alignment automatically adjust itself to be flush with the rest of the content in the tcolorbox environment.

The solution should robust in that it can handle all embedded lists environments.

Problem

The embedded list's text box extends beyond the box (frame) of the myexample environment itself.

Code

\documentclass{article}
 \usepackage{tikz,lipsum} 
 \usepackage{tcolorbox}
 \tcbuselibrary{skins,breakable} 
 \newcounter{myexample} 

 \colorlet{examplecolor}{gray}
 \usepackage{enumitem}

 \setlist[description]{style=nextline,labelwidth=0pt,leftmargin=15pt,itemindent=\dimexpr-5pt-\labelsep\relax} % Global Setup Description List

 \newtcolorbox[use counter=myexample]{myexample}{%
    % myexample Frame Start
    empty,% Empty previously set parameters
    title={\large Example \thetcbcounter},
    % Attaching a box requires an overlay
    attach boxed title to top left,
    % (boxed title style requires an overlay)
    boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
    coltitle=examplecolor,fonttitle=\Large\bfseries,
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,
        breakable,pad at break=0mm,
    % Handles box when it exists on one page only
    overlay unbroken={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
    % Handles multipage box: first page
    overlay first={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
    % Handles multipage box: middle page
    overlay middle={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
    % Handles multipage box: last page
    overlay last={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
    }

 \begin{document}
 \lipsum[1-3]

\begin{description}
    \item [Fruit] Fruits tend to be sweet.
     \begin{description}
        \item [Apples] Here is some text.
             \begin{myexample}
             \lipsum[1]
             \begin{itemize}
                \item \lipsum[1]
            \end{itemize}
             \end{myexample}
    \end{description}
 \end{description}
\lipsum[1] % <-- Nothing to do with question, but I noticed that this text is white/invisible and I do not know why.

 \end{document}

Output

enter image description here

8

While removing parbox=false avoids the problem, it also changes the formatting inside the box -- most notably the parskip. So I would probably only reset \@totalleftmargin:

\documentclass[parskip=half]{scrartcl}
\usepackage{tikz,lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\begin{document}
\begin{description}
    \item [Fruit] Fruits tend to be sweet.
    \begin{tcolorbox}[parbox=false,before upper=\csname @totalleftmargin\endcsname0pt]
     blabl\par blbla
     \begin{itemize}
       \item\lipsum[1]
     \end{itemize}
    \end{tcolorbox}
     \begin{tcolorbox}[]
     blabl\par blbla
     \begin{itemize}
       \item\lipsum[1]
     \end{itemize}
    \end{tcolorbox}
 \end{description}

\end{document}

enter image description here

  • Thank you for that hint. I am not sure I see the difference in your example, however. Ahh now I do. "the blabl blbla"..So then, the parskip is inherited from the global parskip, correct? This is because the content is not limited by another scope (a parbox)… – Jonathan Komar Oct 14 '15 at 11:28
  • No it is not a scoping problem. A normal \parbox (and minipage) actively resets \parskip to zero (and some other values too). parbox=false avoids this, but imho it should better reset \@totalleftmargin in any case @ThomasSturm do you agree? – Ulrike Fischer Oct 14 '15 at 11:39
  • I was wondering... why would one want a \parskip inside a box? – user11232 Oct 14 '15 at 13:47
  • @HarishKumar: When as in this question tcolorbox is used to add a margin marker is would imho look odd if the parskip is different in the text then in the surrounding. – Ulrike Fischer Oct 14 '15 at 13:55
  • @UlrikeFischer I agree that parbox=false should reset \@totalleftmargin automatically. I think that this should also reset \leftskip and \rightskip - at least that seems to be no harm. – Thomas F. Sturm Oct 14 '15 at 14:19
5

This is what the manual says about /tcb/parbox key

enter image description here

Obey the manual, you don't need this key to be false here for good.

Remove parbox=false from the definition of myexample.

\documentclass{article}
 \usepackage{tikz,lipsum}
 \usepackage{tcolorbox}
 \tcbuselibrary{skins,breakable}
 \newcounter{myexample}

 \colorlet{examplecolor}{gray}
 \usepackage{enumitem}

 \setlist[description]{style=nextline,labelwidth=0pt,leftmargin=15pt,itemindent=\dimexpr-5pt-\labelsep\relax} % Global Setup Description List

 \newtcolorbox[use counter=myexample]{myexample}{%
    % myexample Frame Start
    empty,% Empty previously set parameters
    title={\large Example \thetcbcounter},
    % Attaching a box requires an overlay
    attach boxed title to top left,
    % (boxed title style requires an overlay)
    boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
    coltitle=examplecolor,fonttitle=\Large\bfseries,
    before=\par\medskip\noindent,boxsep=0pt,left=0pt,right=3mm,top=2pt,
        breakable,pad at break=0mm,
    % Handles box when it exists on one page only
    overlay unbroken={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
    % Handles multipage box: first page
    overlay first={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
    % Handles multipage box: middle page
    overlay middle={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
    % Handles multipage box: last page
    overlay last={\draw[examplecolor,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
    }

 \begin{document}
 \lipsum[1-3]

\begin{description}
    \item [Fruit] Fruits tend to be sweet.
     \begin{description}
        \item [Apples] Here is some text.
             \begin{myexample}
             \lipsum[1]
             \begin{itemize}
                \item \lipsum*[1]
            \end{itemize}
             \end{myexample}
    \end{description}
 \end{description}
\lipsum[1] % <-- Nothing to do with question, but I noticed that this text is white/invisible and I do not know why.

 \end{document}

enter image description here

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.