7

I'm trying to understand how the coordinate system work for drawing pictures with tikz. I'm a little lost...

Here is a MWE that shows of some example code:

\documentstyle{article}

\begin{document}
\usetikzlibrary{shapes}
\usetikzlibrary{backgrounds,fit,shapes}
\usetikzlibrary{positioning}


\begin{tikzpicture}
  \tikzstyle{Y} = [rectangle, fill=orange!30!white]
  \tikzstyle{X} = [rectangle, fill=green!30!white]
  \tikzstyle{Other} = [rectangle, fill=blue!30!white]

\draw [help lines] (0,0) grid (100,10);

  \node [Y, anchor=south west, minimum width=200,minimum height=10] (Response) at (0,0) {};
  \node [below=of Response] {\small Variation in response};

  \node [X, anchor=south west, minimum width=75,minimum height=10] (Explanatory) at (0,0) {};
  \node [above=of Explanatory] {\small Explanatory};

  \node [Other, anchor=south west, minimum width=50,minimum height=10] (Other) at (75,0) {};
  \node [above=of Other] {\small Other};

\end{tikzpicture}

\end{document}

enter image description here

This is what was expecting, and wanting:

  • My first rectangle would start at coordinate (0,0) and end at (200, 10) because the width was set as 200 minimum, and height at 10 minimum, and the south west anchor was (0,0).
  • My second rectangle would start at (0,0), and end at (75, 10) for the same reason.
  • My third rectangle would start at (75, 0), because the rectangle is placed at (75, 0) and anchored "south west" like all others. But it doesn't even appear at all on my picture... it is way, way to the right (off the page). It starts somewhere I wasn't expecting.

That third point is where it breaks down for me. Can someone please explain? I'll try to explain my confusion further.

In the second rectangle, I assumed that the rectangle was starting at (0,0) and ending at (75, 10) because of the width and height I provide. But when I place the third rectangle at to start at (75, 0), it starts somewhere else; why is it somewhere different?

Probably an easy explanation, but I don't see it.

[I am trying to draw a rectangle, and then cut it into sub-rectangles. This is a MWE, so it is more involved than this, with things changing slide-by-slide in beamer. But that's the context.]

Thanks.

P.

  • Welcome! I don't know about "minimum width", but why don't you simply use \draw (x1,x2) rectangle (y1,y2); where x an y are opposite points of your rectangle? You can also check the filloption for \draw or the \filldrawcommand (e.g. \filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1)). – anderstood Jul 31 '14 at 3:55
  • Fair answer @anderstood. But I still want to know what my misunderstanding is in the above! – Pete Jul 31 '14 at 4:00
  • Use \begin{tikzpicture}[scale=0.1] to scale down the picture, you will see all. The coordinates are assumed to be cm by default. (100,10) is over the page size and that explains why you don't see the third image. Changing (100,0)/(75,0) to (100mm,0mm)/(75mm,0mm), you will see the third rectangle. – Jesse Jul 31 '14 at 4:11
  • I believe the problem is that you have specified minimum width=200, but have not specified units. I suspect it is defaulting to pt, but the coordinates are in cm. Try adding nodes below 10, 20`, etc to see where there are -- I think that should make it clear what is going on. – Peter Grill Jul 31 '14 at 4:29
  • Yes, minimum width and height are in pt by default. – Jesse Jul 31 '14 at 4:31
4

By default, tikz uses centimeters (cm) as unit. You are using dimensions of the order of 200 (meaning 200cm) resulting in your image going beyond page and part of the image is not seen. Try setting [x=1mm,y=1mm], specifying all dimensions in mm and changing (unrelated) all tikzstyles (deprecated) to tikzset brings the image within the page.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{backgrounds,fit,shapes}
\usetikzlibrary{positioning}
\tikzset{X/.style = {rectangle, fill=orange!30!white},
         Y/.style = {rectangle, fill=green!30!white},
         Other/.style = {rectangle, fill=blue!30!white}
}

\begin{document}



\begin{tikzpicture}[x=1mm,y=1mm]
\draw [thin,dashed][help lines,step=5mm] (0,0) grid (125,20);
\draw [help lines,step=10mm] (0,0) grid (125,20);

  \node [Y, anchor=south west, minimum width=100mm,minimum height=10mm] (Response) at (0,0) {};
  \node [below=of Response] {\small Variation in response};

  \node [X, anchor=south west, minimum width=75mm,minimum height=5mm] (Explanatory) at (0,0) {};
  \node [above=of Explanatory] {\small Explanatory};

  \node [Other, anchor=south west, minimum width=50mm,minimum height=5mm] (Other) at (75mm,1mm) {};
  \node [above=of Other] {\small Other};

\end{tikzpicture}

\end{document}

enter image description here

The dashed lines are 5mm apart from 0 to 125mm and the solid ones are 10mm apart. Hope this is helpful.

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.