元素长度不均匀的矩阵

元素长度不均匀的矩阵

从答案到这个问题

当编辑矩阵元素使其长度不均匀时,矩阵的显示会变得混乱。如何解决这个问题

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{arrows, arrows.meta, positioning, calc, intersections, decorations.pathreplacing, decorations.pathmorphing, decorations.markings, shapes, shapes.misc, shapes.symbols, shapes.geometric, patterns, backgrounds, trees, shadows, fit,matrix}
\begin{document}
\begin{frame}[t,fragile]
\frametitle{matrix}
\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\\
17.4 & 18.6 & 12.9 & 20.3 & 13.7 & 21.4\\
10.3 & 26.1 & 15.718.918.9 & 14.0 & 17.8 & 33.8\\
23.2 & 12.9 & 29.8 & 18.3 & 14.2 & 20.8\\
13.5 & 17.1 & 20.7 & 27.1 & 18.918.9 & 16.6\\};
\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>)
\foreach \i in {1,...,6} 
\draw ($(x-1-\i.north west)+(-5pt,5pt)$) -- ($(x-5-\i.south west)+(-5pt,-5pt)$);%vertical lines
\foreach \i in {1,...,5} 
\draw ($(x-\i-1.south west)+(-5pt,-5pt)$) -- ($(x-\i-6.south east)+(5pt,-5pt)$);%horizontal lines
\draw ($(x-1-1.north west)+(-5pt,5pt)$) -| ($(x-5-6.south east)+(5pt,-5pt)$);%upper and right borders
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

可以使用该fit库始终在最高/最宽的矩阵条目之间绘制线条。我添加了一个样式full matrix grid,可以用作

\draw[matrix name=x,matrix cols=6,matrix rows=5,full matrix grid];

如果您只想绘制垂直线或水平线的子集,则可以使用重新定义的vlineshlines样式,现在也可以使用fit

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,fit,matrix}
\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)}},
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)}},
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{document}
\begin{frame}[t,fragile]
\frametitle{matrix}
\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\\
17.4 & 18.6 & 12.9 & 20.3 & 13.7 & 21.4\\
10.3 & 26.1 & 15.718.918.9 & 14.0 & 17.8 & 33.8\\
23.2 & 12.9 & 29.8 & 18.3 & 14.2 & 20.8\\
13.5 & 17.1 & 20.7 & 27.1 & 18.918.9 & 16.6\\};
\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]; % 

\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

PS 外线draw也可以用 画出来。

答案2

{NiceTabular}这是使用nicematrix和 TikZ 绘制图形元素的解决方案。

彩色圆圈绘制在数组元素之前,箭头绘制在数组元素之后(不使用透明度)。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{frame}[t,fragile]
\frametitle{matrix}

\setlength{\tabcolsep}{10pt}
\renewcommand{\arraystretch}{2}
\setlength{\extrarowheight}{-2pt}

\begin{NiceTabular}{cccccc}[hvlines]
\CodeBefore [create-cell-nodes]
  \begin{tikzpicture} [every node/.style = fill,circle,inner sep=8pt]
     \node<2-> at (2-1) [red!20] { }  ;
     \node<2-> at (4-5) [blue!20] { } ;
  \end{tikzpicture}
\Body
  15.1 & 23.7 & 19.7 & 15.4 & 18.3 & 23.0 \\
  17.4 & 18.6 & 12.9 & 20.3 & 13.7 & 21.4 \\
  10.3 & 26.1 & 15.718.918.9 & 14.0 & 17.8 & 33.8 \\
  23.2 & 12.9 & 29.8 & 18.3 & 14.2 & 20.8  \\
  13.5 & 17.1 & 20.7 & 27.1 & 18.918.9 & 16.6 \\
\CodeAfter 
  \tikz \draw<2-> [ultra thick, blue , <-] (2-1) -- (4-5) ;
\end{NiceTabular}

\end{frame}

\end{document}

您需要多次编译。

上述代码的输出

相关内容