起始线从锚点节点移位

起始线从锚点节点移位

我正在创建一个框图,我想在其中创建两个方向相反的箭头。为此,我创建了两个节点,每个节点都有一个从test 1到 的箭头below test 1,但当我尝试将其沿 x 方向移动时,我得到了一些不完整的结果。

绿线从中心到中心开始,红线从底部到顶部,但开始偏移但再次在中点结束。

以下是我的 MWE:

\documentclass[tikz,margin=3mm]{standalone}
\usepackage[svgnames]{xcolor}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows,arrows.meta, positioning} 
\usepackage[utf8]{inputenc}

\begin{document}
        \begin{tikzpicture}
        \tikzstyle{line} = [draw, thick, -latex',shorten >=5pt];
        \node(T1) [rectangle, draw=red, fill=red!30, thick, minimum width=3cm, minimum height=2cm, text centered, rounded corners] {Test 1};
        \node(T2) [below=of T1, rectangle, dashed, draw=red!80, fill=red!15, thick, minimum width=3cm, minimum height=2cm, text centered, rounded corners] {Below test 1};
        \tikzstyle{every path}=[line]
        % Arrows TC -> PR
        \path [dashed] (T1) -- (T2);
        \path [draw=green, dashed] (T1) ++ (0.5,0) -- (T2) ++ (0.5,0);
        \path [draw=red, dashed] (T1.south) ++ (0.5,0) -- (T2.north) ++ (0.5,0);
    \end{tikzpicture}

\end{document}

我只需要两条垂直线/箭头稍微从南北锚点移动一下,但我无法做到正确。

答案1

看这里: 应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式? 但是如果你只需要这张图片的样式甚至不需要tikzset,你可以直接将你的设置作为环境的一个选项tikzpicture

此外,arrows该库已被弃用。

对于箭头定位,我认为最简单的方法是使用xshift

我还对缩写做了一些修改,因为我认为这样看起来更好:shorten >=2pt, shorten <=2pt

\documentclass[margin=3mm]{standalone}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta, positioning} 
\usepackage[utf8]{inputenc}

\begin{document}
    \begin{tikzpicture}[%
        every path/.style={thick, shorten >=2pt, shorten <=2pt},
        mynode/.style={rectangle, thick, minimum width=3cm, 
            minimum height=2cm, text centered, 
            rounded corners},
        ]
        \node(T1) [mynode, draw=red, fill=red!30] {Test 1};
        \node(T2) [mynode, below=of T1, dashed, draw=red!80, fill=red!15] 
            {Below test 1};
        % Arrows TC -> PR
        \path [draw=green, dashed, -Stealth] 
            ([xshift=-.5cm]T1.south) -- ([xshift=-.5cm]T2.north);
        \path [draw=red, dashed, Stealth-] 
            ([xshift=.5cm]T1.south) -- ([xshift=.5cm]T2.north);
    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, positioning}


\begin{document}
    \begin{tikzpicture}[
     line/.style = {draw, thick, -Latex, dashed, 
                    shorten >=3pt, shorten <=3pt},
 box/.style args = {#1/#2}{%
        rectangle, rounded corners, thick,
        draw=#1,
        fill=#2,
        minimum width=3cm, minimum height=2cm, align=center},
    box/.default = red/red!30
                        ]
\node (T1)  [box]           {Test 1};
\node (T2)  [box=red!80/red!15, dashed,
             below=of T1]   {Below test 1};
\draw[line,blue]    ([xshift=-4mm] T1.south) -- ([xshift=-4mm] T2.north);
\path[line]         (T1) -- (T2);
\draw[line,green]   ([xshift=+4mm] T2.north) -- ([xshift=+4mm] T1.south);
    \end{tikzpicture}
\end{document}

计算坐标的方式仅对起始坐标有效。最后一个仅被视为第一个。但是,使用calc库,您可以将坐标计算“翻译”为:

\draw ($(T1.south)+(-0.4,0)$) -- ($(T2.north)+(-0.4,0)$); 

它给出的结果与使用xshift上面所示的结果相同姆韦

在上面姆韦使用最近的tikz语法。节点和线条样式定义为的选项tikzpicture。如果你在其他图像中使用此样式,那么将其定义为

\tikzset{mytest/.style = {
     line/.style = {draw, thick, -Latex, dashed, 
                    shorten >=3pt, shorten <=3pt},
 box/.style args = {#1/#2}{%
        rectangle, rounded corners, thick,
        draw=#1,
        fill=#2,
        minimum width=3cm, minimum height=2cm, align=center},
    box/.default = red/red!30
    }

然后在图片中使用相同的节点和线条样式,如下所示:

\begin{tikzpicture}[mytest]
....
\end{picture}

相关内容