7

I have the following code (stripped down from a much larger one).

(I understand that there are numerous questions (and answers including this excellent one) on relative positioning of nodes, but they do not seem to solve my problem. Before you mark my question as duplicate, please make it sure that it really addresses my issue ie. use relative positioning and not absolute positioning.)

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\begin{document}

\begin{tikzpicture}[node distance=40.0mm, auto]
  \node[state] (3) {3}; 
  \node[state] (1) [above left=of 3]  {1}; 
  \node[state] (2) [above right=of 3] {2}; 
\end{tikzpicture}

\end{document}

The output looks like this.

enter image description here

I would like to move node 3 somewhat below its current position. I have tried many ways of doing this using relative positioning (and without using absolute positioning), without success. How do I do this?

Masroor
  • 17,842
  • Is a \node (1) [above left=5cm and 1cm of 3] {1}; satisfying your problem? This way you can set the above and the left|right individually from each other. – moospit Aug 22 '14 at 05:27
  • @moospit Afraid not. I have already seen those answers. I am somewhat determined (perhaps for no reason), to achieve the effect using relative positioning only. – Masroor Aug 22 '14 at 05:29
  • 1
    As you are using \begin{tikzpicture}[node distance=40.0mm], would it be a solution to different to horizontal and vertical node spacing using \begin{tikzpicture}[node distance=40.0mm and 2cm]? (2nd value is vertical spacing) – moospit Aug 22 '14 at 05:34
  • @moospit Yes, that is acceptable since you do not have to carry out manual positioning for every node you put in. – Masroor Aug 22 '14 at 05:37
  • For this kind of diagrams, a tikz matrix approach is more suitable where you can declare row separation and column separation in the beginning itself. –  Aug 22 '14 at 06:03
  • @HarishKumar Care to elaborate a little more? Or better yet, why not post a complete answer with your approach. Despite my twenty five years of experience with LaTeX, my experience with tikz is less than twenty five days. – Masroor Aug 22 '14 at 06:17
  • @HarishKumar I would like those sample codes, or as I said, would like more solution to my problem with the matrix approach you suggested. – Masroor Aug 22 '14 at 06:25
  • Masroor, I have added some sample code. Sorry for the delay as my internet is very slow. –  Aug 22 '14 at 08:20
  • Masroor, close this question by selecting an answer ... – Ricardo Cruz Feb 22 '15 at 21:48
  • @RicardoCruz Eventually, I used neither of the answers. – Masroor Feb 23 '15 at 00:56

2 Answers2

9

Just to sum up the above comments and get future readers an overview about the discussed answers:

Prerequisites:

\usetikzlibrary{positioning}

Per node distance definition:

\node (id) [below left=<x-value> and <y-value> of <reference>] {<text>};

Global distance definition:

\begin{tikzpicture}[node distance=<x-value> and <y-value>]
 \node (id) [below left=of <reference>] {<text>};
 % ...
moospit
  • 4,456
7

For this kind of diagrams where you are putting many nodes relative to one another, a matrix approach would be more suited. You have to use \usetikzlibrary{matrix} and declare the nodes as the elements of a matrix. The row and column separations, node styles etc can be declared via \tikzset. You can refer to those nodes using the name of the matrix say (m) like (m-1-1) where (m-2-3) 2 is the row and 3 is the column (indices).

Here is a sample code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,automata}   %% load the library

\begin{document}

\begin{tikzpicture}
  \tikzset{   %%% define the parameters for once
    mstyle/.style={column sep=4cm, row sep=6cm,nodes={state},font=\bfseries},
    line/.style={draw,very thick,blue,-latex},
  }
  \matrix(m)[matrix of nodes,ampersand replacement=\&,mstyle]{
    1 \&  \& 2  \\
      \& 3 \&   \\
    4 \& 5 \& 6  \\
  };
    \draw[line](m-1-1.east)-- (m-1-3.west);
\end{tikzpicture}

\end{document}

enter image description here

With row sep = 2cm in \mstyle, we get

enter image description here

And with our very technique \\[2cm], we get

\matrix(m)[matrix of nodes,ampersand replacement=\&,mstyle]{
    1 \&  \& 2  \\[2cm]
      \& 3 \&   \\
    4 \& 5 \& 6  \\

enter image description here

For more details, please refer to pgfmanual, page no 654 (in my copy of v3) section 57.

However, I accept that I am not aware of how your final diagram will be. Hence, you are the better judge of choices you have.