7

I trying to place a node under another node, but that doesn't seem to be possible.

\documentclass{article}

\usepackage{pgf}
\usepackage{tikz}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,shadows.blur,positioning,calc,arrows.meta,automata}    \usepackage[latin1]{inputenc}
\usepackage{verbatim}

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

            \node[state] (A_1)                      {Sunny};
            \node[state] (B_1) [below of = A_1]     {Cloudy};
            \node[state] (C_1) [below of = B_1]     {Rainy};

            \node[state] (A_2) [right of = A_1]     {Sunny};
            \node[state] (B_2) [right of = B_1]     {Cloudy};
            \node[state] (C_2) [right of = C_1]     {Rainy};

            \node[state] (A_3) [right of = A_2]     {Sunny};
            \node[state] (B_3) [right of = B_2]     {Cloudy};
            \node[state] (C_3) [right of = C_2]     {Rainy};

            \tikzstyle{every state}=[fill=blue,draw=none,text=white]

            \node[state] (O1) [below of = A_3]      {dry};
            %\node[state] (O2) [right of = O1]      {damp};        
            %\node[state] (O3) [right of = O2]      {soggy};

            \path  (A_1) edge   node {}  (A_2)
                   (A_1) edge   node {}  (B_2)
                   (A_1) edge   node {}  (C_2)

                   (B_1) edge   node {}  (A_2)
                   (B_1) edge   node {}  (B_2)
                   (B_1) edge   node {}  (C_2)

                   (C_1) edge   node {}  (A_2)
                   (C_1) edge   node {}  (B_2)
                   (C_1) edge   node {}  (C_2)

                   (A_2) edge   node {}  (A_3)
                   (A_2) edge   node {}  (B_3)
                   (A_2) edge   node {}  (C_3)

                   (B_2) edge   node {}  (A_3)
                   (B_2) edge   node {}  (B_3)
                   (B_2) edge   node {}  (C_3)

                   (C_2) edge   node {}  (A_3)
                   (C_2) edge   node {}  (B_3)
                   (C_2) edge   node {}  (C_3);


        \end{tikzpicture}        
        \caption{Example of Markov chain}
        %\label{fig:ex-markov}
    \end{figure}

\end{document}

I am trying to place

\node[state] (O1) [below of =of A_3]      {dry};

But somehow is the state not placed under A_3, but on C_2 instead.

enter image description here

This is how it looks like, and the red line shows where i want it to be. Not the exact distance but just below A_3, using the fixed distance.

8
  • 3
    What's the second of?
    – TeXnician
    Feb 22, 2017 at 15:40
  • 1
    @TeXnician is right. Without the second "of" it works. Use \node[state] (O1) [below of = A_3] {dry};
    – Bobyandbob
    Feb 22, 2017 at 16:06
  • The error message says Package pgf Error: No shape named of A_3 is known., and if you read it carefully, you'll see that TikZ looks for a node called of A_3, which clearly isn't what you meant, and is a hint at what's wrong. Feb 22, 2017 at 16:22
  • @TeXnician I guess it might have been a typo, but it is still positioned incorrectly.. it should be below A_3, but places on C_2 node...
    – J.Down
    Feb 22, 2017 at 18:09
  • @Torbjørn T. I just removed the second of, but didn't notice any error message before..
    – J.Down
    Feb 22, 2017 at 18:09

2 Answers 2

3

Given the position of the red line, you clearly want

below of=C_1

rather than

below of=A_3

Otherwise, the node will be placed below A_3 i.e. on top of B_3.

You seem to be under the impression that your nodes are arranged like this:

A_1 B_1 C_1
A_2 B_2 C_2
A_3 B_3 C_3

but you have drawn them in this way:

A_1 A_2 A_3
B_1 B_2 B_3
C_1 C_2 C_3
1
  • ahh... Yep.. That was a stupid mistake.
    – J.Down
    Feb 23, 2017 at 0:19
11

Have a look at the TikZ manual "17.5.3 Advanced Placement Options". The keys below of, above of etc, have been deprecated in favour of the more flexible below, above etc that are available with the positioning tikz library.

As an example, you can write [below=of A_3] and even [below=2 of A_3] to specify a distance.

Note that, unless you specify the on grid option, the distance between the nodes will be the length of the space between them, not the distance between their centers.

EDIT: After reading the comments I realised the problem may be a misinterpretation of the effect of the option below: the effect of below=of A is to place the node below A with a distance between their edges of 1 (the exact measure of which depends on scaling etc but defaults to 1cm). Now, it seems you are expecting dry to be placed below A but very close to it. That is what would have happened with a label=below:dry style: \node[state] (A_3) [right=of A_2,label={[state]below:dry}] {Sunny};. You can also specify a distance, try below=.1 of A. In case this is not the problem, try being more specific and report exactly the nature of the issue (i.e. what does "does not like the usage of below" mean? Do you get an error?)

5
  • 1
    +1, that's exactly what I wanted to add. @J.Down see here: tex.stackexchange.com/a/9391/101651!
    – CarLaTeX
    Feb 22, 2017 at 16:17
  • I just tried... i am using share latex, and it doesn't like the usage of below and above.
    – J.Down
    Feb 22, 2017 at 18:10
  • @J.Down Did you add \usetikzlibrary{positioning}? Feb 22, 2017 at 20:05
  • @TorbjørnT. Yes
    – J.Down
    Feb 22, 2017 at 22:37
  • 1
    @J.Down I've just put you mwe into ShareLaTeX, added positioning and \usepackage{float}, changed all the of = with = of and it works perfectly. (BTW, you've loaded tikz twice and probably you don't need a float if your option is H...).
    – CarLaTeX
    Feb 23, 2017 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.