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 am trying to draw state transitions using tikz, and place the reasons for those transitions on the arrows between states. The problem is that the text is not aligned on the arrows, and while in and of itself that is not terrible, things like the following happen (which are).

\documentclass[12pt]{article}
    \usepackage{tikz}
    \usetikzlibrary{automata,positioning,arrows}
    \begin{document}
        \begin{tikzpicture}[shorten >=2pt,node distance=4cm,on grid,auto]
            \node[state] (Rd) {Ready};
            \node[state] (Rn) [above right =of Rd] {Run};
            \node[state] (Bl) [below right =of Rd] {Blocked};
            \node[state] (Nr) [below right =of Rn] {Dead};
            \path[->]
            (Rd)    edge  node {Given a timeslice}     (Rn)
            (Rd)    edge  node  {Asks for I/O}         (Bl)
            (Rd)    edge  node {Gets killed by kernel} (Nr)
            (Bl)    edge  node {Out of memory}         (Nr);
        \end{tikzpicture}
    \end{document}

enter image description here

Adding the [midway,sloped] option to the path options gives the follow, so its closer, but still wrong:

enter image description here

How can I get the text to autmatically rotate to be "on" the arrow (just above or below and parallel, there will be no loops if that matters).

I looked at Curved text (on multiple lines) within curved arrow using tikz, but was unable to get it to work in may case.

share|improve this question

1 Answer 1

up vote 9 down vote accepted

You can use options sloped, anchor=center:

enter image description here

Code:

\documentclass[12pt]{article}
    \usepackage{tikz}
    \usetikzlibrary{automata,positioning,arrows}
    \begin{document}
        \begin{tikzpicture}[shorten >=2pt,node distance=4cm,on grid,auto]
            \node[state] (Rd) {Ready};
            \node[state] (Rn) [above right =of Rd] {Run};
            \node[state] (Bl) [below right =of Rd] {Blocked};
            \node[state] (Nr) [below right =of Rn] {Dead};
            \path[->]
            (Rd)    edge  node[sloped, anchor=center, above, text width=2.0cm] { Given a timeslice}     (Rn)
            (Rd)    edge  node[sloped, anchor=center, below] {Asks for I/O}          (Bl)
            (Rd)    edge  node {Gets killed by kernel} (Nr)
            (Bl)    edge  node {Out of memory}         (Nr);
        \end{tikzpicture}
    \end{document}
share|improve this answer
    
Is there a way to either lengthen the arrows automatically or make the text multiline automatically (if it were to hit the nodes)? –  soandos Feb 21 '13 at 6:21
    
@soandos: I personally don't know of an automated way to extend the node, but that might make a good follow on queestion. In the example above I used text width to auto wrap the timeslice label, which is pretty easy to do manually. –  Peter Grill Feb 21 '13 at 6:30

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.