# How to define coordinates in Tikz?

I'm having a hard time figuring out how Tikz decides how to map the 2d coordinates I give it to the area I've given it to draw in. Sometimes it seems like it will auto-size based on what I've drawn inside of it, leading me to draw a border to define my extents, but other times it seems to not follow that rule.

It's very confusing and it's pretty difficult to find documentation. I've found a few multiple hundred page pdf's but they seem to miss a lot of commands - like tkzInit which looks like it might do what I want, but I can't find documentation to confirm that, and my tests have shown that it doesn't do what I want!

For instance, I would expect this to draw 4 squares, but instead it draws 4 rectangles that are twice as long as they are wide.

    \begin{tikzpicture}[x=8cm,y=4cm]
\tkzInit[xmax=2,ymax=1,xmin=0,ymin=0]
% border
\draw (-0.1,-0.2) rectangle (2,1.2);

\foreach \x in {0,...,4}
\draw (\x * 0.1 - 0.05,0.45) rectangle (\x * 0.1 + 0.05, 0.55);

\end{tikzpicture}


In this case, what I would like is to have my window be twice as wide as it is high, and internally use the coordinates (0,0) to (2,1). I'd also possibly like to pad the edges a bit and add a border.

Anyone able to tell me what I'm doing wrong?

Thank you!!

• x=8cm says, I think, that each unit in the x direction is 8cm. Similarly for y. So (1,1) will be 8cm to the right of the origin and 4cm above it. Hence, (0,0) rectangle (1,1) will be a rectangle rather than a square. – cfr Jun 25 '15 at 14:23
• Oh interesting. I thought that defined width and height. I'll experiment more thanks! – Alan Wolfe Jun 25 '15 at 14:25

## 3 Answers

Apart from what cfr said about [x=8cm,y=4cm] that sets the step for the x and y axis, There is a much easier way to do the same thing (red is mine).

## Output ## Code

 \documentclass[margin=10pt]{standalone}
\usepackage{tikz}%, tkz-euclide}
%\usetkzobj{all}

\begin{document}
\begin{tikzpicture}%[x=8cm,y=4cm]
%\tkzInit[xmax=2,ymax=1,xmin=0,ymin=0]
% border
% \draw (-0.1,-0.2) rectangle (2,1.2);
\begin{scope}[x=1mm, y=1mm]
\foreach \x in {0,...,4}{
\draw[red] (\x,0) rectangle ({\x+1},1);
}
\end{scope}

\begin{scope}[xshift=.5mm]
\foreach \x in {0,...,4}
\draw (\x * 0.1 - 0.05,0.25) rectangle (\x * 0.1 + 0.05, 0.35);
\end{scope}
\end{tikzpicture}
\end{document}

• so many good answers, each with a different valuable nugget of information, hard to choose one :P – Alan Wolfe Jun 25 '15 at 15:43
• If you really want it simple \draw[scale=.1] grid (5,1); is better then ;) – Kpym Jun 25 '15 at 16:48
• @Kpym Eheh true, but it depends on the use you have to make of it, also considering he's using a \foreach. :P – Alenanno Jun 25 '15 at 17:06

Your rectangles come from the first line, where you write [x=8cm,y=4cm]. This is redefining the units used for x and y. Retry your code with [x=4cm,y=4cm]or even removing it completely and see what happens. I took the liberty of changing your rectangle defining code, using relative coordinate computations, with ++(0.1,0.1). This way less thinking goes into the computation of the required coordinates for the squares.

\begin{tikzpicture}[x=4cm,y=4cm]
\tkzInit[xmax=2,ymax=1,xmin=0,ymin=0]
% border
\draw (-0.1,-0.2) rectangle (2,1.2);

\foreach \x in {0,...,4}
\draw (\x * 0.1 - 0.05,0.45) rectangle ++(0.1,0.1);
\end{tikzpicture}


The use of /tikz/x=<dimension> and /tikz/y=<dimension> is documented in section 25.2 of the TikZ manual (p. 358-9 currently).

Roughly, x=8cm says that each unit in the x direction is 8cm. Similarly for y. So if you pass x=8cm, y=4cm to the tikzpicture environment, then (1,1) will be 8cm to the right of the origin and 4cm above it. Hence, (0,0) rectangle (1,1) will be a rectangle rather than a square. To get a square, you would need, say, (0,0) rectangle (.5,1) to compensate for the different sizes of the units you've specified.

Possibly what you want is something more like this. (I've commented out the \tkzInit command as I've no idea where that is from.)

\documentclass[12pt,tikz]{standalone}

\begin{document}
\begin{tikzpicture}
%   \tkzInit[xmax=2,ymax=1,xmin=0,ymin=0]% what package or library defines this?
% window
\path (-4cm,-2cm) rectangle (4cm,2cm);
% border
\draw (-0.1,-0.2) rectangle (2,1.2);

\foreach \x in {0,...,4}
\draw (\x * 0.1 - 0.05,0.45) rectangle (\x * 0.1 + 0.05, 0.55);

\end{tikzpicture}
\end{document} More likely, you either want to specify, say, x=4cm, y=4cm or x=8cm, y=8cm to scale the picture within the window up, or you want to change the coordinates within the picture so that you don't have such a large border. And likely the image should be centred as well.

You might like to use the backgrounds library for this. In the following, I've put the picture in an \fbox just to show where the border of the 'window' is in comparison with the drawn border, which is shown in green. Blue is used for drawing within the picture proper.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\fbox{\begin{tikzpicture}[inner frame sep=5pt, draw=blue, background rectangle/.style={draw=green}, show background rectangle]
%   \tkzInit[xmax=2,ymax=1,xmin=0,ymin=0]% what package or library defines this?

\foreach \x in {0,...,4}
\draw (\x * 0.1 - 0.05,0.45) rectangle (\x * 0.1 + 0.05, 0.55);

\end{tikzpicture}}
\end{document} 