Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a circuit diagram with 3 DFFs (rectangles) and an xor gate (pointy bit on the right).

enter image description here

For reference, let's identify the components from left to right as x[n], DFF0, DFF1, DFF2 and XOR0. What I want is for DFF1.Q to be routed to XOR0.input 2. The problem is that the connection cannot go over DFF2, and it must use vertical or horizontal lines only. Here is a freehand representation of what I am trying to do:

enter image description here

I have tried different things and reading for a few hours and can't get what I want. Here is my code, my most recent effort is at the bottom, commented out.

\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}

Credit to Martin Scharrer for DFF code

share|improve this question

2 Answers 2

up vote 3 down vote accepted

One possibility:

\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}

enter image description here

Here's another variation in which only the vertical shifting has to be specified:

\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}

enter image description here

As Qrrbrbirlbel mentions in a comment, defining styles can be a better option specially if this kind of paths is going to be used several times. The two styles are given by

-|_|-/.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)}
  }

the first one allows to draw "below an obstacle", and the second one allows to draw "above an obstacle". They are to be used as, for example

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

The first one draws around below the second argument with a distance given by the first argument; the first one draws around above the second argument with a distance given by the first argument.

A complete example:

\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}

enter image description here

share|improve this answer
    
Quality service! It is working on my end. I'll take some time to understand it, make sure there are no other problems, and let anyone else chime in with other info. After that I'll accept this. –  trav1s Aug 8 '13 at 2:02
1  
@trav1s I added another option involving less manual calculations; now only the vertical distance between the DFF2 shape and the connecting line has to be specified. –  Gonzalo Medina Aug 8 '13 at 2:10
1  
@trav1s I’d define a to path for that \tikzset{-|_|-/.style args={#1:#2}{to path={-| ($(\tikztostart)!0.5!(#2.west)$) |- ([yshift=-#1] #2.south) -| ($(\tikztotarget)!0.5!(#2.east)$) |- (\tikztotarget)}}} which, in this case, has to been used as \draw (DFF1.Q) to[-|_|-=10pt:DFF2] (XOR0.input 2);. With this you can draw around any shape (the second argment) with the distance of the first argument. –  Qrrbrbirlbel Aug 8 '13 at 2:46
    
@Qrrbrbirlbel yes, I've incorporated your suggestion to my answer (with a minor correction) and adding another style -|^|- to draw "above an obstacle". –  Gonzalo Medina Aug 8 '13 at 3:37
    
@trav1s I updated my answer incorporating Qrrbrbirlbel's suggestion (with a minor correction) and adding an additional style to draw "above the obstacle". –  Gonzalo Medina Aug 8 '13 at 3:44

If you often need orthogonal paths you can use my paths.ortho library and define a to path similar to Gonzalo Medina’s solution:

\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)
 }}
}

and draw the line then with

\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);

which looks like

enter image description here

They -|_|- path draws a line around a node (the second argument after the :) with the distance from the first argument while the vertical part is midway between the nodes. If the south west and south east anchors do not lie on the same vertical height as south you should use ([yshift=-#1]#2.south west|-#2.south) and so on.

share|improve this answer
    
This is a great solution if the problem ends up being recurring for me. I will keep this in mind. –  trav1s Aug 9 '13 at 1:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.