我在以下代码中遇到了意外行为:
\documentclass[tikz,border=3pt]{standalone}
\usetikzlibrary{shapes.multipart,positioning}
\begin{document}
\begin{tikzpicture}
\node[draw,rectangle split,
rectangle split parts=3,
rectangle split part fill={red!20,blue!20,yellow!80!red!20},
rectangle split ignore empty parts](top){%
Box 1
\nodepart{two}Box 2
\nodepart{three}\parbox{2in}{Box 3 which really can contain a lot of text just to see what is going on here}};
\node[below=1em of top,
draw,rectangle split,
rectangle split parts=3,
rectangle split part fill={red!20,blue!20,yellow!80!red!20},
rectangle split ignore empty parts]{%
Box 1
\nodepart{three}\parbox{2in}{Box 3 which really can contain a lot of text just to see what is going on here}};
\end{tikzpicture}
\end{document}
nodepart{three}
当 缺失时,无法正确填充nodepart{two}
,因此被忽略。我简化了代码—— 是\parbox
必需的,因为 中的文本有一些复杂的要求nodepart{three}
。
有解决方法吗?
更新
我感兴趣的是为什么它不起作用,而从表面上看,它应该起作用。蓝色表示必需的位;黄色表示应该做什么。有时没有必要的位,因此不应显示单元格——但会有解释性文字,由黄色背景表示。TikZ 似乎能够自动执行此操作,但有一个我指出的问题。如果这还不够清楚,我深表歉意。
我出于各种原因需要 TikZ,并且并没有真正寻找替代解决方案。我尝试了各种表格,但结果并不令人满意。
解决方法
这不是我问题的答案,但让我回去工作......
\documentclass[tikz,border=3pt]{standalone}
\usetikzlibrary{shapes.multipart,positioning,backgrounds}
\begin{document}
\begin{tikzpicture}
\node[draw,rectangle split,
rectangle split parts=3,
rectangle split part fill={red!20,blue!20,none},
rectangle split ignore empty parts] (X) {%
Box 1
\nodepart{three}\parbox{2in}{Box 3 which really can contain a lot of text just to see what is going on here}};
\begin{scope}[on background layer]
\draw[fill=yellow!80!red!20] (X.north west) rectangle (X.south east);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
使用tblr
fromtabularray
会容易得多:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{colspec={p{7cm}},
hlines, vlines,
row{1-Y}={halign=c},
row{1}={bg=red!20},
row{2-Y}={bg=blue!20},
row{Z}={bg=yellow!80!red!20}}
Box 1 \\
Box 2 \\
Box 3 which really can contain a lot of text just to see what is going on here
\end{tblr}
\medskip
\begin{tblr}{colspec={p{7cm}},
hlines, vlines,
row{1-Y}={halign=c},
row{1}={bg=red!20},
row{2-Y}={bg=blue!20},
row{Z}={bg=yellow!80!red!20}}
Box 1 \\
Box 3 which really can contain a lot of text just to see what is going on here
\end{tblr}
\medskip
\begin{tblr}{colspec={p{7cm}},
hlines, vlines,
row{1-Y}={halign=c},
row{1}={bg=red!20},
row{2-Y}={bg=blue!20},
row{Z}={bg=yellow!80!red!20}}
Box 1 \\
Box 2 \\
Box 2 \\
Box 2 \\
Box 3 which really can contain a lot of text just to see what is going on here
\end{tblr}
\end{document}