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 created a simple node-edge graph in which I would like to have the texts on each edge aligned to the direction of the edge.

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                    semithick]
  \tikzstyle{every state}=[fill=red,draw=none,text=white]

  \node[state]         (D)                     {$D$};
  \node[state]         (C) [below right of=D]  {$C$};
  \node[state]         (B) [above right of=D]  {$B$};
  \node[state]         (A) [below right of=B]  {$A$};

  \path (D) edge              node {tdc} (C)            
        (B) edge              node {tbc} (C)
        (C) edge              node {tca} (A);
\end{tikzpicture}

\end{document}

For example, I would like the text tbc rotates -90 degrees automatically to be aligned to the direction of the edge connects the node B to the node C. Is that possible?

share|improve this question
    
Try adding the key sloped to the relevant nodes (PGF Manual Section 16.8). Does that do what you want? –  Loop Space Aug 17 '12 at 12:24
    
Thanks! That's the solution! I was trying to figure out how to use text along path... however, the text is not aligned to the center of edge. any solution? –  Rasoul Aug 17 '12 at 12:30

1 Answer 1

up vote 14 down vote accepted

The answer to the question of how to get the text to go along the path is to use the sloped key. This rotates the node to match the tangent of the path at the point at which the node would be placed (see Section 16.8 of the PGF Manual).

However, as noted in the comments, this doesn't quite work as desired. This is because of the auto key. This shifts the node off the path. It does so by placing one of the node anchors at the placement point, the anchor chosen appropriately for the tangent of the path (I haven't looked at the code so I'm guessing as to how it chooses it). The problem comes with the fact that the anchor is chosen first and then the node is rotated. What ought to happen is that the node is rotated and then the anchor is chosen. But actually, this doesn't need any complicated code since the anchor will always be one of north or south (if allow upside down is set then it will always be south). So simply setting anchor=south instead of auto will do.

Here's a simple example demonstrating the above analysis. The first three nodes are not sloped, the second three are. The first of the triples has no (other) options, the second is auto and the third is anchor=south. It's clear from the 2nd and 5th that the node has effectively been rotated around its anchor point.

nodes rotated to the path

Here's your code with this change (plus a couple of minor stylistic changes: tikz loads pgf automatically, and \tikzstyle is depreciated).

\documentclass{standalone}
%\url{http://tex.stackexchange.com/q/67552/86}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}[
  ->,
  >=stealth',
  shorten >=1pt,
  auto,
  node distance=2.8cm,
  semithick,
  every state/.style={fill=red,draw=none,text=white},
]
  \node[state]         (D)                     {$D$};
  \node[state]         (C) [below right of=D]  {$C$};
  \node[state]         (B) [above right of=D]  {$B$};
  \node[state]         (A) [below right of=B]  {$A$};

  \path[every node/.style={sloped,anchor=south,auto=false}]
        (D) edge              node {tdc} (C)            
        (B) edge              node {tbc} (C)
        (C) edge              node {tca} (A);
\end{tikzpicture}
\end{document}

(NB the auto=false isn't necessary as the anchor=south overrides the auto key)

And here's the result:

Questioner's picture with text along arrows

share|improve this answer
1  
Thanks for your detailed answer. It helped me to get it to work properly. –  Rasoul Aug 18 '12 at 16:42

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.