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.

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

share|improve this question

1 Answer 1

up vote 13 down vote accepted

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

share|improve this answer
    
Thanks! That's exactly what I was looking for. –  Alan Munn Nov 2 '12 at 20:38
    
@AlanMunn You are very welcome. –  percusse Nov 2 '12 at 20:43

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.