[Tex/LaTex] TikZ – Adjusting the Node distance for Individiual nodes

tikz-pgf

I am drawing E-R diagrams using the tikz-er2 package. In the begintikzpicture environment, there is an optional argument node distance. This is global for all the nodes. Now, If I give a small distance, The nodes intersect and if I give an average distance, the nodes appear too far. How do I set the distance for individual nodes? The tikz-er2 package sty file and user manual can found here. Here is a MWE:

\documentclass[12pt,draft]{article}
\usepackage{tikz-er2}
\begin{document}
\tikzstyle{every entity} = [draw=blue,fill=blue!20,text=red]
\tikzstyle{every attribute} = [fill=yellow!20]
\tikzstyle{every relationship} = [fill=red!20]
\tikzstyle{every edge} = [link]
\begin{tikzpicture}[node distance=8em]
    \node[entity] (student) {Student};
    \node[attribute] (stud-id) [left of=student] {\key{student-id}} edge (student);
    \node[attribute] (name) [above left of=student] {Name} edge (student);
    \node[attribute] (fname) [above left of=name] {First-Name} edge (name);
    \node[attribute] (lname) [above right of=name] {Last-Name} edge (name);
    \node[attribute] (cgpa)  [above right of=student] {CGPA} edge (student);
    \node[relationship] (joins) [right of=student] {joins} edge [total] (student);
    \node[entity] (pgroup) [right of=joins] {Project-Group} edge [<-] (joins);
\end{tikzpicture}
\end{document}

I have enabled the draft mode. It seems there is a bad box. How do I remove that too?

enter image description here

Best Answer

You can use positioning library of tikz and control the distance between each node pair individually.

\documentclass[12pt,draft]{article}
\usepackage{tikz-er2}
\usetikzlibrary{positioning}
\begin{document}
\tikzstyle{every entity} = [draw=blue,fill=blue!20,text=red]
\tikzstyle{every attribute} = [fill=yellow!20]
\tikzstyle{every relationship} = [fill=red!20]
\tikzstyle{every edge} = [link]
\begin{tikzpicture}%[node distance=8em]
    \node[entity] (student) at (-2,0) {Student};
    \node[attribute] (stud-id) [left = 0.5cm of student] {\key{student-id}} edge (student);
    \node[attribute] (name) [above left = 0.75cm and 0cm of student] {Name} edge (student);
    \node[attribute] (fname) [above left = 0.75cm and 0cm of name] {First-Name} edge (name);
    \node[attribute] (lname) [above right = 0.75cm and 0cm of name] {Last-Name} edge (name);
    \node[attribute] (cgpa)  [above right =0.75cm and 0cm of student] {CGPA} edge (student);
    \node[relationship] (joins) [right = 0.5cm  of student] {joins} edge [total] (student);
    \node[entity] (pgroup) [right = 0.5cm of joins] {Project-Group} edge [<-] (joins);
\end{tikzpicture}
\end{document}

enter image description here

Adjust the distances to suit your needs.

Related Question