Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm having a hard time understanding how TikZ uses space

\documentclass{article}
\usepackage{tikz}
 \usetikzlibrary{positioning}
\begin{document}
\tikzset{red/.style={fill=red,circle,inner sep=0pt}}
\tikzset{green/.style={fill=green,circle,inner sep=0pt}}
\begin{tikzpicture}[x=1cm,y=1cm]
  \draw[help lines,step=.2] (-2,-1) grid (-1,0);
 \draw[help lines,line width=.5pt,step=1] (-2,-1) grid (2,1);
 \foreach \x in {-2,-1}
 \node[anchor=north] at (\x,-1) {\x};
 \foreach \y in {-1,0}
 \node[anchor=east] at (-2,\y) {\y};
 \node [rectangle,inner xsep=20mm,inner ysep=10mm,line width=.5pt](fr){};

 \node [red,anchor=center] at (fr.south west){};
 \node [green,anchor=center] at (-2,-1)(sw){};
 \node [green,right=2mm of sw.center](r){};
\end{tikzpicture}
\end{document}

1) If you compile the above code you'll see that the red dot, which is placed with a relative coordinate, and the green one which is placed with numerical coordinates do not overlap perfectly. Why is that?

2) the node labeled r is not placed where it should be but a bit on the right, even if the distance is specified from sw.center. Why?

share|improve this question
1  
I believe that the entire reason for this is the inner sep of a node. If you change xsep=20mm,inner ysep=10mm to inner xsep=0mm,inner ysep=0mm the two are aligned exactly on top of each other. So fr.south west is not (-2.-1). The two are offset by the diagonal of the two inner seps you added. Anohter possible confusion is that the anchor=center refers to the center of the new node you are placing, not the coordinate defined by the at option. –  Peter Grill Jun 24 '13 at 15:40
    
@PeterGrill it doesn't do what you describe here. Also if you set x=0 y=0 the reference rectangle collapses to its center –  marcinkus Jun 24 '13 at 15:44
    
The anchors are placed at the border of the line (which has a width), this is done by a default outer sep (outer xsep/outer ysep) of .5\pgflinewidth. Setting these to +0pt places the anchors in the middle of the line. See also [1]. The right key automatically also sets anchor=west (which is usually what you want), so you need to do right=2mm of sw.center,anchor=center. The on grid option makes this the default behavior. See also [2] and [3]. –  Qrrbrbirlbel Jun 24 '13 at 16:52

1 Answer 1

up vote 5 down vote accepted

In the placing of the red node, the line width is being taken into account. For the second green dot, the node border is being used as reference, you should use on grid to ignore that.

Sample zoomed output

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}

\tikzset{red/.style={fill=red,circle,inner sep=0pt}}
\tikzset{green/.style={fill=green,circle,inner sep=0pt}}

\begin{tikzpicture}[x=1cm,y=1cm]
  \draw[help lines,step=.2] (-2,-1) grid (-1,0);
  \draw[help lines,line width=.5pt,step=1] (-2,-1) grid (2,1);
  \foreach \x in {-2,-1}
    \node[anchor=north] at (\x,-1) {\x};
  \foreach \y in {-1,0}
    \node[anchor=east] at (-2,\y) {\y};
  \node [rectangle,inner xsep=20mm,inner ysep=10mm,line width=.5pt](fr){};

  \node [red,anchor=center] at ($(fr.south west)+(.25pt,.25pt)$) {};
  \node [green,anchor=center] at (-2,-1) (sw) {};
  \node [green,anchor=center,on grid,right=2mm of sw](r){};
\end{tikzpicture}
\end{document}
share|improve this answer
    
so when you place node relatively to another also the border of the target node is taken in consideration? On grid is like saying: "right= 22 of sw.center, put the center of the new node here", right? –  marcinkus Jun 24 '13 at 15:47
    
Yes, without on grid the border of the target is used. –  Andrew Swann Jun 24 '13 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.