tikz 增量相对路径连接

tikz 增量相对路径连接

我想要创建的图表的一个示例是:

基本框图

我可以使用以下 tikz 接近所需的输出:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{intersections,shapes,arrows.meta,positioning}
\SetSymbolFont{letters}{bold}{OML}{cmbr}{bx}{it}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[
          line width=1.0pt
          , node distance=3cm
          , defblk/.style = {draw, rectangle, draw=black, minimum height=8cm, minimum width=1cm}
    ]

    \node[defblk, align=center](a){Block A};
    \node[defblk, right=of a](b){Block B};

    \draw[->] ([yshift=0 * -1cm]a.north east) -- node[above]{Signal 1} ([yshift=0 * -1cm]b.north west) ;
    \draw[->] ([yshift=1 * -1cm]a.north east) -- node[above]{Signal 2} ([yshift=1 * -1cm]b.north west) ;
    \draw[<-] ([yshift=2 * -1cm]a.north east) -- node[above]{Signal 3} ([yshift=2 * -1cm]b.north west) ;

    \draw[<-] ([yshift=4 * -1cm]a.north east) -- node[above]{Signal 4}([yshift=4 * -1cm]b.north west) ;
    \draw[<-] ([yshift=5 * -1cm]a.north east) -- node[above]{Signal 5}([yshift=5 * -1cm]b.north west) ;


\end{tikzpicture}
\end{document}

这是一个相当手动的过程,但重新安排连接需要我重新输入所有不同的偏移量。我想要做的是能够以以下方式逐步指定节点:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections,shapes,arrows.meta,positioning}
\SetSymbolFont{letters}{bold}{OML}{cmbr}{bx}{it}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[
          line width=1.0pt
          , node distance=3cm
          , defblk/.style = {draw, rectangle, draw=black, minimum height=8cm, minimum width=1cm}
    ]

    \node[defblk, align=center](a){Block A};
    \node[defblk, right=of a](b){Block B};

% This syntax below is pseudo-code that expresses how
% I'd like to incrementally specify paths
    \draw (a.north east) 
                    node[above, ->](sig1){Signal 1} -- ((sig1) -| (b.west))
        ++(0, -1cm) node[above, ->](sig2){Signal 2} -- ((sig2) -| (b.west))
        ++(0, -1cm) node[above, <-](sig3){Signal 3} -- ((sig3) -| (b.west))
        ++(0, -1cm) 
        ++(0, -1cm) node[above, <-](sig4){Signal 4} -- ((sig4) -| (b.west))
        ++(0, -1cm) node[above, <-](sig5){Signal 5} -- ((sig5) -| (b.west));

\end{tikzpicture}
\end{document}

根据@marmot,带有 [count=\Z] 选项的 \foreach 循环实现了我想要的效果。下面包含一个更详细的示例:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}
\SetSymbolFont{letters}{bold}{OML}{cmbr}{bx}{it}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[
          line width=1.0pt
          , node distance=3cm
          , rightof/.style = {right=of #1.north east, anchor=north west}
          , defblk/.style = {draw, rectangle, draw=black, minimum height=8cm, minimum width=1cm}
          , sig/.style = {above right, text width=2cm, align=left, black}
          , lvds/.style = {blue!80}
          , lvcmos/.style = {black}
    ]

    \node[defblk, align=center](a){Block A};
    \node[defblk, right=of a](b){Block B};

    \foreach \dir/\type/\name [count=\Z] in {
         ->/lvds/din clk
        ,->/lvds/din data
        ,->/lvds/din en
        ,->/lvds/din sync
        ,<-/lvds/din interrupt
        ,white//{}
        ,<-/lvds/dout clk
        ,<-/lvds/dout data
        ,<-/lvds/dout en
        ,white//{}
        ,<-/lvcmos/ctrl clk
        ,<-/lvcmos/ctrl data
        ,<-/lvcmos/ctrl en
    }
    {
        \def\ioff{-1em}
        \def\ispc{-1.5em}
        \draw[\dir, \type] ([yshift=\ioff + \Z*\ispc]a.north east) 
        node[sig]{\name} 
        -- 
        ([yshift=\ioff + \Z*\ispc]b.north west);
    }

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

欢迎使用 TeX.SE!缩短代码的一种方法是使用循环。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\SetSymbolFont{letters}{bold}{OML}{cmbr}{bx}{it}
\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[
          line width=1.0pt
          , node distance=3cm
          , defblk/.style = {draw, rectangle, draw=black, minimum height=8cm, minimum width=1cm}
    ]

    \node[defblk, align=center](a){Block A};
    \node[defblk, right=of a](b){Block B};
    \foreach \X/\Y [count=\Z] in {->/0,->/1,<-/2,<-/4,<-/5}
    {\draw[\X] ([yshift=\Y * -1cm]a.north east) -- node[above]{Signal \Z}
    ([yshift=\Y * -1cm]b.north west);}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

呵呵,@marmot 打败了我四分钟......想法是一样的,但实现方式略有不同......

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

\begin{document}
    \begin{tikzpicture}[
node distance = 5mm and 30mm,
   line width = 1.0pt,
            > = Stealth,
defblk/.style = {rectangle, draw=black,
                 minimum height=4cm, minimum width=1cm,
                 outer sep=0pt},
    sy/.style = {yshift=#1 mm},
every edge quotes/.style = {anchor=south,inner sep=1pt, font=\footnotesize}
                    ]
\node[defblk]               (a) {Block A};
\node[defblk, right=of a]   (b) {Block B};
%
\coordinate[below=of a.north east] (sig1);
\foreach \i [count=\j] in {0,1,2, 4,5}
{\ifnum\j<3
    \draw[->]   ([sy=-\i*5] sig1) to ["Signal \j"] ([sy=-\i*5] sig1 -| b.west)
 \else
    \draw[<-]   ([sy=-\i*5] sig1) to ["Signal \j"] ([sy=-\i*5] sig1 -| b.west)
 \fi;
}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容