1

I have the following code:

\documentclass[a4paper, 11pt]{article}
\usepackage{comment} % enables the use of multi-line comments (\ifx \fi) 
\usepackage{fullpage} % changes the margin
\usepackage[swedish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{titlesec}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata}
\usepackage{color}


 \begin{document}
\begin{tikzpicture}[
        > = stealth, % arrow head style
        shorten > = 1pt, % don't touch arrow head to node
        auto,
        node distance = 2cm, % distance between nodes
        semithick % line style
    ]

    \tikzstyle{every state}=[
        draw = black,
        thin,
        fill = cyan!29,
        minimum size = 5mm
    ]

    \node[state] (a) {$a$};
    \node[state] (b)[below left=0.2cm and 0.2cm of a] {$b$};
    \node[state] (c)[below left=0.2cm and 0.2cm of b] {$c$};
    \node[state] (f)[below left=0.2cm and 0.2cm of c] {$f$};
    \node[state] (j)[below left=0.2cm and 0.2cm of f ] {$j$};
    \node[state] (d)[below left=0.2cm and 0.2cm of j] {$d$};
    \node[state] (i)[below left=0.2cm and 0.2cm of d] {$i$};

     \path[->] (a) edge node {} (b);
      \path[->] (b) edge node {} (c);
       \path[->] (c) edge node {} (f);
        \path[->] (f) edge node {} (j);
         \path[->] (j) edge node {} (d);
          \path[->] (d) edge node {} (i);
          \path[->] (b) edge[bend right=45, blue, very thick] node {} (j);


   \end{tikzpicture}
\end{document}

I would like the nodes to be more under each other, not so spread on the left. How can I achieve such a positioning? Thanks.

  • Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob Oct 20 '17 at 9:20
  • 1
    I have edited my question, thank you for the tipps! – Cherry Oct 20 '17 at 9:24
  • You have the answer in your code already I think, in below left=0.2cm and 0.2cm. One of those is the horizontal distance, the other is the vertical. (And negative distances are allowed.) – Torbjørn T. Oct 20 '17 at 9:35
4

You can adjust the parameters to your node distance, to e.g.

node distance=1cm and 0cm

The first is vertical offset, the second horizontal. Note that with below left you are attaching to a "lower left" corner of the node and even the and 0cm will give a displacement to the left. You can make this number negative, but if you want a vertical stack then you should just use below instead of below left:

Sample output

\documentclass[a4paper, 11pt]{article}

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

\begin{document}

\begin{tikzpicture}[ > = stealth, % arrow head style
        shorten > = 1pt, % don't touch arrow head to node
        semithick % line style
    ]

    \tikzstyle{every state}=[
        draw = black,
        thin,
        fill = cyan!29,
        minimum size = 7mm
    ]
    \begin{scope}[node distance=1cm and 0cm, every node/.style=state]
      \node (a) {$a$};
      \node (b) [below left=of a] {$b$};
      \node (c) [below left=of b] {$c$};
      \node (f) [below left=of c] {$f$};
      \node (j) [below left=of f] {$j$};
      \node (d) [below left=of j] {$d$};
      \node (i) [below left=of d] {$i$};
    \end{scope}
    \path[->] (a) edge (b)
              (b) edge (c)
              (c) edge (f)
              (f) edge (j)
              (j) edge (d)
              (d) edge (i)
              (b) edge[bend right=45, blue, very thick] (j);
\end{tikzpicture}
\hspace{2cm}
\begin{tikzpicture}[ > = stealth, % arrow head style
        shorten > = 1pt, % don't touch arrow head to node
        semithick % line style
    ]

    \tikzstyle{every state}=[
        draw = black,
        thin,
        fill = cyan!29,
        minimum size = 7mm
    ]
    \begin{scope}[node distance=1cm, every node/.style=state]
      \node (a) {$a$};
      \node (b) [below=of a] {$b$};
      \node (c) [below=of b] {$c$};
      \node (f) [below=of c] {$f$};
      \node (j) [below=of f] {$j$};
      \node (d) [below=of j] {$d$};
      \node (i) [below=of d] {$i$};
    \end{scope}
    \path[->] (a) edge (b)
              (b) edge (c)
              (c) edge (f)
              (f) edge (j)
              (j) edge (d)
              (d) edge (i)
              (b) edge[bend right=45, blue, very thick] (j);
\end{tikzpicture}

\end{document}
4

You can automate all the procedure like this:

\documentclass[a4paper, 11pt]{article}
\usepackage{comment} % enables the use of multi-line comments (\ifx \fi) 
\usepackage{fullpage} % changes the margin
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{titlesec}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata}
\usepackage{color}

\xdef\mybelow{0.4cm} %< Select distance for below
\xdef\myleft{0.12cm} %< Select distance for left

 \begin{document}
\begin{tikzpicture}[
        > = stealth, % arrow head style
        shorten > = 1pt, % don't touch arrow head to node
        auto,
        node distance = 2cm, % distance between nodes
        semithick % line style
    ]

    \tikzstyle{every state}=[
        draw = black,
        thin,
        fill = cyan!29,
        minimum size = 5mm
    ]


    \foreach  \letter [count=\i from 1] in {a,b,c,f,j,d,i}{
    \ifnum\i=1
    \xdef\myprevpoint{\letter}
    \node[state] (\letter) {$\letter$};
    \else
    \node[state] (\letter)[below left=\mybelow and \myleft of \myprevpoint] {$\letter$};
    \path[->] (\myprevpoint) edge node {} (\letter);
    \xdef\myprevpoint{\letter}
    \fi
    }
   \path[->] (b) edge[bend right=45, blue, very thick] node {} (j);

   \end{tikzpicture}
\end{document}

Output:

enter image description here

tikz is a powerful package and it is better to automate things so that it could be easier to make your manipulations in the future (if papper size has to be changed or whatever)

  • In the above code you have just to change the list of the letters in the \foreach command to get your next diagram with other letters. Also by changing the values of \mybelow and \myleft you an adjust the distances for every diagram through this code. – koleygr Oct 20 '17 at 10:15

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.