首页 > 解决方案 > 如何用 Tikz 在别人身上画一个障碍?

问题描述

这是我尝试使用 tikz 制作的图表。块 A、B 和 C 位于一个块内(此处为红色背景块),块 E 和 F 位于另一个块内。两个大块(红色背景)顶部有标签(LabA 和 LabB)。此外,我希望 E 块在 A 块和 B 块的中间,并且位于 A 块和 B 块的中间。指向 E 块的箭头应该有一些弯曲角度;所以,不像我试过的那个。

预期的

这是我的代码。

\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\tikzstyle{block} = [draw=black, thick, text width=2cm, minimum height=1cm, align=center]  
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

    \begin{tikzpicture}
    
        \node[block] (a) {a};
        \node[block, below=of a, yshift=0.7cm] (b) {b};
        \node[block, below=of b, yshift=0.7cm] (c) {c};
        \node[block, right=of c, xshift=0.7cm] (d) {d};
        
        \node[block, right=of a, xshift=0.7cm, yshift=-0.4cm] (e) {e};
        
        \draw [arrow] (a) -- (e);
        \draw [arrow] (b) -- (e);
        \draw [arrow] (c) -- (d);
        
    \end{tikzpicture}

\end{document}

我怎么能在一些节点上添加一个矩形并在上面写一个标签。
此处如何使箭头具有一定的倾斜角度(不是直的),以及如何使 E 块位于 A 块和 B 块的中间和右侧。我虽然可以得到箭头 \draw [arrow] (a) -|- (e);,但它不起作用。
先感谢您

当前状态

标签: latextikz

解决方案


我试了一下,希望能得到正确的要求:

\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\tikzstyle{block} = [draw=none, thick, text width=.4cm, minimum height=.5cm, align=center, fill=blue!50]  
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}


    \begin{tikzpicture}
    \Large
        %red rectangles    
        \node (r1) [draw=none, fill=red, minimum width=1.2cm,minimum height=3.5cm]{};
        \node (r2) [right=2cm of r1.center, anchor=center, draw=none, fill=red, minimum width=1.2cm,minimum height=3.5cm]{};
        \node (r1Label)[above=0cm of r1] {\textbf{LabA}};
        \node (r2Label)[above=0cm of r2]{\textbf{LabB}};
    
        %nodes
        \node[block, below=3mm of r1.north, anchor=north] (a) {\color{white}A};
        \node[block, below=4mm of a] (b) {\color{white}B};
         \node[block, below=4mm of b] (c) {\color{white}C};
        \node[block, right=2cm of c.center, anchor=center] (d) {\color{white}D};
        \node at ($(a.south)+(2,-.2)$) [block] (e) {\color{white}E};
        
        %arrows
        \draw [arrow, rounded corners=2] (a) -- ($(a)+(1,0)$) |- ($(e.west)+(0,.1)$);
        \draw [arrow, rounded corners=2] (b) -- ($(b)+(1,0)$) |- ($(e.west)+(0,-.1)$);
        \draw [arrow] (c) -- (d);
        
    \end{tikzpicture}

\end{document}

有了这个结果:

乳胶输出

一些解释:

  • 您可以通过在其他节点之前定义它们来在后面绘制矩形。使用层有更复杂的方法,但我认为在这个例子中就足够了。
  • 我主要使用给定示例代码中的绝对距离。您可能需要调整...但是,使用这些您可以轻松找到 A 和 B 之间的中间(距离 4mm),以找到 E 的 y 位置(a.south+ 2mm)。使用 ($(node1)+(node2)$) 您可以计算节点位置。
  • 您提到的-|-选项分两步工作。首先绘制--到某个锚点(否则这是不明确的),然后绘制到|-目标节点。
  • draw 的圆角选项将圆角半径作为参数。

推荐阅读