我很难对齐 TikZ 矩阵内的节点内容。
我特别感兴趣的是实现下面这种表格:
我从互联网上找到了大部分代码,并对其进行了重新排列和添加,因此可能有多余的部分:
\documentclass[a4paper,openany,14pt]{extbook}%extbook
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black,font=\ttfamily\small},space/.style={minimum height=1.5em,matrix of nodes,row sep=-\pgflinewidth,column sep=-\pgflinewidth,column 1/.style={font=\ttfamily\small}},text depth=0.5ex,text height=2ex,nodes in empty cells]
\matrix (MYMAT) [matrix of nodes, ampersand replacement=\&,
row sep=-\pgflinewidth -1, column sep=-\pgflinewidth -1, % -1's are there to unthicken the inner borders
nodes={text width=7.2mm,text height=1.2mm,font=\ttfamily\small,anchor=center,},% align=center,inner ysep=5pt didn't help at all
column 1/.style={column sep=-\pgflinewidth + 8},
column 2/.style={nodes={draw,very thick}},
column 3/.style={nodes={draw,very thick}},]
{ 1321 \& 2233 \& \\
1322 \& 3111 \& \\
1323 \& 4010 \& \\ };
\end{tikzpicture}
\end{document}
如何实现所需的表格外观?
更新
用户ignasi
的回答从根本上解决了对齐问题并生成了足够好的表格。
\begin{tikzpicture}
\matrix (MYMAT) [matrix of nodes, nodes in empty cells,
row sep=-\pgflinewidth -1 , column sep=-\pgflinewidth -1,
nodes={draw, very thick, minimum width=10mm, inner sep=1mm, minimum height=5.0mm, font=\ttfamily\small, anchor=center,},
column 1/.style={nodes={draw=none, minimum width=20mm}},
]
{ 1321 & 2233 & \\
1322 & 3111 & \\
1323 & 4010 & \\ };
\end{tikzpicture}
因此,这是可接受的答案。但是,为了使节点的边缘稍微变窄,通过使minimum height=4mm
,第三列变为:
有没有办法进一步缩小节点边缘?
答案1
我不确定期望结果和实现结果之间的差异,但简化版本可能TiKZ
是:
\documentclass[a4paper,openany,14pt]{extbook}%extbook
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (MYMAT) [matrix of nodes, nodes in empty cells,
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
nodes={draw, minimum width=10mm, inner sep=1mm, minimum height=5mm, font=\ttfamily\small, anchor=center,},
column 1/.style={nodes={draw=none, minimum width=20mm}},
]
{ 1321 & 2233 & \\
1322 & 3111 & \\
1323 & 4010 & \\ };
\end{tikzpicture}
\end{document}
答案2
您不应该使用minimum height
比“字体高度 + 内部 ysep”更小的值。如果节点内容仅为数字(而非字母),则可以对矩阵节点使用以下样式:
\matrix (M) [matrix of nodes, nodes in empty cells,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes={draw, minimum width=10mm, inner sep=1pt,
text height=1.2ex, % <---
font=\ttfamily\small, anchor=center},
column 1/.style={nodes={draw=none, minimum width=20mm}},
]
如果单元格内容包含字母 b、d、j、g,则可能的设置为:
\documentclass[a4paper,openany,14pt]{extbook}%extbook
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (M) [matrix of nodes, nodes in empty cells,
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,
nodes={draw, minimum width=10mm, inner sep=2pt, % or 1pt
minimum height=2.4ex,
font=\ttfamily\small, anchor=center},
column 1/.style={nodes={draw=none, minimum width=20mm}},
]
{ 1321 & 2233 & bdjg\\
1322 & 3111 & \\
1323 & 4010 & \\
};
\end{tikzpicture}
\end{document}
答案3
你真的需要 tikz 吗?如果不需要,下面这个应该可以解决你的问题:
\documentclass[a4paper,openany,14pt]{extbook}%extbook
\begin{document}
\begin{tabular}{p{1.5cm}|c|p{1cm}|}
\cline{2-3}
1321 & 2233 & \\
\cline{2-3}
1322 & 3111 & \\
\cline{2-3}
1323 & 4010 & \\
\cline{2-3}
\end{tabular}
\end{document}