4

The following code (using the automata library in TikZ)

\documentclass[11pt]{article}

\usepackage{tikz,tkz-graph}
\usetikzlibrary{automata,positioning}


\begin{document}
    \begin{tikzpicture}
       \node[state] (p0) {$p_0$};
       \node[state] (p1) [right = of p0]{$p_1$};
       \node[state] (q) [above right = 1 and 0.5 of p0]{$q$};
    \end{tikzpicture}
 \end{document}

does not position the q node above the midpoint of p_0 and p_1, as I would like to.

  1. Why is that?
  2. How can I obtain the desired result referencing q from p_0?
1
  • Please, be so kind and extend your code sniped to complete small document (Minimal Working Example)!
    – Zarko
    Feb 7, 2017 at 12:38

2 Answers 2

5

Obtain desired position of q state relative to position of p0 is not simple. The easiest way is calculate it as follows:

enter image description here

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{automata,calc,positioning}

\begin{document}
\begin{tikzpicture}
   \node[state] (p0) {$p_0$};
   \node[state] (p1) [right= of p0]{$p_1$};
   \node[state] (q)  [above= of $(p0)!0.5!(p1)$]{$q$};
\end{tikzpicture}
\end{document}

Edit: Regarding your code, it doesn't work as (may people) expected. In case, that nodes are rectangle, than the q node is right=0.5 cm from p0.east and above=1 cm from p.north (see illustration with blue line on image below). This is not the same at circle (see red line in illustration below). At it are considered coordinates of rectangle inside circle:

enter image description here

in above image I already consider, that the distance of circle had to be bigger for

d - 2^{1/2}*d

where d = 2 r is circle diameter:

    \documentclass[11pt]{article}
    \usepackage{tikz}
    \usetikzlibrary{automata,calc,positioning}

    \begin{document}
\begin{tikzpicture}[node distance=2cm and 2cm, 
     rect node/.style={draw, minimum size=1cm},
     dash node/.style={draw=red, densely dashed, minimum size=0.707cm},
   every state/.style={minimum size=1cm}
   ]
   \node[rect node] (p0) {$p_0$};
   \node[rect node] (p1) [right= of p0]{$p_1$};
   \node[rect node] (q)  [above right= 1cm and 1cm of p0]{$q$};
   \draw[thick,blue] (p0.45) -| ++ (1,1);
%
   \node[state] (p0) {$p_0$};
   \node[state] (p1) [right= of p0]{$p_1$};
   \node[state] (q)  [above right= 1.293 cm and 1.293 cm of p0]{$q$};
   \draw[thick,red]  (p0.45) -| ++ (1.293,1.293);
% rectangle inside circle
   \node[dash node] {};
   \node[dash node,above right= 1.293 cm and 1.293 cm of p0] {};
\end{tikzpicture}
    \end{document}

In above explanation I didn't bother with centering od node q. knowing what is going on the determinbation of corect distances that q node be centered should be simple.

But all this complication can be omited, if you rearrange node position as follows:

\begin{tikzpicture}(1,1);
   \node[state] (p0) {$p_0$};
   \node[state] (q)  [above right=of p0]{$q$};
   \node[state] (p1) [below right=of  q]{$p_1$};
\end{tikzpicture}

which gives very similar result as is shown on the first image:

enter image description here

Hopefully, now I answer on your question :)

2
  • Thank you, your code works fine also for me. But, do you know why mine does not behave well?
    – suitangi
    Feb 7, 2017 at 13:18
  • @suitangi, see edit of my answer. hopefully it now answer your question.
    – Zarko
    Feb 7, 2017 at 14:45
5

Use polar coordinates.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata}
\begin{document}
  \begin{tikzpicture}
    \node[state] (p0) at (210:1) {$p_0$};
    \node[state] (p1) at (330:1) {$p_1$};
    \node[state] (q)  at ( 90:1) {$q$};
  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .