我有以下代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
\matrix[
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, minimum width=2cm, cell},
column 1/.style = {nodes={minimum width=2cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
row 5/.style = {text height=1.3ex, text depth=0},
row 6/.style = {text height=1.3ex, text depth=0},
] (m)
{ \\
\\
\\
i \mapsto 5 \\
\\
\\
};
\end{tikzpicture}
它产生这个矩阵:
有人知道如何移除所有内行分隔符(或给它们上色white
以不改变高度)吗?我只想让文本被大矩形包围。
编辑1:
我按照第一条评论的第一个解决方案(draw
从cell
样式选项中删除并将其用于\matrix
),并尝试通过更改为来用颜色填充它column 1/.style = {nodes={minimum width=2cm, fill=purple}},
,它显示以下内容。但是,我希望背景颜色填充整个矩形。
我坚持使用矩阵的原因是因为这个 tikz 图片将与其他具有矩阵(6 行,height=1.3ex
)的图片对齐。我希望它们具有相同的高度。这就是为什么我认为将行分隔符的颜色更改为white
可能是一个很好的解决方案...
编辑2:
我认为我需要更清楚地说明我的需求...这是生成两个并排矩阵的代码。左侧的矩阵是参考矩阵;右侧的矩阵遵循第一条注释的解决方案。我只想让它们具有相同的高度:一个有内行分隔,另一个没有内行分隔,并用紫色填充。
\begin{frame}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
\matrix[
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, minimum width=2cm, cell},
column 1/.style = {nodes={minimum width=1.5cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
row 5/.style = {text height=1.3ex, text depth=0},
] (m)
{ \\ \\ i \mapsto 5 \\ \\ \\ };
\end{tikzpicture}
\begin{tikzpicture}[cell/.style={rectangle}, nodes in empty cells]
\matrix[
matrix,
fill=purple,
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, minimum width=2cm, cell},
column 1/.style = {nodes={minimum width=1.5cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
row 5/.style = {text height=1.3ex, text depth=0},
] (m)
{ \\ \\ i \mapsto 5 \\ \\ \\ };
\end{tikzpicture}
\end{frame}
答案1
您可以draw
从cell
样式选项中删除它,并将其用于\matrix
(见下文);但是,使用它似乎有点小题大做,因为一个简单的方法\node
可以用更少的努力完成同样的工作,如下所示:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle}, nodes in empty cells]
\matrix[
draw,
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center, minimum width=2cm, cell},
column 1/.style = {nodes={minimum width=2cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
row 5/.style = {text height=1.3ex, text depth=0},
row 6/.style = {text height=1.3ex, text depth=0},
] (m)
{ \\
\\
\\
i \mapsto 5 \\
\\
\\
};
\end{tikzpicture}
\begin{tikzpicture}[cell/.style={rectangle}, nodes in empty cells]
\node[
draw,
text width=2cm,minimum height=3cm,align=center] (m)
{$i \mapsto 5$};
\end{tikzpicture}
\end{document}
更新:
杀死但保留里面的节点inner sep
:\matrix
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,matrix}
\colorlet{mycolor}{gray!10}
\begin{document}
\begin{frame}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells,baseline]
\matrix[
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
nodes={anchor=center,cell},
column 1/.style = {nodes={minimum width=1.5cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
row 5/.style = {text height=1.3ex, text depth=0},
] (m)
{ \\ \\ i \mapsto 5 \\ \\ \\ };
\end{tikzpicture}
\begin{tikzpicture}[cell/.style={rectangle,inner sep=0.3333em,draw},nodes in empty cells,baseline]
\matrix[
matrix,
nodes={anchor=center, minimum width=2cm, cell},
inner sep=0em,
fill=purple,
matrix of math nodes,
row sep =-\pgflinewidth,
column sep = -\pgflinewidth,
column 1/.style = {nodes={minimum width=1.5cm}},
row 1/.style = {nodes={text height=1.3ex, text depth=0}},
row 2/.style = {text height=1.3ex, text depth=0},
row 3/.style = {text height=1.3ex, text depth=0},
row 4/.style = {text height=1.3ex, text depth=0},
row 5/.style = {text height=1.3ex, text depth=0},
] (m)
{ \\ \\ i \mapsto 5 \\ \\ \\ };
\end{tikzpicture}
\end{frame}
\end{document}