tcolorbox
我使用这个包是mdframed
因为它提供了大量的功能来创建强大的盒子(处理单页和多页情况)。
如何在tcolorbox
环境中嵌入列表(例如描述列表),同时让正确的对齐方式自动调整为与环境中的其余内容齐平tcolorbox
。
该解决方案应非常强大,因为它可以处理所有嵌入式列表环境。
问题
嵌入列表的文本框超出了环境本身的框(框架)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,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}
输出
答案1
虽然删除parbox=false
可以避免这个问题,但它也会改变框内的格式——最明显的是 parskip。所以我可能只会重置\@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}
答案2
这是手册中关于/tcb/parbox
钥匙的说法
遵守手册,你不需要这个密钥,因为它永远是假的。
parbox=false
从 的定义中删除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}