15

In this question Is there a TikZ equivalent to the PSTricks \ncbar command? Jake provided a very nice TikZ version of the PSTricks \ncbar command.

With normal node connections in TikZ, I can use the [auto] parameter to automatically place nodes along paths. I'd like to be able to do this using the \ncbar path, but I'm not sure how.

So in the code below, I'd like to add a C node along the bent arrow similar to the C node added to the straight arrow.

\documentclass{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}[auto]
   \node (A) at (1,4) {A};
   \node (B) at (3,4) {B};
   \draw[->] (A) to  node {C} (B);
   \draw[->] (A) to [ncbar=-1.5em] node {C} (B); % can I get this to work?
   \end{tikzpicture}

\end{document}

output of code

14

You can add \tikztonodes to the path construction in the main link.

\documentclass{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)$) 
           \tikztonodes
        -- (\tikztotarget) 
    },
    ncbar/.default=0.5cm,
}
\begin{document}

   \begin{tikzpicture}[auto]
   \node (A) at (1,4) {A};
   \node (B) at (3,4) {B};
   \draw[->] (A) to  node {C} (B);
   \draw[->] (A) to [ncbar=-1.5em] node[pos=0.3,below] {C} (B); 
   \end{tikzpicture}

\end{document}

enter image description here

  • @AlanMunn You are very welcome. – percusse Nov 2 '12 at 20:43

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.