在以下情况下,当外部左框高于外部右框时,我希望内部框(外部右框的光栅内)具有尽可能大的高度。
\documentclass[11pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\tcbuselibrary{raster}
\begin{document}
\tcbset{%
outer left/.style={frame hidden, boxrule=0pt, boxsep=5pt, colback=yellow, halign=center, valign=center},
outer right/.style={frame hidden, boxrule=0pt, boxsep=2pt, colback=orange, halign=center, valign=center},
inner right/.style={boxsep=3pt, halign=center, valign=center}
}
\begin{tcbraster}[raster columns=2, raster equal height=rows, raster valign=center]
\begin{tcolorbox}[outer left]
w\\
x\\
y\\
z
\end{tcolorbox}
%
\begin{tcolorbox}[outer right]
\begin{tcbraster}[raster columns=3]
\begin{tcolorbox}[inner right]
d
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
e
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
f
\end{tcolorbox}
\end{tcbraster}
\end{tcolorbox}
\end{tcbraster}
\end{document}
我想出了一个 hacky 解决方案(远非完美),经过两次连续编译后似乎有效。
\documentclass[11pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\tcbuselibrary{raster}
\begin{document}
\tcbset{%
outer left/.style={frame hidden, boxrule=0pt, boxsep=5pt, colback=yellow, halign=center, valign=center},
outer right/.style={frame hidden, boxrule=0pt, boxsep=2pt, colback=orange, halign=center, valign=center},
inner right/.style={boxsep=3pt, halign=center, valign=center, ht}
}
\makeatletter
\begin{tcbraster}[raster columns=2, raster equal height=rows, raster valign=center]
\begin{tcolorbox}[outer left]
w\\
x\\
y\\
z
\end{tcolorbox}
%
\begin{tcolorbox}[outer right,
code={%
\edef\tcb@ehgid{\kvtcb@raster@ehgid @\number\c@tcbrasterrow}%
\ifcsundef{tcb@ehg@height@\tcb@ehgid}%
{\tcbset{ht/.style={}}}%
{%
\tcbheightfromgroup\myht{\tcb@ehgid}%
\tcbdimto\myresult{\kvtcb@top@rule@stand+\kvtcb@bottom@rule@stand+\kvtcb@boxsep*2+\kvtcb@top+\kvtcb@bottom}%
\iftcb@hasTitle%
\tcbdimto\myresult{\tcbtextheight+\ht\tcb@titlebox+\dp\tcb@titlebox+\kvtcb@title@rule+\kvtcb@boxsep*2+\kvtcb@toptitle+\kvtcb@bottomtitle}%
\fi%
\tcbdimto\myresult{\myht-\myresult}%
\tcbset{ht/.style={height=\myresult}}%
}%
}
]
\begin{tcbraster}[raster columns=3]
\begin{tcolorbox}[inner right]
d
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
e
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
f
\end{tcolorbox}
\end{tcbraster}
\end{tcolorbox}
\end{tcbraster}
\makeatother
\end{document}
但是这个解决方案要求我删除辅助文件,如果代码有任何改变(框内容或设置等等boxsep
),就需要重新编译两次。
有谁有更好的解决方案吗?谢谢!
答案1
我想我已经找到了解决我的问题的方法,它基于密钥的使用space to
。
如果是包含在外面的右框中的tcolorbox,我们可以使用该add to natural height
键。
另一方面,如果它是一个光栅,height
当外部右框没有其自然高度(\myspace>0pt
)时,我们将使用该键。
\documentclass[11pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\tcbuselibrary{raster}
\begin{document}
\tcbset{%
ht/.style={},
outer left/.style={frame hidden, boxrule=0pt, boxsep=5pt, colback=yellow, halign=center, valign=center},
outer right/.style={frame hidden, boxrule=0pt, boxsep=2pt, colback=orange, halign=center, valign=center},
inner right/.style={boxsep=3pt, halign=center, valign=center, ht}
}
tcolorbox inside outer right box :
\begin{tcbraster}[raster columns=2, raster equal height=rows, raster valign=center]
\begin{tcolorbox}[outer left]
w\\x\\y\\z
\end{tcolorbox}
%
\begin{tcolorbox}[outer right, space to=\myspace]
\begin{tcolorbox}[inner right, add to natural height=\myspace]
d
\end{tcolorbox}
\end{tcolorbox}
\end{tcbraster}
\begin{tcbraster}[raster columns=2, raster equal height=rows, raster valign=center]
\begin{tcolorbox}[outer left]
w
\end{tcolorbox}
%
\begin{tcolorbox}[outer right, space to=\myspace]
\begin{tcolorbox}[inner right, add to natural height=\myspace]
d\\e\\f
\end{tcolorbox}
\end{tcolorbox}
\end{tcbraster}
tcbraster inside outer right box :
\begin{tcbraster}[raster columns=2, raster equal height=rows, raster valign=center]
\begin{tcolorbox}[outer left]
w\\x\\y\\z
\end{tcolorbox}
%
\begin{tcolorbox}[outer right, space to=\myspace]
\ifdim\myspace>0pt\relax\tcbset{ht/.style={height=\myspace}}\fi%
\begin{tcbraster}[raster columns=3]
\begin{tcolorbox}[inner right]
d
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
e
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
f
\end{tcolorbox}
\end{tcbraster}
\end{tcolorbox}
\end{tcbraster}
\begin{tcbraster}[raster columns=2, raster equal height=rows, raster valign=center]
\begin{tcolorbox}[outer left]
w
\end{tcolorbox}
%
\begin{tcolorbox}[outer right, space to=\myspace]
\ifdim\myspace>0pt\relax\tcbset{ht/.style={height=\myspace}}\fi%
\begin{tcbraster}[raster columns=3]
\begin{tcolorbox}[inner right]
d\\e\\f
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
f
\end{tcolorbox}
%
\begin{tcolorbox}[inner right]
g
\end{tcolorbox}
\end{tcbraster}
\end{tcolorbox}
\end{tcbraster}
\end{document}