我正在尝试在带有网格的 tikz 矩阵中插入多行,因此:
\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{tikzpicture}
\matrix (table) [%
matrix of nodes,
nodes in empty cells,
draw,
inner sep=0mm,
minimum size=7mm
] {%
\node[draw] {A}; & \node[draw] {B}; & \node[draw] {C}; & \node[draw] {D}; \\
\node[draw] {E}; & & & \node[draw] {G}; \\ };
\node[draw,fit=(table-2-2)(table-2-3)]{F};
\end{tikzpicture}
\end{document}
结果很糟糕。有办法解决这个问题吗?有更好的实现方法吗?谢谢。
答案1
如果您明确设置文本高度和深度,则可以更好地控制单元格的边框。此外,我会使用样式来使代码更易于维护。以下是基于“用垂直线对表格的每隔一行进行着色“。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\tikzset{
table nodes/.style={
rectangle,
draw=black,
align=center,
minimum height=7mm,
text depth=0.5ex,
text height=2ex,
inner xsep=0pt,
outer sep=0pt
},
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
table nodes
},
execute at empty cell={\node[draw=none]{};}
}
}
\begin{tikzpicture}
\matrix (first) [table,text width=7mm,name=table]
{
A & B & C & D\\
E & & & F\\
};
\node[draw,fit=(table-2-2)(table-2-3),table nodes]{F};
\end{tikzpicture}
\end{document}