Einfachere oder bessere Möglichkeit, ein 1D-Array zu zeichnen

Einfachere oder bessere Möglichkeit, ein 1D-Array zu zeichnen

Ich versuche, die folgende Abbildung zu reproduzieren, und frage mich, ob es einen einfacheren Weg gibt.Bildbeschreibung hier eingeben

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\leftbracket{[}
\def\rightbracket{]}

\tikzset{my arrow/.style={
  blue!60!black,
  -latex
  }
}
\begin{document}

\begin{tikzpicture}
\matrix [matrix of nodes,column sep=5.6mm, nodes in empty cells
] (P)
{
& t0 & t1 & t2 & t3 & t4 & t5 \\
};
\end{tikzpicture}


\begin{tikzpicture}
\matrix [matrix of nodes,row sep=,row sep=0mm,
column 2/.style={nodes={rectangle,draw,minimum width=3em}},
column 3/.style={nodes={rectangle,draw,minimum width=3em}},
column 4/.style={nodes={rectangle,draw,minimum width=3em}},
column 5/.style={nodes={rectangle,draw,minimum width=3em}},
column 6/.style={nodes={rectangle,draw,minimum width=3em}},
column 7/.style={nodes={rectangle,draw,minimum width=3em}}
] (O)
{
v1 & 0 & 1 & 2 & 3 & 4 & 5\\
};


\end{tikzpicture}

\begin{tikzpicture}
\matrix [matrix of nodes,column sep=5.6mm, nodes in empty cells
] (P)
{
& + & + & + & + & + & +\\
};
\end{tikzpicture}

\begin{tikzpicture}
\matrix [matrix of nodes,row sep=,row sep=8mm,
column 2/.style={nodes={rectangle,draw,minimum width=3em}},
column 3/.style={nodes={rectangle,draw,minimum width=3em}},
column 4/.style={nodes={rectangle,draw,minimum width=3em}},
column 5/.style={nodes={rectangle,draw,minimum width=3em}},
column 6/.style={nodes={rectangle,draw,minimum width=3em}},
column 7/.style={nodes={rectangle,draw,minimum width=3em}}
] (O)
{
v2 & 0 & 1 & 2 & 3 & 4 & 5\\
v1 & 0 & 2 & 4 & 6 & 8 & 10\\
};

\draw[my arrow] (O-1-2) to (O-2-2);
\draw[my arrow] (O-1-3) to (O-2-3);
\draw[my arrow] (O-1-4) to (O-2-4);
\draw[my arrow] (O-1-5) to (O-2-5);
\draw[my arrow] (O-1-6) to (O-2-6);
\draw[my arrow] (O-1-7) to (O-2-7);

\end{tikzpicture}

\end{document}

Antwort1

Etwas wie das?

\documentclass[tikz, border=5mm]{standalone}
\begin{document}
 \begin{tikzpicture}
  \foreach \x in {0,...,5} {
   \node at (\x, 0) {t\x};
   \node at (\x, -1.5) {+};
   \foreach \y\lbl in {1/1,2/2,3/1} {
    \node [left] at (-1,-\y) {v\lbl};
    \ifnum\y=3
     \pgfmathtruncatemacro{\result}{2*\x}
     \node (n\x\y) [draw, minimum width=1cm] at (\x, -\y) {\result};
     \draw [->] (n\x2) -- (n\x\y);
    \else
     \node (n\x\y) [draw, minimum width=1cm] at (\x, -\y) {\x};
    \fi
   }
  }
 \end{tikzpicture}
\end{document}

Gerendertes Bild: Bildbeschreibung hier eingeben

Antwort2

Hier ist eine Möglichkeit mit Stapeln anstelle von tikz.

\documentclass{article}
\usepackage{stackengine}
\def\mbx#1{\fbox{\makebox[1.5cm]{#1}}}
\def\xdownarrow{\raisebox{-1.9ex}{%
  \stackengine{0pt}{$\downarrow$}{\rule{.5pt}{4ex}\kern.1pt}{O}{c}{F}{F}{S}}}
\def\boxcol#1#2{\Longstack{%
  t#1\\\mbx{#1}\\$+$\\\mbx{#1}\\\xdownarrow\\\mbx{#2}}\kern-\fboxrule}
\setstackEOL{\\}
\renewcommand\stacktype{L}
\setstackgap{L}{2em}
\begin{document}
\Longstack{\\v1\\\\v2\\\\v1}
\boxcol{0}{0}%
\boxcol{1}{2}%
\boxcol{2}{4}%
\boxcol{3}{6}%
\boxcol{4}{8}%
\boxcol{5}{10}%
\end{document}

Bildbeschreibung hier eingeben

Antwort3

Wenn Sie bei Ihrer Matrixlösung bleiben möchten, können Sie einen allgemeinen Setter für Spalten und Zeilen erstellen. Die Ausführungsreihenfolge für jeden Zellstilschlüssel ist im Handbuch angegeben. Sie können also die Spalteneigenschaften festlegen und dann unerwünschte Eigenschaften in den Zeileneinstellungen rückgängig machen (zumindest in diesem Fall).

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}

\tikzset{my arrow/.style={blue!60!black,-latex},
  set@com@col/.style={},set@com@col@aryarg/.style={column #1/.style={set@com@col}},
  set@com@row/.style={},set@com@row@aryarg/.style={row #1/.style={set@com@row}},
  set common column/.style 2 args={set@com@col/.style={#2}, set@com@col@aryarg/.list={#1}},
  set common row/.style 2 args={set@com@row/.style={#2}, set@com@row@aryarg/.list={#1}},
}

\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes,row sep=0mm,
set common column={2,...,7}{nodes={rectangle,draw,minimum width=3em}},
set common row={1,3}{nodes={draw=none}},
] (O)
{
& t0 & t1 & t2 & t3 & t4 & t5 \\
v1 & 0 & 1 & 2 & 3 & 4 & 5\\
& + & + & + & + & + & +\\
v2 & 0 & 1 & 2 & 3 & 4 & 5\\[8mm]
v1 & 0 & 2 & 4 & 6 & 8 & 10\\
};

\foreach\x in{2,...,7}{\draw[my arrow] (O-4-\x) to (O-5-\x);}
\end{tikzpicture}
\end{document}

Bildbeschreibung hier eingeben

Antwort4

\foreachUm die einzelnen Spalten Deines Bildes zu platzieren, kannst Du entweder eine Schleife verwenden :

Mit Schlaufe

Dies funktioniert jedoch nicht, wenn die Höhe der einzelnen Felder variieren kann. In diesem Fall können Sie ein verwenden \matrix[matrix of nodes]und den Inhalt manuell eingeben:

Mit Matrix

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{matrix}

\tikzset{mybox/.style={draw, minimum width=1cm}}

\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,5} {
    \node at (\i,0) (t\i) {t\i};
    \node[mybox, below=0.3cm of t\i.center] (top\i) {\i};
    \node[mybox, below=1cm of top\i.center] (mid\i) {\i};
    \node[mybox, below=1cm of mid\i.center] (bot\i) {\pgfmathparse{int(2*\i)}\pgfmathresult}
        edge[<-] (mid\i);
    \node at ($(top\i)!0.5!(mid\i)$) (plus\i) {+};
}
\path (top0)[late options={label=left:v1}]
    (mid0)[late options={label=left:v2}]
    (bot0)[late options={label=left:v1}];
\end{tikzpicture}

\noindent
\begin{tikzpicture}
\matrix (matrix) [
    matrix of nodes,
    row sep={0.8cm,between origins}, column sep=-\pgflinewidth,
    row 2/.style={every node/.append style={mybox}},
    row 4/.style={every node/.append style={mybox}},
    row 5/.style={every node/.append style={mybox}},
]{
                    & t0 & t1 & t2 & t3 & t4 & t5\\
    |[draw=none]|v1 & 0  & 1  & 2  & 3  & 4  & 5 \\
                    & +  & +  & +  & +  & +  & + \\
    |[draw=none]|v2 & 0  & 1  & 2  & 3  & 4  & 5 \\[0.8cm]
    |[draw=none]|v1 & 0  & 2  & 4  & 6  & 8  & 10\\
};
\foreach \i in {2,...,7} {
    \draw[->] (matrix-4-\i) -- (matrix-5-\i); 
}
\end{tikzpicture}
\end{document}

verwandte Informationen