29

Although I now use TikZ, every so often I find myself looking for very useful PSTricks commands which don't seem to exist in TikZ. The pst-node package has an \ncbar command which allows one to connect two nodes with arms dropping down, typically at a 90 degree angle. Here's an example:

\documentclass{article}
\usepackage{pst-node}
\begin{document}
\rput(0,0){\rnode{A}{\psframebox{A}}}
\rput(2,0){\rnode{B}{\psframebox{B}}}
\ncbar[angle=90]{A}{B}
\end{document}

output of code

Is there a TikZ equivalent to this useful command? If not, how easy would it be to create one?

27

There's no direct equivalent, but you can use the to path mechanism together with the calc library to do something similar. If you set:

\tikzset{
    ncbar angle/.initial=90,
    ncbar/.style={
        to path=(\tikztostart)
        -- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
        -- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
        -- (\tikztotarget)
    },
    ncbar/.default=0.5cm,
}

you can draw a rectangular connection between two coordinates using \draw (a) to [ncbar] (b);. The offset of the line can be controlled using the optional parameter: \draw (a) to [ncbar=1cm] (b);. To change the angle of the connecting line, you can use the key ncbar angle: \draw [blue] (a) to [ncbar=1.5cm, ncbar angle=70] (b);

\documentclass[landscape]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    ncbar angle/.initial=90,
    ncbar/.style={
        to path=(\tikztostart)
        -- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
        -- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
        -- (\tikztotarget)
    },
    ncbar/.default=0.5cm,
}

\begin{document}
\begin{tikzpicture}
\node [draw] (a) {A};
\node at (2,-1) [draw] (b) {B};

\draw  (a) to [ncbar] (b);
\draw [red] (a) to [ncbar=-1cm] (b);
\draw [blue] (a) to [ncbar=1.5cm, ncbar angle=70] (b);
\end{tikzpicture}
\end{document}
  • With some fancy code, you could make it ncbar=1cm at 90 to be able to specify the angle as well (for extra points, ncbar=1cm and ncbar should also work). – Loop Space May 9 '12 at 13:16
  • I think @AndrewStacey is referring to a ncbar/.style args={#1 at #2}{....} type of input parsing. Nevertheless, great stuff. – percusse May 9 '12 at 13:26
  • 1
    @percusse Almost. You'd have to be a bit more sneaky to be able to handle ncbar=1cm and ncbar=1cm at 90. – Loop Space May 9 '12 at 13:28
  • @AndrewStacey: I'm not sure that that syntax would be preferable, though (not just because it's much more work to implement): If you have a separate angle key, you can set it for all ncbars without specifying it explicitly. – Jake May 9 '12 at 13:34
  • Excellent! Should the ncbar angle/.initial be outside the ncbar style to make it possible to set it globally? – Loop Space May 9 '12 at 15:15
12

Reference

Code

\documentclass{article}
\usepackage{pst-node,tikz}
\usetikzlibrary{paths.ortho}
\begin{document}
\hspace{.125cm}
\rput(0,0){\rnode{A}{\psframebox{A}}}
\rput(2,1){\rnode{B}{\psframebox{B}}}
\ncbar[angle=90]{A}{B}
\ncbar[angle=45]{A}{B}

\par
\begin{tikzpicture}[thick,nodes=draw]
\node (A)          {A};
\node (B) at (2,1) {B};

\draw (A) ud (B);
\draw[rotate=45] (A) rl (B);
\end{tikzpicture}
\end{document}

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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