矩阵的边界位置不太好

矩阵的边界位置不太好

我有以下代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\colorlet{mlightgray}{gray!20}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center},
  column 1/.style = {nodes={cell, minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={cell, minimum width=2cm}},
  column 3/.style = {nodes={cell, minimum width=2cm}},
  row 1/.style={nodes={cell, minimum height=0.5, fill=mlightgray}},
  row 2/.style={nodes={cell, minimum height=0.5}},
  row 3/.style={nodes={cell, minimum height=0.5}},
  row 4/.style={nodes={cell, minimum height=0.5}},
  ] 
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

它产生这个矩阵:

在此处输入图片描述

似乎我已经使用了其他线程中提到的所有解决方案:pgflinewidth并且nodes={anchor=center}......但边界仍然不重叠......

不明白为什么图书馆让重叠变得如此困难,这是一个很明显的需求...有人可以帮忙吗?

另外,我只想始终使第一行和第一列具有mlightgray背景颜色,有没有办法避免重复row .../.style={nodes={cell, minimum height=0.5}}多次?

此外,与行不同,当我尝试将minimum width=2(而不是2cm)用作列时,效果并不好。为什么我必须为该宽度指定单位?

答案1

您可以使用text heighttext depth键以便所有节点具有相同的大小。

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\colorlet{mlightgray}{gray!20}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex},
  column 1/.style = {nodes={cell, minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={cell, minimum width=2cm}},
  column 3/.style = {nodes={cell, minimum width=2cm}},
  row 1/.style={nodes={cell, minimum height=0.5, fill=mlightgray}},
  row 2/.style={nodes={cell, minimum height=0.5}},
  row 3/.style={nodes={cell, minimum height=0.5}},
  row 4/.style={nodes={cell, minimum height=0.5}},
  ] 
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

代码还可以进一步简化:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\colorlet{mlightgray}{gray!20}
\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex,cell},
  column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={minimum width=2cm}},
  column 3/.style = {nodes={minimum width=2cm}},
  row 1/.style={nodes={fill=mlightgray}},
  ] 
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

答案2

您还可以使用

execute at begin cell=\strut,
execute at empty cell={\node{\strut};},

并删除nodes in empty cells。此外,通过将节点定义为

nodes={cell,anchor=center,minimum width=2cm},

您可以进一步简化。请注意,当您使用minimum width它时需要一个长度参数,因此您必须指定单位,例如1cm1in等,而不是简单地指定1

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\colorlet{mlightgray}{gray!20}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw}]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  execute at begin cell=\strut,
  execute at empty cell={\node{\strut};},
  nodes={cell,anchor=center,minimum width=2cm},
  column 1/.style = {nodes={cell, minimum width=1cm, fill=mlightgray}},
%  column 2/.style = {nodes={cell, minimum width=2cm}},
%  column 3/.style = {nodes={cell, minimum width=2cm}},
  row 1/.style={nodes={cell, fill=mlightgray}},
  %row 2/.style={nodes={cell, minimum height=0.5}},
%  row 3/.style={nodes={cell, minimum height=0.5}},
%  row 4/.style={nodes={cell, minimum height=0.5}},
  ]
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容