1차원 배열을 그리는 더 쉽고 더 나은 방법

1차원 배열을 그리는 더 쉽고 더 나은 방법

다음 그림을 재현하려고 하는데 더 간단한 방법이 있는지 궁금합니다.여기에 이미지 설명을 입력하세요

\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}

답변1

이 같은?

\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}

렌더링된 이미지: 여기에 이미지 설명을 입력하세요

답변2

. 대신 스택을 사용하는 방법이 있습니다 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}

여기에 이미지 설명을 입력하세요

답변3

행렬 솔루션을 고수하려면 열과 행에 대한 일반 설정기를 만들 수 있습니다. 각 셀 스타일 키의 실행 순서는 매뉴얼에 나와 있으므로 열 속성을 설정한 다음 행 설정에서 원하지 않는 속성을 실행 취소할 수 있습니다(적어도 이 경우).

\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}

여기에 이미지 설명을 입력하세요

답변4

\foreach루프를 사용하여 그림의 개별 열을 배치 할 수 있습니다 .

루프 포함

그러나 개별 상자의 높이가 다를 수 있는 경우 이는 깨질 수 있습니다. 이 경우 a를 사용하여 \matrix[matrix of nodes]콘텐츠를 수동으로 입력할 수 있습니다.

매트릭스 포함

\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}

관련 정보