在 tikz 中绘制自定义节点和箭头

在 tikz 中绘制自定义节点和箭头

我正在尝试使用 tikz 在 paint 中绘制我在这里绘制的图片。我已经设法做到了这一点(请参阅右侧的代码和输出图),但无法完全达到我想要的效果(在 paint 中的绘图左侧有说明)。也就是说,有三件事我做不到:

  1. 将 f 周围的框改为不规则四边形,而不是矩形。

  2. 使从“b 位”指向 f 周围框的箭头不成对角线,并指向框的左侧(而不是顶部),但箭头不会与来自“l 位”的箭头合并,并且

  3. 我想要两个节点之间的箭头,前半部分是虚线,后半部分是标准箭头。

对于如何完成其​​中任何一项事情的任何帮助/建议都将不胜感激。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

\begin{tikzpicture}
[nodes={draw, thick, fill=black!0}]

\node (fone) [rectangle, minimum width=1cm, minimum height=1cm] {$f$};
\node (xone) [draw=none,fill=none, above left=1cm and 0cm of fone] {$b$ bits};
\node (ktwo) [draw=none,fill=none, left of=fone, xshift=-1cm] {$\ell$ bits};
\node (ftwo) [draw=none, fill=none, right of=fone, xshift=1cm]{$\ell$ bits};

\draw [arrow] (xone) -- (fone);[![enter image description here][2]][2]
\draw [arrow] (ktwo) -- (fone);
\draw [arrow] (fone) -- (ftwo);

\end{tikzpicture}
\end{document}

代码输出的图表 在此处输入图片描述

答案1

不需要您将所有内容放置在特定坐标下的解决方案如下:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzstyle{arrow} = [thick,->,>=stealth]

\usetikzlibrary{shapes,shapes.multipart} % Required for the trapezoid shape
\usetikzlibrary{calc} % Required for the computation of the angle arrow

\begin{document}

\begin{tikzpicture}
% Draw the trapezium and rotate by 90 degrees. A rotation of the text is also needed
\node (fone) [draw,trapezium,trapezium left angle=70,trapezium right angle=0,rotate=-90,minimum width=1cm, minimum height=1cm] {\rotatebox{90}{$f$}};

% Inotrduce a coordinate to lean on for drawing the angle shape
\node[coordinate, left of=fone] (x) {};

\node (xone) [draw=none,fill=none, above of=x,node distance=1.5cm] {$b$ bits};
\node (ktwo) [draw=none,fill=none, left of=fone, xshift=-1cm] {$\ell$ bits};
\node (ftwo) [draw=none, fill=none, right of=fone, xshift=1cm]{$\ell$ bits};

% The following connects xone with the coordinate x at the exact point where
% fone has 245 degrees (Note that it was rotated by 90 deg), then draw the line.
\draw [arrow] (xone) |- ($(x.north east)!(fone.245)!(x.south east)$) |- (fone.245);
\draw [arrow] (ktwo) -- (fone);
\draw [arrow] (fone) -- (ftwo);

\end{tikzpicture}
\end{document}

最终方案如下: 生成的图形的图片

答案2

我发现在这种情况下,手动控制所有物体的位置通常更容易

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

\begin{tikzpicture}
[nodes={draw, thick, fill=black!0}]

\draw (1,0) -- (2,0) -- (2,1) -- (1,2) -- (1,0);
\node (fone) [draw=none,fill=none] at (1.5,.5) {$f$} ;
\node (xone) [draw=none,fill=none] at (.25,3) {$b$ bits};
\node (ktwo) [draw=none,fill=none] at (-1,.5) {$\ell$ bits};
\node (ftwo) [draw=none, fill=none] at (4,.5) {$\ell$ bits};

\draw [arrow] (ktwo) -- (1,.5);
\draw [dashed] (2,.5) -- (3,.5);
\draw [arrow] (3,.5) -- (ftwo);
\draw [arrow] (.25,1) -- (1,1);
\draw (xone) -- (.25,1);

\end{tikzpicture}
\end{document}

相关内容