![如何使用 Tikz 绘制简单模型](https://linux22.com/image/456045/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Tikz%20%E7%BB%98%E5%88%B6%E7%AE%80%E5%8D%95%E6%A8%A1%E5%9E%8B.png)
答案1
仅使用软件包提供的库tikz
。显示两种情况:(i) 重新创建图像,(ii) 可能重新设计,我更喜欢后者。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
calc, chains,
decorations.pathreplacing,
calligraphy,
positioning,
quotes,
shapes}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[
node distance = 8mm and 24mm,
> = Stealth,
every path/.append style = {draw=cyan},
every edge quotes/.append style = {font=\footnotesize, sloped},
E/.style = {ellipse, draw, semithick, fill=cyan!30},
N/.style = {draw, semithick, minimum width = 8em, inner sep=1ex}
]
\node (a) [N] {text};
\node (b) [N, below=of a] {text};
\node (e) [E, right=of {$(a.east)!0.5!(b.east)$}] {TEXT};
%
\coordinate[below=of b.south] (aux);
\draw[<->] (a.west) -- ++ (-2em,0) |- (b.west) coordinate[pos=0.25] (ab);
\draw[->] (ab) -- ++ (-1em,0)
|- (aux) node[below, font=\footnotesize] {Model 3}
-| (e.west);
\draw[->, shorten >=1pt] (a.east) to ["Model 1"] (e.west);
\draw[->, shorten >=1pt] (b.east) to ["Model 2" '] (e.west);
\end{tikzpicture}
\end{figure}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[
node distance = 8mm and 16mm,
> = Stealth,
every path/.append style = {draw=cyan},
every edge quotes/.append style = {font=\footnotesize, sloped},
E/.style = {ellipse, draw, semithick, fill=cyan!30},
N/.style = {draw, semithick, minimum width = 8em, inner sep=1ex}
]
\node (a) [N] {text};
\node (b) [N, below=of a] {text};
\node (e) [E, right=of {$(a.east)!0.5!(b.east)$}] {TEXT};
%
\coordinate[below=of b.south] (aux);
\draw[<->] (a.west) -- ++ (-2em,0) |- (b.west) coordinate[pos=0.25] (ab);
\draw[->] (ab) -- ++ (-1em,0)
|- (aux) node[below, font=\footnotesize] {Model 3}
-| (e);
\draw[->, shorten >=1pt] (a.east) to ["Model 1"] (e);
\draw[->, shorten >=1pt] (b.east) to ["Model 2" '] (e);
\end{tikzpicture}
\end{figure}
\end{document}
答案2
使用正确的工具:
ellipse
来自shapes.geometric
图书馆的形状该
quotes
库可以轻松地沿边缘放置节点该
ext.paths.ortho
库提供路径操作r-lr
(<start>) r-lr (<target>)
它首先
ortho/lr distance
向左绘制,然后向上/向下,然后向右绘制<target>
。图书馆
ext.positioning-plus
允许right = of (r1)(r2)
将节点放置在 r1 和 r2 垂直中心的右侧,即
($(r1.north east)!.5!(r2.south east)$)
。
m
和坐标分别r3
作为模型 3 箭头的起点和中点。
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
arrows.meta, % for arrow tips
shapes.geometric, % for ellipse shape
ext.positioning-plus, % for of (r1)(r2)
ext.paths.ortho, % for r-lr and r-du
quotes, % for "nodes" on edges
}
\begin{document}
\begin{tikzpicture}[
>=Latex,
r/.style = {rectangle, draw=blue, minimum width=2cm},
ell/.style = {ellipse, draw, fill=blue!30},
node distance=5mm and 2cm,
% r-lr: to the left, then up/down, then right
lr/.style = {to path={r-lr (\tikztotarget)\tikztonodes}},
% lrhv: lr to #1, from #1 to target
lrhv/.style = {to path={r-lr (#1) \tikztonodes -| (\tikztotarget)}},
]
\node[r] (r1) {TEXT};
\node[r] (r2) [below=of r1] {TEXT};
\coordinate[below=of r2] (r3); % auxilliary coordinate
% right = of (r1)(r2) → right = of <the vertical middle of r1 and r2>
\node[ell] (ell) [right=of (r1)(r2)] {TEXT};
\path[blue, ->, text=black, sloped]
(r1) edge[-,lr] coordinate(m) (r2)
(r1.east) edge["Model 1"] (ell.west)
(r2.east) edge["Model 2" '] (ell.west)
(m) edge["Model 3" at end, ', lrhv=r3] (ell.west);
\end{tikzpicture}
\end{document}
输出
答案3
简单的图形应该用简单的代码来绘制。
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[teal,thick,nodes={text=black}]
\path
(5,0) node[ellipse,draw,fill=yellow,inner ysep=3mm] (E) {Ellipse}
(0,1) node[draw,minimum width=3cm,minimum height=8mm] (Ra) {Rectangle}
(0,-1) node[draw,minimum width=3cm,minimum height=8mm] (Rb) {Rectangle}
(-2,0) coordinate (A)
;
\draw[->] (Ra.east)--(E) node[above,sloped,midway]{Model 1};
\draw[->] (Rb.east)--(E) node[below,sloped,midway]{Model 2};
\draw (A)|-(Ra) (A)|-(Rb);
\draw[->] (A)--++(180:.5)--++(-90:2.5)-|(E) node[below,pos=.25]{Model 3};
\end{tikzpicture}
\end{document}