2

I want to draw several nodes below each other. Therefore I use relative positioning with a node alias. It seems the positions are out of alignment the more nodes are drawn. This is what I tried:

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

\begin{document}

\begin{tikzpicture}[
  unit/.style={draw, circle, inner sep=0pt, minimum size=10mm}
]

\draw[black!10, very thin] (0, -4) grid [step=1mm] +(4, 8);
\draw[help lines] (0, -4) grid [step=10mm] +(4, 8);
\filldraw (0,0) circle [radius=2pt];

\node[unit] (c1) at (0,3) [alias=last] {};

\foreach \i in {2,...,5}
  \node[unit] (c\i) [alias=last, below=5mm of last] {};

\filldraw[gray!50] (c3) circle [radius=2pt];

\end{tikzpicture}

\end{document}

And this is how it looks (note the yellow marked spots):

nodes

I want to have the circles aligned to the grid. Any ideas on how to fix this?

3 Answers 3

5

If you include outer sep=0pt,

unit/.style={draw, circle, inner sep=0pt, minimum size=10mm, outer sep=0pt,}

the result are as desired.

enter image description here

Code:

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

\begin{document}

\begin{tikzpicture}[
  unit/.style={draw, circle, inner sep=0pt, minimum size=10mm, 
      outer sep=0pt,% <-- added
  }
]

\draw[black!10, very thin] (0, -4) grid [step=1mm] +(4, 8);
\draw[help lines] (0, -4) grid [step=10mm] +(4, 8);
\filldraw (0,0) circle [radius=2pt];

\node[unit] (c1) at (0,3) [alias=last] {};

\foreach \i in {2,...,5}
  \node[unit] (c\i) [alias=last, below=5mm of last] {};

\filldraw[gray!50] (c3) circle [radius=2pt];

\end{tikzpicture}
\end{document}
0
4

@PeterGrill answered earlier but I was preparing illustrations. ;-) So, here is the important thing: node anchors are placed at outer sep from the middle line of the node border.

The border middle line of your c1 circle has its high and low points at ordinates that are multiples of 5 (starting from the origin at (0,0), using 1mm as unit length). This is an ideal circle with no thickness. When TikZ draws the border, there is ink on both sides, in equal thickness:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[line width=3pt, draw opacity=0.3]

\draw[black!10, very thin] (0, -4) grid [step=1mm] +(4, 8);
\draw[help lines] (0, -4) grid [step=10mm] +(4, 8);
\filldraw (0,0) circle [radius=2pt];

\node (c1) at (0,3)
  [draw=blue, alias=last, circle, inner sep=0pt, minimum size=10mm, draw] {};
\fill[red!90!black] (c1.south) circle[radius=0.4pt]
  node[inner sep=0pt, below=0pt of c1.south, line width=0pt] {c1.south};
\end{tikzpicture}

\end{document}

enter image description here

Why is c1.south precisely at the lower limit of the ink here? Because by default, /pgf/outer sep is half the line width. If we set outer sep=3pt (same as the line width) after draw opacity=0.3 in this example, we get the following, because this starts from the middle line of the blue stripe whose width is the line width:

enter image description here

And if we use outer sep=0pt, c1.south is spot on this middle line (ordinate that is a multiple of 5). And this is enough to fix your shifting issue.

enter image description here

2
  • Thanks! Coming from CSS inner and outer sep seem similar to the margin and padding Jun 6, 2022 at 20:03
  • Yes, padding in CSS is similar to inner sep and margin to outer sep (the TikZ & PGF manual explicitly mentions this, see §17.2.3, Common Options: Separations, Margins, Padding and Border Rotation).
    – frougon
    Jun 6, 2022 at 20:22
2

Some explanation on Peter Grill's answer.

Suppose \i is 2, then in \node[unit] (c\i) [alias=last, below=5mm of last] {}; the resulted distance between coordinates last.center and c2.center is

radius_last + outer_sep_last + 5mm + outer_sep_c2 + radius_c2 = 15mm + .4pt

in which

  • radius_last = radius_c2 = 5mm and
  • outer_sep_last = outer_sep_c2 = line_width / 2 = .2pt

Here outer_sep is controlled by key outer sep which is initially half the line width. And the line_width is controlled by key line width which is initially .4pt.

0

You must log in to answer this question.

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