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?

10
  • 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
    Commented Aug 22, 2014 at 5: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
    Commented Aug 22, 2014 at 5: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
    Commented Aug 22, 2014 at 5:34
  • @moospit Yes, that is acceptable since you do not have to carry out manual positioning for every node you put in.
    – Masroor
    Commented Aug 22, 2014 at 5: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.
    – user11232
    Commented Aug 22, 2014 at 6:03

2 Answers 2

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>};
 % ...
1
  • Note that this code requires the positioning tikz library. Commented Aug 22, 2014 at 7:04
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.

You must log in to answer this question.

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