Graphics, Figures & Tablestikz node distance: spacing larger than expected

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
jess-elzbth
Posts: 7
Joined: Thu Sep 01, 2016 8:12 pm

tikz node distance: spacing larger than expected

Post by jess-elzbth »

I am using the command node distance. When I generate help lines the spacing between the nodes is not what I expected. I have many ideas as to why this is occurring, but I'm not sure which questions to ask to get the answer I actually need.

Also, in node distance why is the vertical, or y-axis, spacing given as an argument before the horizontal, or x-axis, spacing? My math mind is having a hard time remembering that one.
node-distance.png
node-distance.png (9.66 KiB) Viewed 24303 times

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}
	[inner sep=0.5mm,
	nofill/.style={circle,draw=cyan},
	filled/.style={circle, draw=cyan, fill=cyan}]

	\begin{scope}[node distance=1cm and 0.1cm]
	\draw[help lines,step=5mm,gray!20] (-1,1) grid (6,-2);
%Text 1
		\node (t) at (0,0) {Text 1};
		\node [filled, right=of t] (t1) {};
		\node [filled, right=of t1] (t2) {}; 
		\node [nofill, right=of t2] (t3) {};
		\node [nofill, right=of t3] (t4) {};
		\node [nofill, right=of t4] (t5) {};
%Text 2
		\node [filled, below=of t1] (j1) {}; 
		\node [left= of j1](j) {Text 2};
		\node [filled, right=of j1] (j2) {}; 
		\node [filled, right=of j2] (j3) {};
		\node [nofill, right=of j3] (j4) {};
		\node [nofill, right=of j4] (j5) {};
%Text 3
		\node (w) at (3.5,0) {Text 3};
		\node [filled, right=of w] (w1) {};
		\node [filled, right=of w1] (w2) {}; 
		\node [nofill, right=of w2] (w3) {};
		\node [nofill, right=of w3] (w4) {};
		\node [nofill, right=of w4] (w5) {};
%Text 4
		\node [filled, below=of w1] (e1) {}; 
		\node [left= of e1](e) {Text 4};
		\node [filled, right=of e1] (e2) {}; 
		\node [filled, right=of e2] (e3) {};
		\node [nofill, right=of e3] (e4) {};
		\node [nofill, right=of e4] (e5) {};
	\end{scope}
\end{tikzpicture}
	
\end{document}
Please forgive my terrible names; it makes more sense in my actual code.

Side question: Could anyone suggest a different way to achieve what I've done? Thank you for your help.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

tikz node distance: spacing larger than expected

Post by Stefan Kottwitz »

Hi,

the node distance is the distance between the borders of nodes by default, that is, if the on grid option is set to false (default). If on grid is set, it's the distance between the centers of the nodes.

Perhaps you look for

\begin{scope}[node distance=1cm and 0.5cm,on grid]

that gives an y-distance of 1 cm between node centers, and an x-distance of 0.5 cm between node centers.
dots.png
dots.png (12.56 KiB) Viewed 24296 times
Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

tikz node distance: spacing larger than expected

Post by Stefan Kottwitz »

For positioning in a grid structure, there's also the handy matrix syntax.

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
  [ inner sep=0.5mm,
    nofill/.style={circle,draw=cyan},
    filled/.style={circle, draw=cyan, fill=cyan}]
  \matrix (m) [ matrix of nodes, row sep=1cm, column sep = 0.5cm,
            nodes = {anchor=center}] {
    Text1 & |[filled]| & |[filled]| & |[nofill]| & |[nofill]| & |[nofill]| \\
    Text2 & |[nofill]| & |[nofill]| & |[nofill]| & |[filled]| & |[filled]| \\
  };
\end{tikzpicture}
\end{document}
matrix.png
matrix.png (7.42 KiB) Viewed 24295 times
I gave the matrix the name m. Now you can access the matrix nodes with already predefined names, (m-row-column), such as by:

\draw (m-1-2) -- (m-2-6);

Stefan
LaTeX.org admin
User avatar
jess-elzbth
Posts: 7
Joined: Thu Sep 01, 2016 8:12 pm

Re: tikz node distance: spacing larger than expected

Post by jess-elzbth »

Thank you Stefan for both of your clear explanations. I knew there was a much better way to achieve a grid structure; a matrix is perfect! :D
Post Reply