我正在尝试重现下图,并想知道是否有更简单的方法。
\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
循环来放置图片的各个列:
但是,如果各个框的高度不同,这种方法就会失效。在这种情况下,你可以使用\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}