使用 Tikz 在 LaTeX 中绘制数组聚合

使用 Tikz 在 LaTeX 中绘制数组聚合

抱歉,我刚刚开始使用 LaTeX。

我想显示具有 n 个元素的 2 个数组的元素总和,这是我期望的输出:

数组聚合

我搜索了很多,但没有找到任何相关结果。有人可以帮我完成这个形状吗?

答案1

除了有点不必要的简洁之外,并没有真正增加其他答案的太多内容:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth, every node/.style={rectangle, draw,
  minimum height=1.5em, minimum width=2em, outer sep=0}]
\path (0,0.5) node [draw=none] (p-0) {$P=$} (0,-.5) 
  node [draw=none] (q-0) {$Q=$} (4.5,0) coordinate (p+q-0);
\foreach \s [count=\i from 1, count=\j from 0] in {1,2,\ldots,N}
  \foreach \t [count=\k] in {p, q, p+q}
    \node [anchor=west] (\t-\i) at (\t-\j.east) 
      {$\ifnum\i=3\ldots\else\ifnum\k=3p_{\s}+q_{\s}\else\t_{\s}\fi\fi$};
\draw [->] [gray] (p-4) .. controls +(1,0) and +(-1,0) .. (p+q-0);
\draw [->] [gray] (q-4) .. controls +(1,0) and +(-1,0) .. (p+q-0);
\end{tikzpicture}    
\end{document}

在此处输入图片描述

答案2

pstricks我提出一个具有普通环境的简单代码alignat:我在两个左数组后分别定义空节点,在右数组前定义两个节点,并用曲线+直线连接它们:

\documentclass[svgnames]{article}
\usepackage{array, mathtools}
\usepackage{pst-node}
\usepackage{auto-pst-pdf} % to compile with pdflatex

\begin{document}

\begin{postscript}
  \begin{alignat*}{2} P & = \begin{array}{|*{4}{c|}}
      \hline
      p_1 & p_2 & \dots & p_N \\[0.4ex]
      \hline
    \end{array}\pnode[0.4ex,0.5ex]{P} \\[-1ex]
          & & \hspace{3em}\pnode[0,0.5ex]{A}\quad\pnode[-0.4ex,0.5ex]{B} & \begin{array}{|*{4}{c|}}
      \hline
      p_1 + q_1 & p_2 + q_2 & \dots & p_N + q_N \\[0.4ex]
      \hline
    \end{array} \\[-1ex]
    Q & = \begin{array}{|*{4}{c|}}
      \hline
      q_1 & q_2 & \dots & q_N \\[0.4ex]
      \hline
    \end{array}\pnode[0.4ex,0.5ex]{Q}
  \end{alignat*}
  \psset{angleB=180, linecolor=LightSteelBlue}
  \nccurve{P}{A}\nccurve{Q}{A}\ncline[arrows=->, arrowinset=0.15]{A}{B}
\end{postscript}

\end{document} 

在此处输入图片描述

答案3

在此处输入图片描述

LaTeX \tabular一个简单的例子:用作Asymptote标签和Asymptote曲线和箭头的两个环境的组合:

// arrbox.asy
//
// run 
// asy arrbox.asy
// to get arrbox.pdf
//
settings.tex="pdflatex";
unitsize(1pt);
import fontsize;defaultpen(fontsize(9pt));
texpreamble("\usepackage{lmodern}");

Label L1=Label(
"\begin{tabular}{r|c|c|c|c|}" 
    +"\cline{2-5}" 
    +"$P=$ & $p_1$ & $p_2$ & $\cdots$ & $p_n$"  
    +"\\ \cline{2-5}" 
    +"\multicolumn{5}{c}{}\\" 
    +"\cline{2-5}" 
    +"$Q=$ & $q_1$ & $q_2$ & $\cdots$ & $q_n$"  
    +"\\ \cline{2-5}"
    +"\end{tabular}"
,(0,0)
,plain.W
);

pair p1max,p1min, p2max,p2min;

path[] p1=texpath(L1);

p1max=max(p1);
p1min=min(p1);
real h1=(p1max-p1min).y;

Label L2=Label(
"\begin{tabular}{|c|c|c|c|}" 
    +"\hline" 
    +"$p_1+q_1$ & $p_2+q_2$ & $\cdots$ & $p_n+q_n$" 
    +"\\"+'\n'+ "\hline" 
    +"\end{tabular}"
,p1max+(120,-h1/2)
);

path[] p2=texpath(Label(L2));

p2max=max(p2); p2min=min(p2);
real h2=(p2max-p2min).y;

label(L1); label(L2);

pair v1,v2,v3,v4;
v1=p1max-(0,1/6*h1);
v2=p1max-(0,5/6*h1);
v3=p2min+(0,h2/2);
v4=(v1+v2+6*v3)/8;

draw(v1{dir( 5)}..v4{dir(0)}--v3,Arrow);
draw(v2{dir(-5)}..v4{dir(0)}--v3       );

dot(v1,UnFill);
dot(v2,UnFill);
dot(v3,UnFill);

shipout(bbox(Fill(paleyellow)));

注意,在这种情况下文本部分可以被复制为普通文本。

答案4

编辑:修正了变量表示,改进了箭头设计……

在此处输入图片描述

tikz

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta, 
                bending,
                calc, chains,
                positioning
                }
\usepackage{amsmath}

\begin{document}
    \begin{tikzpicture}[
node distance = 5mm and 0mm,
  start chain = A going right,
   box/.style = {rectangle, draw, inner sep=1mm, outer sep=0mm,
                 minimum height=5mm, minimum width=8mm,
                 on chain=A},
    LA/.style = {-{Straight Barb[flex=0]},
                 thick, shorten >=1mm, shorten <=1mm,
                 looseness=1.6}
                        ]
\node [box, label=left:{$P=$}]  {$p_1$};        % A-1
\node [box]                     {$p_2$};
\node [box, densely dashed]     {};
\node [box]                     {$p_N$};        % A-4
%
\node [box, label=left:{$Q=$},
       below=of A-1]            {$q_1$};        % A-5
\node [box]                     {$q_2$};
\node [box, densely dashed]     {};
\node [box]                     {$q_N$};        % A-8
%
\node [box,right=12mm of $(A-4.east)!0.5!(A-8.east)$]
                                {$p_1 + q_1$};  % A-9
\node [box]                     {$p_2 + q_2$};
\node [box, densely dashed]     {};
\node [box]                     {$p_N + q_N$};  % A-12
%
\coordinate[left=3mm of A-9.west] (aux);
\draw[LA]   (A-4) to [out=0, in=180] (aux) -- (A-9);
\draw[LA]   (A-8) to [out=0, in=180] (aux) -- (A-9);
    \end{tikzpicture}
\end{document}

相关内容