[Tex/LaTex] Setting unit length in TikZ

contexttikz-pgf

As I understand it, the basic unit length in TikZ is 1cm, so that for example

\draw (0,0) -- (1,0);

draws a line 1cm in length. What I want to do is to redefine that length so that all drawings in my document are a different size. Using ConTeXt, how can I do this?

Best Answer

You can set the units length in TikZ for X, Y and even Z coordinates independently using the x, y and z keys:

\begin{tikzpicture}[x=2cm,y=1.5cm]
  ...

See the pgfmanual p.249 section 22.2 The XY- and XYZ-Coordinate Systems.

Note that TikZ actually doesn't have unit length, but uses vectors. The above keys therefore also accept (a,b) coordinates (which must be enclosed in { } to hide the ,). The X and Y unit vectors do not need to be orthogonal to each other. The above code is the short version of [x={(2cm,0cm)},y={(0cm,1.5cm)}].

You can also use the mentioned option scale as well, but I would prefer the above options. One difference will be that coordinates with explicit units are scaled as well with scale. For example (2cm,2) will be 3cm in both directions when scale=1.5 is set, but (2cm,3cm) with x=1.5cm,y=1.5cm.


One problem with the use of vectors is that you can't access the effective unit length even for the default setup of an XY (no Z) orthogonal coordinate system. I personally needed that for my tikz-timing package. You need to use PGF code for this: \pgfpointxy{1}{1} will give you the X and Y unit length as \pgf@x and \pgf@y. But note that these length registers will be overwritten by the next PGF or TikZ command.

Related Question