我正在尝试制作一个\开始{tcolorbox}它会占用页面的剩余部分以及其中嵌套的其他 tcolorbox,它们又会占用外部 tcolorbox 剩余的可用区域,即嵌套框会占用页面的剩余部分,以匹配我试图模仿的 Microsoft Word 表格的格式。然而,我想我一定是遇到了 Latex 中的某种基本问题或设计特性,因为我似乎找不到任何允许我将 makebox/tcolorbox 等的底部设置为页面底部或将大小设置为剩余页面大小的东西。我尝试使用的代码如下所示。(即在下面的代码中我不想使用 6em、30em 等)。如果可能的话,你能否也给我解释一下为什么这通常很难做到。
我感谢任何人给予我的帮助,也感谢迄今为止一路上帮助过我的所有人。我确信现在我肯定已经迷路或放弃了。
\begin{tcolorbox}[title=Outter tcolor Bo : I want to consume the whole page height,height=\textheight]
Hello
\begin{tcolorbox}[breakable,title=First Box: I want consume only required height,height=6em]
I woud like this tcbbox to be a natural height and not inherit height from parent.
\end{tcolorbox}
\begin{tcolorbox}[breakable,title=Second Box: I want to consume the remaining height,height=30em]
I woud like this tcbbox to consume the rest of the page
\end{tcolorbox}
\end{tcolorbox}
答案1
实现这样的结果有点棘手,因为内部文本内容是先组装的,然后构造外部框。我的答案侵入了内部tcolorbox
并将可用空间保存到写入aux
文件的宏中。经过两次编译后,内部部分“知道”可用空间。保存宏由选项定义save height
。
\documentclass{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\makeatletter
\tcbset{%
save height/.code={%
\tcbset{breakable}%
\providecommand{#1}{2cm}%
\def\tcb@split@start{%
\tcb@breakat@init%
\tcb@comp@h@page%
\def\tcb@ch{%
\tcbset{height=\tcb@h@page}%
\tcbdimto#1{#1+\tcb@h@page-\tcb@natheight}%
\immediate\write\@auxout{\string\gdef\string#1{#1}}%
\tcb@ch%
}%
\tcb@drawcolorbox@standalone%
}%
}%
}
\makeatother
\begin{document}
\tcbset{colback=yellow!10!white,colframe=red!50!black,fonttitle=\bfseries}
\begin{tcolorbox}[save height=\MyHeight,title=Outer tcolorbox: I want to consume the whole page height]
Hello
\begin{tcolorbox}[title=First Box: I want consume only required height,]
I woud like this tcbbox to be a natural height and not inherit height from parent.
\end{tcolorbox}
\begin{tcolorbox}[title=Second Box: I want to consume the remaining height,height=\MyHeight]
I woud like this tcbbox to consume the rest of the page
\end{tcolorbox}
\end{tcolorbox}
\lipsum[1]
\begin{tcolorbox}[save height=\MyHeightTwo,title=Outer tcolorbox: I want to consume the whole page height]
Hello
\begin{tcolorbox}[title=First Box: I want consume only required height,]
I woud like this tcbbox to be a natural height and not inherit height from parent.
\end{tcolorbox}
\begin{tcolorbox}[title=Second Box: I want to consume the remaining height,height=\MyHeightTwo]
I woud like this tcbbox to consume the rest of the page
\end{tcolorbox}
\end{tcolorbox}
\end{document}