从答案到这个问题
使用以下代码
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{arrows, calc, fit, matrix}
\begin{document}
\tikzset{matrix rows/.initial=5,matrix cols/.initial=6,matrix name/.initial=x,
vline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix rows}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\fi},
insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {}
($(fitA.east)!0.5!(fitB.west)$) coordinate (aux)
(\pgfkeysvalueof{/tikz/matrix name}.north-|aux) -- (\pgfkeysvalueof{/tikz/matrix name}.south-|aux)}},%end vline
hline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix cols}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\fi},insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {}
($(fitA.south)!0.5!(fitB.north)$) coordinate (aux)
(\pgfkeysvalueof{/tikz/matrix name}.west|-aux) -- (\pgfkeysvalueof{/tikz/matrix name}.east|-aux)}},%end hline
full matrix grid/.style={vline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1},
hline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix rows}-1},
insert path={(\pgfkeysvalueof{/tikz/matrix name}.south west) rectangle (\pgfkeysvalueof{/tikz/matrix name}.north east)}}
}
\begin{frame}[t,fragile]
\frametitle{}
\begin{tikzpicture}
\matrix (x) [matrix of nodes, row sep=10pt, column sep=10pt] {%
15.1 & 23.7 & 19.7 & 15.4 & 18.3 & 23.0 & y\\
17.4 & 18.6 & 12.9 & 20.320.320.3 & 13.7 & 21.45 & y\\
10.3 & 26.1 & 15.718.918.9 & 14.0 & 17.8 & 33.8 & y\\
23.2 & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & y\\
xx & xx & xx & xx & xx & xx & y\\
13.5 & 17.1 & 20.7 & 27.1 & 18.918.9 & 16.6 & y\\};
\draw<2->[ultra thick, blue, latex'-] (x-2-1) node [fill, red!40!white, circle, inner sep=8pt, opacity=.4]{} -- (x-4-5) node [fill, blue!40!white, circle, inner sep=8pt, opacity=.4]{};
% Node names: (<name of matrix>-<row>-<column>)
% \draw[vline/.list={1,...,5},hline/.list={1,...,4}] (\pgfkeysvalueof{/tikz/matrix name}.north west) rectangle
% (\pgfkeysvalueof{/tikz/matrix name}.south east); %
\draw[matrix name=x,matrix cols=6,matrix rows=5,full matrix grid];% drawing the borders
\end{tikzpicture}
\end{frame}
\end{document}
添加新行/列时,新行/列与旧行/列之间没有线。如何修复此问题?
我尝试重新调整
matrix rows/.initial=5,matrix cols/.initial=6
到
matrix rows/.initial=0,matrix cols/.initial=0
或者
matrix rows/.initial=10,matrix cols/.initial=10
但它没有作用。
从我得到的答案来看,问题不在于这条线路;问题在于我没有重新调整线路
\draw[matrix name=x,matrix cols=6,matrix rows=5,full matrix grid];
到
\draw[matrix name=x,matrix cols=7,matrix rows=6,full matrix grid];
答案1
您需要更改此行:
\draw[matrix name=x,matrix cols=6,matrix rows=5,full matrix grid];
到
\draw[matrix name=x,matrix cols=7,matrix rows=6,full matrix grid];
这一行代码的作用是绘制一个具有指定行数和列数的网格。在本例中,代码如下:
完整代码如下:
documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{arrows, calc, fit, matrix}
\begin{document}
\tikzset{matrix rows/.initial=5,matrix cols/.initial=6,matrix name/.initial=x,
vline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix rows}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\fi},
insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {}
($(fitA.east)!0.5!(fitB.west)$) coordinate (aux)
(\pgfkeysvalueof{/tikz/matrix name}.north-|aux) -- (\pgfkeysvalueof{/tikz/matrix name}.south-|aux)}},%end vline
hline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix cols}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\fi},insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {}
($(fitA.south)!0.5!(fitB.north)$) coordinate (aux)
(\pgfkeysvalueof{/tikz/matrix name}.west|-aux) -- (\pgfkeysvalueof{/tikz/matrix name}.east|-aux)}},%end hline
full matrix grid/.style={vline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1},
hline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix rows}-1},
insert path={(\pgfkeysvalueof{/tikz/matrix name}.south west) rectangle (\pgfkeysvalueof{/tikz/matrix name}.north east)}}
}
\begin{frame}[t,fragile]
\frametitle{}
\begin{tikzpicture}
\matrix (x) [matrix of nodes, row sep=10pt, column sep=10pt] {%
15.1 & 23.7 & 19.7 & 15.4 & 18.3 & 23.0 & y\\
17.4 & 18.6 & 12.9 & 20.320.320.3 & 13.7 & 21.45 & y\\
10.3 & 26.1 & 15.718.918.9 & 14.0 & 17.8 & 33.8 & y\\
23.2 & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & y\\
xx & xx & xx & xx & xx & xx & y\\
13.5 & 17.1 & 20.7 & 27.1 & 18.918.9 & 16.6 & y\\};
\draw<2->[ultra thick, blue, latex'-] (x-2-1) node [fill, red!40!white, circle, inner sep=8pt, opacity=.4]{} -- (x-4-5) node [fill, blue!40!white, circle, inner sep=8pt, opacity=.4]{};
% Node names: (<name of matrix>-<row>-<column>)
% \draw[vline/.list={1,...,5},hline/.list={1,...,4}] (\pgfkeysvalueof{/tikz/matrix name}.north west) rectangle
% (\pgfkeysvalueof{/tikz/matrix name}.south east); %
\draw[matrix name=x,matrix cols=7,matrix rows=6,full matrix grid];% drawing the borders
\end{tikzpicture}
\end{frame}
\end{document}
答案2
Andrew 指出了正确的事情。现在让我深入研究一下。这是\tikzset
, 对齐(我最喜欢的方式)
\tikzset{
matrix rows/.initial=5,
matrix cols/.initial=6,
matrix name/.initial=x,
vline/.style={
/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix rows}} {
\ifnum\XX=1
\xdef\MatLstA{
(\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{
(\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\else
\xdef\MatLstA{
\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{
\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\fi},
insert path={
node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {} ($(fitA.east)!0.5!(fitB.west)$) coordinate (aux)
(\pgfkeysvalueof{/tikz/matrix name}.north-|aux) -- (\pgfkeysvalueof{/tikz/matrix name}.south-|aux)}
},%end vline
hline/.style={
/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix cols}} {
\ifnum\XX=1
\xdef\MatLstA{
(\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{
(\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\else
\xdef\MatLstA{
\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{
\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\fi},
insert path={
node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {} ($(fitA.south)!0.5!(fitB.north)$) coordinate (aux)
(\pgfkeysvalueof{/tikz/matrix name}.west|-aux) -- (\pgfkeysvalueof{/tikz/matrix name}.east|-aux)}
},%end hline
full matrix grid/.style={
vline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1},
hline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix rows}-1},
insert path={(\pgfkeysvalueof{/tikz/matrix name}.south west) rectangle (\pgfkeysvalueof{/tikz/matrix name}.north east)}
}
}
在没有任何事先知识的情况下,我想我们仍然可以理解vline
样式会在给定的列表中添加垂直线,以及hline
水平线。
现在,看看full matrix grid
:
full matrix grid/.style={
vline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1},
hline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix rows}-1},
insert path={(\pgfkeysvalueof{/tikz/matrix name}.south west) rectangle (\pgfkeysvalueof{/tikz/matrix name}.north east)}
}
它输出给定列表中的一组hline
s 和vline
s。列表由matrix cols
和matrix rows
(\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1
等)控制,因此您必须更改这些选项的值才能更改网格。
matrix rows
和的初始值为matrix cols
和5
,6
但这并不重要,因为你已经在命令中再次重置了它
\draw[matrix name=x,matrix cols=6,matrix rows=5,full matrix grid];
现在,改变上面命令中的matrix cols
和的值,您将得到您想要的结果。matrix rows
为了完整起见,请参阅 Andrew 的回答中的可编译代码。