我想绘制一条分隔两列的规则。不幸的是,如果单元格宽度不同,它将无法正常工作。有没有已知的解决方法?
我的代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes]{
a & b & c \\
d & long row & e \\
f & g & h \\
};
\draw (m-1-2.north east -| m-3-2.south east) -| (m-3-2.south east);
\end{tikzpicture}
\end{document}
结果(注意与长单元格相交的规则):
答案1
这个怎么样?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes]{
\node(a){a}; & \node(b){b}; & \node(s1){}; & \node(c){c}; \\
\node(d){d}; & \node(long){long row}; & \node(s2){}; & \node(e){also long}; \\
\node(f){f}; & \node(g){g}; & \node(s3){}; & \node(h){h}; \\
};
\draw[red] (s1.north) -- (s3.south);
\end{tikzpicture}
\end{document}
答案2
您可以指定text width
这些node
s 的宽度为long row
和align=center
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes]{
a & |[text width=\widthof{long row},align=center]|b & c \\
d & |[align=center]|long row & e \\
f & |[text width=\widthof{long row},align=center]|g & h \\
};
\draw (m-1-2.north east -| m-3-2.south east) -| (m-3-2.south east);
\end{tikzpicture}
\end{document}
更新:
您可以通过添加来修复text width
整个第二列
column 2/.style={nodes={text width=\widthof{long row},align=center}}
到matrix
选项。
代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells,
column 2/.style={nodes={text width=\widthof{long row},align=center}}]{
a & b & c \\
d & long row & e \\
f & g & h \\
};
\draw (m-1-2.north east -| m-3-2.south east) -| (m-3-2.south east);
\end{tikzpicture}
\end{document}
答案3
添加具有可选文本高度的空单元格来调整规则的位置。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, nodes in empty cells]{
a & b &|[text height=1em]| & c \\
d & long row && e \\
f & g &|[text height=1em]|& h \\
};
\draw[red] (m-1-2.north east -| m-3-3.south) -| (m-3-3.south);
\end{tikzpicture}
\end{document}