实现内部带有箭头的复杂矩阵对象(LaTeX)

实现内部带有箭头的复杂矩阵对象(LaTeX)

我想在 Latex 中绘制一个类似的对象,但我认为这对于我的知识来说非常复杂,我不知道从哪里开始。我想从矩阵开始,但我没有找到定义水平和垂直线或插入复杂图形(如下面的箭头)的具体方法。如果没有办法做到这一点,我应该直接使用它作为图片。

在此处输入图片描述

答案1

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,positioning, backgrounds, arrows.meta}

\begin{document}
\begin{tikzpicture}

\matrix[matrix of math nodes, column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={anchor=center, minimum width=8mm, minimum height=6mm, fill=gray!30, draw},
    ] (Top)
    {x_0& ~&~ &~ &g_1&~ &~ &~ & g_2&~ & ~& ~& g_3\\
    & \theta_{t_0}& \theta_{t_1}& \theta_{t_2} & & \theta_{t_0}& \theta_{t_1}& \theta_{t_2} & & \theta_{t_0}& \theta_{t_1}& \theta_{t_2}\\};
    \foreach \i in {1,2,3,4,6,7,8,10,11,12}
    \draw[gray!30] ([shift={(-.5\pgflinewidth,-\pgflinewidth)}]Top-1-\i.north east)--([shift={(-.5\pgflinewidth,\pgflinewidth)}]Top-1-\i.south east);

\matrix[matrix of math nodes, column sep=-\pgflinewidth, 
    nodes={anchor=center, minimum width=8mm, minimum height=6mm, fill=gray!30, draw},
    below left=2cm and 0pt of Top.south east] (Bottom)
    {r_{t_0}& r_{t_1}& r_{t_2}& \phi_{t_3} &r_{t_0}& r_{t_1}& r_{t_2}& \phi_{t_3}&r_{t_0}& r_{t_1}& r_{t_2}& \phi_{t_3}\\};


\foreach \i [count=\ni from 0] in {2,3,4}
{
    \draw[pink, opacity=.5, line width=1pt, shorten > = 1mm] ([yshift=-1.5cm+\ni*1mm]Top-2-\i.south west) coordinate (aux1) -- (aux1-|Top-1-5.east);
    \draw[-{Latex}, pink, line width=1.5pt, shorten >=2mm] (aux1-|Top-2-\i) coordinate (aux2) -- (Top-2-\i.south);
    \path (Bottom.north-|Top-2-\i.south)--(Top-2-\i.south) node[pos=.6, font=\scriptsize] {$S(\tau_\ni)$};
    }

\foreach \i [count=\ni from 0] in {6,7,8}
{
    \draw[pink, opacity=.5, line width=1pt, shorten > = 1mm] ([yshift=-1.5cm+\ni*1mm]Top-2-\i.south west) coordinate (aux1) -- (aux1-|Top-1-9.east);
    \draw[-{Latex}, pink, line width=1.5pt, shorten >=2mm] (aux1-|Top-2-\i) coordinate (aux2) -- (Top-2-\i.south);
    \path (Bottom.north-|Top-2-\i.south)--(Top-2-\i.south) node[pos=.6, font=\scriptsize] {$S(\tau_\ni)$};
    }

\foreach \i [count=\ni from 0] in {10,11,12}
{
    \draw[pink, opacity=.5, line width=1pt, shorten > = 1mm] ([yshift=-1.5cm+\ni*1mm]Top-2-\i.south west) coordinate (aux1) -- (aux1-|Top-1-13.east);
    \draw[-{Latex}, pink, line width=1.5pt, shorten >=2mm] (aux1-|Top-2-\i) coordinate (aux2) -- (Top-2-\i.south);
    \path (Bottom.north-|Top-2-\i.south)--(Top-2-\i.south) node[pos=.6, font=\scriptsize] {$S(\tau_\ni)$};
    }


\foreach \i/\j  [count=\ni] in {1/4, 5/8,9/12}
{
    \draw[green, line width=1pt] ([yshift=2mm+\ni*1mm]Bottom-1-\i.north west) coordinate (aux1) -- (aux1-|Bottom-1-12.east);
    \draw[-{Latex}, green, line width=2pt, shorten >=2mm] (aux1-|Bottom-1-\j) coordinate (aux2) -- (Top-1-1.south-|Bottom-1-\j) coordinate (aux3);
    \path (Bottom-1-\j)--(aux3) node[pos=.65, font=\small] {$S(\Pi_\ni)$};
    }
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是另一种方法,使用\pics。我制作了两个\pics,一个用于灰色矩形,另一个用于箭头。其余部分是为了避免使用几个\foreach命令重复代码。

这是我的建议:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\tikzset
{%
  pics/rectangle/.style 2 args={% width, text
    code={%
      \draw[fill=gray!10] (0,0) rectangle (#1,1);
      \node at (#1-0.5,0.5) {\strut #2};
    }},
  pics/arrow/.style n args={5}{% width, height, vertical arrow postion, text, text position
    code={%
      \draw[thick] (0,0) -- (#1,0);
      \draw[very thick,-latex] (#3,0) -- (#3,#2);
      \node[black] at (#3,#5) {$S(#4)$};
    }}
}

\begin{document}
\begin{tikzpicture}
%\draw[help lines] (-1,0) grid (12,6);
\foreach\i in {1,2,3}
{%
  \ifnum\i = 1
    \pic at (-1,5)        {rectangle={5}{$g_1$}};
  \else
    \pic at (4*\i-4,5)    {rectangle={4}{$g_\i$}};
  \fi
  \pic   at (4*\i-1,0)    {rectangle={1}{$\phi_{t_3}$}};
  \foreach\j in {0,1,2}
  {%
    \pic at (4*\i-4+\j,4) {rectangle={1}{$\theta_{t_\j}$}};
    \pic at (4*\i-4+\j,0) {rectangle={1}{$r_{t_\j}$}};
    \pic[pink] at (4*\i-4+\j,2.25+0.25*\j) {arrow={3.9-\j}{1.5-0.25*\j}{0.5}{\tau_\j}{1-0.25*\j}};
  }
  \pic[green] at (4*\i-4,1+0.25*\i)        {arrow={16-4*\i}{3.5-0.25*\i}{3.5}{\Pi_\i}{2.75-0.25*\i}};
}
\node at (-0.5,5.5) {$\mathstrut x_0$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容