在电路图中在障碍物周围画线

在电路图中在障碍物周围画线

我有一个带有 3 个 DFF(矩形)和一个异或门(右侧的尖头)的电路图。

在此处输入图片描述

作为参考,让我们从左到右将组件标识为 x[n]、DFF0、DFF1、DFF2 和 XOR0。我想要的是将 DFF1.Q 路由到 XOR0.input 2。问题是连接不能越过 DFF2,并且它必须仅使用垂直或水平线。以下是我正在尝试执行的操作的手绘表示:

在此处输入图片描述

我尝试了不同的方法,阅读了几个小时,但还是没有得到我想要的。这是我的代码,我最近的努力在底部,被注释掉了。

\documentclass[a4paper,landscape]{article}

\usepackage{pgf,tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

\usetikzlibrary{calc,arrows}
\usepackage{amsmath}
\usepackage[left=1cm,right=1cm]{geometry}

\usetikzlibrary{arrows,shapes.gates.logic.US,shapes.gates.logic.IEC,calc}
\tikzstyle{branch}=[fill,shape=circle,minimum size=3pt,inner sep=0pt]

\pagestyle{empty}

\makeatletter

% Data Flip Flip (DFF) shape
\pgfdeclareshape{dff}{
  % The 'minimum width' and 'minimum height' keys, not the content, determine
  % the size
  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.25\pgf@x
    \pgf@y=0.25\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.25\pgf@x
    \pgf@y=-0.25\pgf@y
  }
  % Inherit from rectangle
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  % Define anchors for signal ports
  \anchor{D}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=.5\pgf@y%
  }
  \anchor{CLK}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=-.5\pgf@y%
  }
  \anchor{Q}{
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
  }
  \anchor{Qn}{
    \pgf@process{\northeast}%
    \pgf@y=-.5\pgf@y%
  }
  % Draw the rectangle box and the port labels
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
    % Angle (>) for clock input
    \pgf@anchor@dff@CLK
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@x \pgf@yc=\pgf@y
    \pgfmathsetlength\pgf@x{1.6ex} % size depends on font size
    \advance\pgf@ya by \pgf@x
    \advance\pgf@xb by \pgf@x
    \advance\pgf@yc by -\pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
    \pgfclosepath

    % Draw port labels
    \begingroup
    \tikzset{flip flop/port labels} % Use font from this style
    \tikz@textfont

    \pgf@anchor@dff@D
    \pgftext[left,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{\raisebox{-0.75ex}{D}}

    \pgf@anchor@dff@Q
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{Q}}

    \pgf@anchor@dff@Qn
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{$\overline{\mbox{Q}}$}}

    \endgroup
  }
}

% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}}

% Define default style for this node
\tikzset{flip flop/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every dff node/.style={draw,minimum width=2cm,minimum
    height=2.828427125cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add
    font=\sffamily}}

\makeatother

\begin{document}

\begin{tikzpicture}[font=\sffamily,>=triangle 45]

  % place dffs and draw connections
  \node [shape=dff] (DFF0) at (0,0) {};
  \node [shape=dff] (DFF1) at (1.5,0) {};
  \node [shape=dff] (DFF2) at (3,0) {};
  \draw [<-] (DFF0.D) -- +(-0.5,0) node [anchor=east] {$x[n]$} ;
  \draw [|-] (DFF0.Q) -- (DFF1.D);

  % place xor and draw connections
  \node[xor gate US, draw, logic gate inputs=nn, anchor=input 1] at
  ($(DFF2.Q)+(0.5,0)$) (XOR0) {};
  \draw (DFF2.Q) -- (XOR0.input 1);
%  \draw (DFF1.Q) |- ([xshift=0.25]DFF1.Q) |- ([yshift=-1.25]DFF1.Q) |- (XOR0.input 2);

\end{tikzpicture}

\end{document}

归功于马丁·沙雷尔为了DFF 代码

答案1

一种可能性是:

\documentclass[a4paper,landscape]{article}

\usepackage{pgf,tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

\usetikzlibrary{calc,arrows}
\usepackage{amsmath}
\usepackage[left=1cm,right=1cm]{geometry}

\usetikzlibrary{arrows,shapes.gates.logic.US,shapes.gates.logic.IEC,calc}
\tikzstyle{branch}=[fill,shape=circle,minimum size=3pt,inner sep=0pt]

\pagestyle{empty}

\makeatletter

% Data Flip Flip (DFF) shape
\pgfdeclareshape{dff}{
  % The 'minimum width' and 'minimum height' keys, not the content, determine
  % the size
  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.25\pgf@x
    \pgf@y=0.25\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.25\pgf@x
    \pgf@y=-0.25\pgf@y
  }
  % Inherit from rectangle
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  % Define anchors for signal ports
  \anchor{D}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=.5\pgf@y%
  }
  \anchor{CLK}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=-.5\pgf@y%
  }
  \anchor{Q}{
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
  }
  \anchor{Qn}{
    \pgf@process{\northeast}%
    \pgf@y=-.5\pgf@y%
  }
  % Draw the rectangle box and the port labels
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
    % Angle (>) for clock input
    \pgf@anchor@dff@CLK
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@x \pgf@yc=\pgf@y
    \pgfmathsetlength\pgf@x{1.6ex} % size depends on font size
    \advance\pgf@ya by \pgf@x
    \advance\pgf@xb by \pgf@x
    \advance\pgf@yc by -\pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
    \pgfclosepath

    % Draw port labels
    \begingroup
    \tikzset{flip flop/port labels} % Use font from this style
    \tikz@textfont

    \pgf@anchor@dff@D
    \pgftext[left,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{\raisebox{-0.75ex}{D}}

    \pgf@anchor@dff@Q
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{Q}}

    \pgf@anchor@dff@Qn
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{$\overline{\mbox{Q}}$}}

    \endgroup
  }
}

% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}}

% Define default style for this node
\tikzset{flip flop/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every dff node/.style={draw,minimum width=2cm,minimum
    height=2.828427125cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add
    font=\sffamily}}

\makeatother

\begin{document}

\begin{tikzpicture}[font=\sffamily,>=triangle 45]

  % place dffs and draw connections
  \node [shape=dff] (DFF0) at (0,0) {};
  \node [shape=dff] (DFF1) at (1.5,0) {};
  \node [shape=dff] (DFF2) at (3,0) {};
  \draw [<-] (DFF0.D) -- +(-0.5,0) node [anchor=east] {$x[n]$} ;
  \draw [|-] (DFF0.Q) -- (DFF1.D);

  % place xor and draw connections
  \node[xor gate US, draw, logic gate inputs=nn, anchor=input 1] at
  ($(DFF2.Q)+(0.5,0)$) (XOR0) {};
  \draw (DFF2.Q) -- (XOR0.input 1);
  \draw (DFF1.Q) -- +(7pt,0) |- ([xshift=7pt,yshift=-10pt]DFF2.Q|-DFF2.south) |- (XOR0.input 2);

\end{tikzpicture}

\end{document}

在此处输入图片描述

这是另一种变体,其中仅需指定垂直移动:

\documentclass[a4paper,landscape]{article}

\usepackage{pgf,tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

\usetikzlibrary{calc,arrows}
\usepackage{amsmath}
\usepackage[left=1cm,right=1cm]{geometry}

\usetikzlibrary{arrows,shapes.gates.logic.US,shapes.gates.logic.IEC,calc}
\tikzstyle{branch}=[fill,shape=circle,minimum size=3pt,inner sep=0pt]

\pagestyle{empty}

\makeatletter

% Data Flip Flip (DFF) shape
\pgfdeclareshape{dff}{
  % The 'minimum width' and 'minimum height' keys, not the content, determine
  % the size
  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.25\pgf@x
    \pgf@y=0.25\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.25\pgf@x
    \pgf@y=-0.25\pgf@y
  }
  % Inherit from rectangle
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  % Define anchors for signal ports
  \anchor{D}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=.5\pgf@y%
  }
  \anchor{CLK}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=-.5\pgf@y%
  }
  \anchor{Q}{
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
  }
  \anchor{Qn}{
    \pgf@process{\northeast}%
    \pgf@y=-.5\pgf@y%
  }
  % Draw the rectangle box and the port labels
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
    % Angle (>) for clock input
    \pgf@anchor@dff@CLK
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@x \pgf@yc=\pgf@y
    \pgfmathsetlength\pgf@x{1.6ex} % size depends on font size
    \advance\pgf@ya by \pgf@x
    \advance\pgf@xb by \pgf@x
    \advance\pgf@yc by -\pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
    \pgfclosepath

    % Draw port labels
    \begingroup
    \tikzset{flip flop/port labels} % Use font from this style
    \tikz@textfont

    \pgf@anchor@dff@D
    \pgftext[left,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{\raisebox{-0.75ex}{D}}

    \pgf@anchor@dff@Q
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{Q}}

    \pgf@anchor@dff@Qn
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{$\overline{\mbox{Q}}$}}

    \endgroup
  }
}

% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}}

% Define default style for this node
\tikzset{flip flop/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every dff node/.style={draw,minimum width=2cm,minimum
    height=2.828427125cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add
    font=\sffamily}}

\makeatother

\begin{document}

\begin{tikzpicture}[font=\sffamily,>=triangle 45]

  % place dffs and draw connections
  \node [shape=dff] (DFF0) at (0,0) {};
  \node [shape=dff] (DFF1) at (1.5,0) {};
  \node [shape=dff] (DFF2) at (3,0) {};
  \draw [<-] (DFF0.D) -- +(-0.5,0) node [anchor=east] {$x[n]$} ;
  \draw [|-] (DFF0.Q) -- (DFF1.D);

  % place xor and draw connections
  \node[xor gate US, draw, logic gate inputs=nn, anchor=input 1] at
  ($(DFF2.Q)+(0.5,0)$) (XOR0) {};
  \draw (DFF2.Q) -- (XOR0.input 1);
  \coordinate  (aux) at ($ (DFF2.Q)!0.5!(XOR0.input 2) $);
  \draw (DFF1.Q) -- ($ (DFF1.Q)!0.5!(DFF2.D)$ ) |- ([yshift=-10pt]DFF2.Q|-DFF2.south) -|
(aux|-XOR0.input 2) -- (XOR0.input 2);

\end{tikzpicture}

\end{document}

在此处输入图片描述

作为 奎伯比尔贝尔在评论中提到,定义样式可能是一个更好的选择,特别是如果这种路径要多次使用。两种样式如下

-|_|-/.style args={#1:#2}{
  to path={-| ( $ (\tikztostart)!0.5!([yshift=-#1]#2.south west|-#2.south) $ ) |- ([yshift=-#1] #2.south) -|
  ($(\tikztotarget)!0.5!([yshift=-#1]#2.south east|-#2.south)$) |- (\tikztotarget)}
  },
-|^|-/.style args={#1:#2}{
  to path={-| ( $ (\tikztostart)!0.5!([yshift=#1]#2.north west|-#2.north) $ ) |- ([yshift=#1] #2.north) -|
  ($(\tikztotarget)!0.5!([yshift=#1]#2.north east|-#2.north)$) |- (\tikztotarget)}
  }

第一个允许绘制“障碍物下方”,第二个允许绘制“障碍物上方”。它们可以用作例如

\draw (DFF1.Q) to[-|_|-=10pt:DFF2] (XOR0.input 2);
\draw (DFF1.Q) to[-|^|-=10pt:DFF2] (XOR0.input 2);

第一个在第二个参数下方按照第一个参数给出的距离进行绘制;第一个在第二个参数上方按照第一个参数给出的距离进行绘制。

完整示例:

\documentclass[a4paper,landscape]{article}

\usepackage{pgf,tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

\usetikzlibrary{calc,arrows}
\usepackage{amsmath}
\usepackage[left=1cm,right=1cm]{geometry}

\usetikzlibrary{arrows,shapes.gates.logic.US,shapes.gates.logic.IEC,calc}
\tikzstyle{branch}=[fill,shape=circle,minimum size=3pt,inner sep=0pt]

\pagestyle{empty}

\makeatletter

% Data Flip Flip (DFF) shape
\pgfdeclareshape{dff}{
  % The 'minimum width' and 'minimum height' keys, not the content, determine
  % the size
  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.25\pgf@x
    \pgf@y=0.25\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.25\pgf@x
    \pgf@y=-0.25\pgf@y
  }
  % Inherit from rectangle
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  % Define anchors for signal ports
  \anchor{D}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=.5\pgf@y%
  }
  \anchor{CLK}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=-.5\pgf@y%
  }
  \anchor{Q}{
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
  }
  \anchor{Qn}{
    \pgf@process{\northeast}%
    \pgf@y=-.5\pgf@y%
  }
  % Draw the rectangle box and the port labels
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
    % Angle (>) for clock input
    \pgf@anchor@dff@CLK
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgf@xc=\pgf@x \pgf@yc=\pgf@y
    \pgfmathsetlength\pgf@x{1.6ex} % size depends on font size
    \advance\pgf@ya by \pgf@x
    \advance\pgf@xb by \pgf@x
    \advance\pgf@yc by -\pgf@x
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
    \pgfclosepath

    % Draw port labels
    \begingroup
    \tikzset{flip flop/port labels} % Use font from this style
    \tikz@textfont

    \pgf@anchor@dff@D
    \pgftext[left,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{\raisebox{-0.75ex}{D}}

    \pgf@anchor@dff@Q
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{Q}}

    \pgf@anchor@dff@Qn
    \pgftext[right,base,at={\pgfpoint{\pgf@x}{\pgf@y}},x=-\pgfshapeinnerxsep]{\raisebox{-.75ex}{$\overline{\mbox{Q}}$}}

    \endgroup
  }
}

% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}}

% Define default style for this node
\tikzset{flip flop/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every dff node/.style={draw,minimum width=2cm,minimum
    height=2.828427125cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add
    font=\sffamily}}

\makeatother

\tikzset{
-|_|-/.style args={#1:#2}{
  to path={-| ( $ (\tikztostart)!0.5!([yshift=-#1]#2.south west|-#2.south) $ ) |- ([yshift=-#1] #2.south) -|
  ($(\tikztotarget)!0.5!([yshift=-#1]#2.south east|-#2.south)$) |- (\tikztotarget)}
  },
-|^|-/.style args={#1:#2}{
  to path={-| ( $ (\tikztostart)!0.5!([yshift=#1]#2.north west|-#2.north) $ ) |- ([yshift=#1] #2.north) -|
  ($(\tikztotarget)!0.5!([yshift=#1]#2.north east|-#2.north)$) |- (\tikztotarget)}
  }
}

\begin{document}

\begin{tikzpicture}[font=\sffamily,>=triangle 45]

  % place dffs and draw connections
  \node [shape=dff] (DFF0) at (0,0) {};
  \node [shape=dff] (DFF1) at (1.5,0) {};
  \node [shape=dff] (DFF2) at (3,0) {};
  \draw [<-] (DFF0.D) -- +(-0.5,0) node [anchor=east] {$x[n]$} ;
  \draw [|-] (DFF0.Q) -- (DFF1.D);

  % place xor and draw connections
  \node[xor gate US, draw, logic gate inputs=nn, anchor=input 1] at
  ($(DFF2.Q)+(0.5,0)$) (XOR0) {};
  %\draw (DFF2.Q) -- (XOR0.input 1);
  \draw (DFF1.Qn) to[-|_|-=10pt:DFF2] (XOR0.input 2);
  \draw (DFF1.Q) to[-|^|-=10pt:DFF2] (XOR0.input 1);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

如果你经常需要正交路径,你可以使用我的paths.ortho图书馆并定义to path类似于Gonzalo Medina 的解决方案

\usetikzlibrary{paths.ortho}
\tikzset{
 -|_|-/.style args={#1:#2}{
  hvvh/ratio=.5,
  to path={
    -|- ([yshift=-#1]#2.south west) -- ([yshift=-#1]#2.south east) -|- (\tikztotarget)
 }},
 -|^|-/.style args={#1:#2}{
  hvvh/ratio=.5,
  to path={
    -|- ([yshift=#1]#2.north west) -- ([yshift=#1]#2.north east) -|- (\tikztotarget)
 }}
}

然后画线,然后用

\draw[blue] (DFF1.Qn) to[-|_|-=.25cm:DFF2] node[auto,swap] {above} (XOR0.input 2);
\draw[red] (DFF1.Q) to[-|^|-=.25cm:DFF2]   node[auto]      {below} (XOR0.input 1);

看起来像

在此处输入图片描述

路径-|_|-围绕节点( 后的第二个参数:)绘制一条线,距离第一个参数的距离为 ,而垂直部分位于节点之间的中间。如果south westsouth east锚点不位于相同的垂直高度,则应south 使用([yshift=-#1]#2.south west|-#2.south)等。

相关内容