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 would like to use a mark connection node decoration on a line that also has an arrow tip. Unfortunately, using the decoration causes the arrow tip to point in the wrong direction (always straight up). For example:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
  \node (A) at (0, 0) {A} ;
  \node (B) at (3, 0) {B} ;
  \draw [->, decorate, decoration={
    markings,
    mark connection node=label,
    mark=at position .5 with {
      \node (label) {label};
    },
  }]
  (A) -- (B)
  ;
\end{tikzpicture}
\end{document}

output showing incorrect arrow tip direction

Notice the arrow tip, just to the left of node B, pointing up instead of to the right.

The Tikz documentation mentions that “the decoration usually destroys the path. However, this is no longer the case when the [mark connection node] key is set.” If the path is not being destroyed, then why is the arrow tip oriented incorrectly? Is there any way to get arrow tips and mark connection node to coexist on the same path?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Looking at the figure, my code would be very different. I don't see the need of decorations for that:

\documentclass{standalone}
\usepackage{tikz}
\tikzset{
  label/.style = {font=\footnotesize, midway, fill=white, anchor=center}
}

\begin{document}
\begin{tikzpicture}
  \node (A) at (0, 0) {A} ;
  \node (B) at (3, 0) {B} ;
  \draw [->] (A) -- (B) node[label] {label};
\end{tikzpicture}
\end{document}

Result

share|improve this answer
1  
This works well for my minimal example, and is the simplest-looking solution yet offered. In a more complex diagram, though, the fill=white strategy will create problems if any other background content is present. An ideal solution would create a transparent gap in the line where it passes through the label. That's what I'd hoped mark connection node would do. –  Ben Liblit Feb 26 '13 at 22:19
    
My background content concerns are really hypothetical, as I have no such background material in the diagram I actually want to make. I do have curves, though, and of the three solutions proposed so far, this is the only one that lets curves remain curved. I'm going to mark this as the accepted answer. I will probably post a new question with the challenge of interrupting a curved line with a label without obscuring background content. –  Ben Liblit Feb 28 '13 at 15:56

I don't really know what is happening here with the ->. But you can use mark=at position 1 with {\arrow{>}}; to get the desired result. Also, you can define a style label decorate so that you can use decorate option for other elements in your tikzpicture or document.

\documentclass[border=5,convert={density=150},resize=200]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{label decorate/.style={
    decorate,
    decoration={
    markings,
    mark connection node=label,
    mark=at position .5 with {
      \node (label) {label};
    },
    mark=at position 1 with {\arrow{>}};
    }}}

\begin{document}
\begin{tikzpicture}
  \node (A) at (0, 0) {A} ;
  \node (B) at (3, 0) {B} ;
  \draw [label decorate] (A) -- (B);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
This looks good when small. At higher magnifications, one can see that the right edge of the line extends slightly beyond the arrow tip, leading to something like --->- (though not as exaggerated). I suppose I can use shorten >=.5pt to trim off the excess. But I've never really liked using shorten >. It requires manually guessing the proper amount to shorten and needs to be tweaked whenever I change the arrow style, line thickness, etc. Not sure how to automate that better. –  Ben Liblit Feb 26 '13 at 23:30
    
Yes, I see your point. Indeed, a shorten >-.5pt will do the trick. It is up to you now if you want to do this. It looks to me like @percusse's solution is neater. I don't know though if you still have some other uses for mark connection node. But, then again, choose what you think is the best solution. :) –  hpesoj626 Feb 26 '13 at 23:47
    
I do like the way your approach neatly tucks everything away in a style. And I could get over my distaste for shorten >. But now I am seeing a different problem. If the line is supposed to curve, your solution instead draws two straight line segments. Try changing (A) -- (B) to (A) to [bend right] (B) and you should see what I mean. The label is correctly shifted downward, but the line segments from A to the label and from the label to B are both straight. Without label decorate, the line from A to B is curved as desired. –  Ben Liblit Feb 27 '13 at 0:05
    
@BenLiblit Perhaps this necessitates a separate question. –  hpesoj626 Feb 27 '13 at 0:24
    
bend right has the same problematic effect on edge. In both cases, mark connection node turns the bend into two straight segments. But I'm inclined to agree that this is a separate question. –  Ben Liblit Feb 27 '13 at 0:27

The path is not destroyed but also not available after the decoration application. In other words, the decoration uses the path just to plug in the node but the actual arrow placement does not have access to it anymore. So after the decoration applied the arrow placement code sees a zero length path and the arrow is misplaced.

A similar effect can be obtained by

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \node (A) at (0, 0) {A} ;
  \node (B) at (3, 0) {B} ;
\draw[->] (A) node[append after command={--(label)--(B)}] (label) at ($(A)!0.5!(B)$) {label};
\end{tikzpicture}
\end{document}

But if the path is complicated curve etc. marking with an additional arrow head is better.

enter image description here

share|improve this answer
    
Unfortunately I do indeed need a curved path. I omitted it from my initial example because I did not realize it would an extra difficulty, but I need the line from A to B to bend right. Do you know a way to do that? hpesoj626 posted a suggestion that marks with an additional arrow head, but that approach also breaks when using a bend right line from A to B. –  Ben Liblit Feb 27 '13 at 0:10

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.