![如何在 tikz 中绘制复杂节点?](https://linux22.com/image/253960/%E5%A6%82%E4%BD%95%E5%9C%A8%20tikz%20%E4%B8%AD%E7%BB%98%E5%88%B6%E5%A4%8D%E6%9D%82%E8%8A%82%E7%82%B9%EF%BC%9F.png)
我知道如何在 tikz 中绘制自动机。但如何在其中使用复杂节点?我想在 tikz 中绘制类似这样的内容:
答案1
也许你可以用这个库来做matrix
:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{tikzpicture}
[ mymatrix/.style={draw,thick,minimum width=1.0cm,minimum height=1.0cm,inner sep=0,anchor=south west}
]
\matrix[matrix of nodes, nodes=mymatrix] (firstmatrix) at (0,0)
{ q & w & e \\
a & s & d \\
};
\matrix[matrix of nodes, nodes=mymatrix] (secondmatrix) at (10,0)
{ i & o & p \\
j & k & l \\
};
\draw[-latex] (firstmatrix.20) -- (secondmatrix.200);
\end{tikzpicture}
\end{document}
编辑1:作为阿尔特蒙杜斯如上所述,内线是双线,这是不可取的,因此这里有一个使用节点和手动绘制的线的解决方案(我会尝试自动化):
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{tikzpicture}
[ mynode/.style={thick,minimum width=1.0cm,minimum height=1.0cm,inner sep=0,anchor=south west},
mymatrix/.style={draw,inner sep=0}
]
\matrix[matrix of nodes, nodes=mynode, mymatrix] (firstmatrix) at (0,0)
{ \node (q) {q}; & w & \node (e) {e}; \\
\node (a) {a}; & s & \node (d) {d}; \\
};
\draw (q.south west) -- (e.south east);
\draw (q.north east) -- (a.south east);
\draw (e.north west) -- (d.south west);
\matrix[matrix of nodes, nodes=mynode, mymatrix] (secondmatrix) at (10,0)
{ \node (i) {i}; & o & \node (p) {p}; \\
\node (j) {j}; & k & \node (l) {l}; \\
};
\draw (i.south west) -- (p.south east);
\draw (i.north east) -- (j.south east);
\draw (p.north west) -- (l.south west);
\draw[-latex] (firstmatrix.20) -- (secondmatrix.200);
\end{tikzpicture}
\end{document}
编辑1.1:作为安德鲁·史黛西注意到,当然没有必要明确放置节点。您是在matrix of nodes
Neo 中,而不是在从属中。所有单个单元格都具有形式为 的名称matrixname-rownumber-columnnumber
,可以使用此名称。以下产生的结果与之前完全相同:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows}
\begin{document}
\begin{tikzpicture}
[ mynode/.style={thick,minimum width=1.0cm,minimum height=1.0cm,inner sep=0,anchor=south west},
mymatrix/.style={draw,inner sep=0},
myline/.style={shorten <=0.2pt, shorten >=0.2pt},
]
\matrix[matrix of nodes, nodes=mynode, mymatrix] (firstmatrix) at (0,0)
{ q & w & e \\
a & s & d \\
};
\draw[myline] (firstmatrix-1-1.south west) -- (firstmatrix-1-3.south east);
\draw[myline] (firstmatrix-1-1.north east) -- (firstmatrix-2-1.south east);
\draw[myline] (firstmatrix-1-3.north west) -- (firstmatrix-2-3.south west);
\matrix[matrix of nodes, nodes=mynode, mymatrix] (secondmatrix) at (10,0)
{ i & o & p \\
j & k & l \\
};
\draw[myline] (secondmatrix-1-1.south west) -- (secondmatrix-1-3.south east);
\draw[myline] (secondmatrix-1-1.north east) -- (secondmatrix-2-1.south east);
\draw[myline] (secondmatrix-1-3.north west) -- (secondmatrix-2-3.south west);
\draw[-latex] (firstmatrix.20) -- (secondmatrix.200);
\end{tikzpicture}
\end{document}
编辑2:“避免”双重轮廓的另一种方法是draw
同时使用node
s 和matrix
,但后者的线宽为双倍:
\tikzset{
mynode/.style={line width=0.25pt, draw, minimum width=1.0cm, minimum height=1.0cm, inner sep=0, anchor=south west},
mymatrix/.style={line width=0.5pt, draw, inner sep=0},
myline/.style={shorten <=0.2pt, shorten >=0.2pt},
}
\begin{tikzpicture}
\matrix[matrix of nodes, nodes=mynode, mymatrix] (firstmatrix) at (0,0)
{ q & w & e \\
a & s & d \\
};
\end{tikzpicture}
答案2
您还可以使用臭名昭著 \tikzmark
解决方案允许人们在环境外部(或内部)组成表格tikzpicture
,并标记要连接的两个点,然后在它们之间画箭头。
因此,将c
数据条目定义为c\tikzmark{left}
,将j
数据条目定义为\tikzmark{right}j
,可以使用\DrawArrow[<draw_options>]{left}{right}
不同的绘图选项来获得:
笔记:
这确实需要两次运行。第一次确定位置,第二次进行绘图。
来自
\tikzmark
在正文旁边添加大括号。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand*{\DrawArrow}[3][]{%
% #1 = draw options
% #2 = left point
% #3 = right point
\begin{tikzpicture}[overlay,remember picture]
\draw [-latex, shorten <= 0.25cm, shorten >= 0.30cm, #1] ($(#2)+(0,0.5ex)$) to ($(#3)+(0,0.25ex)$);
\end{tikzpicture}%
}%
\begin{document}
\begin{tabular}{| c | c | c |}\hline
a & b & c\tikzmark{left} \\\hline
d & e & f \\\hline
\end{tabular}
\hspace*{2.0cm}
\begin{tabular}{| c | c | c |}\hline
g & h & i \\\hline
\tikzmark{right}j & k & l \\\hline
\end{tabular}
%
\DrawArrow{left}{right}
\end{document}
答案3
这是另一个基于彼得·格里尔的答案(我tabular
在写第一个答案时甚至没有想到使用 a,所以感谢你提醒我可以使用一些非 TikZ 构造)。它不使用\tikzmark
(这在其他方面很棒,但在我看来这里不需要)而是将表格放在node
s 中,因此您可以使用度数概念(node.degree
)来指定绘制箭头的位置:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\newcommand{\mytab}[2]% alignment, content
{ \begin{tabular}{#1}
#2
\end{tabular}
}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0] (one) {\mytab{|c|c|c|}{\hline g & h & i \\ \hline j & k & l \\ \hline}};
\node[inner sep=0,right=2cm of one.east] (two) {\mytab{|c|c|c|}{\hline a & b & c \\ \hline d & e & f \\ \hline}};
\draw[-latex] (one.15) to[out=0,in=180] (two.195);
\end{tikzpicture}
\end{document}