5

Regarding TikZ-images: When should I type positioning values attached with a unit (e.g. \draw (0cm, 0cm)...) and when should I type them without a unit (e.g. simply \draw (0, 0)...)?


Minimum Working Example No. 1:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture} 
           \draw (0, 0) rectangle (4, 4);
    \end{tikzpicture}
\end{document}

Minimum Working Example No. 2:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture} 
       \draw (0cm, 0cm) rectangle (4cm, 4cm);
    \end{tikzpicture}
\end{document}

Screenshot of the result from MWE no. 1:

Screenshot of MWE no. 1


Screenshot of the result from MWE no. 2:

Screenshot of MWE no. 2


As you can see, both codes will create the completely same picture. Therefore: What is the advantage of \draw (0cm, 0cm)... over \draw (0, 0)... and vice versa?

  • 1
    default unit in tikz (with some exception, for example at xshift, yshift) is cm, so, if you stick with it you not need to write it explicit. – Zarko Apr 6 at 19:55
  • 2
    TikZ interprets (x,y) as x*e_1+y*e_2, where the defaults for the unit vectors e_i are e_1=(1cm,0) and e_2=(0,1cm). The perhaps clearest explanation can be found in this answer by LoopSpace, which was written at a time when Jake still had questions. And I disagree with @Zarko, the default unit for TikZ is pt. – marmot Apr 6 at 23:06
6

These two expressions are in principle very different and give "accidentally" the same result. IMHO the clearest discussion of this site can be found in this nice answer by LoopSpace, out of which I recycle some relevant parts here. TikZ interprets (x,y) rather differently depending on whether or not x and y carry units.

  1. If they are dimensionless, then the coordinate (x,y) means x times unit vector in x direction plus y times unit vector in y direction.
  2. If they carry units, then it just means x to the right and y up.

By default, the unit vector in x direction is (1cm,0) and the unit vector in y direction is (0,1cm), such that for two dimensionless numbers x and y in the default settings (x,y) and (xcm,ycm) yield the same result, which is what your MWEs illustrate. However, if we change the basis vectors, this is no longer true, as the following example shows (using a rectangle may not be the best example to illustrate these issues, but at least it is simple).

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[font=\sffamily] 
        \begin{scope}[local bounding box=standard]
           \begin{scope}[local bounding box=without units 1]
            \draw (0, 0) rectangle (4, 4);
           \end{scope}
           \node[above] at (without units 1.north){without units};
           \begin{scope}[xshift=5cm,local bounding box=with units 1]
            \draw (0cm, 0cm) rectangle (4cm, 4cm);
           \end{scope}
           \node[above] at (with units 1.north){with units};
        \end{scope}   
        \node[rotate=90,above=2em] at (standard.west){standard unit vectors};
        \begin{scope}[local bounding box=nonstandard,
        yshift=-5cm,x={(0.75,0.25)},y={(0,0.8)}]   
           \begin{scope}[local bounding box=without units 2]
            \draw (0, 0) rectangle (4, 4);
           \end{scope}
           \node[above] at (without units 2.north){without units};
           \begin{scope}[xshift=5cm,local bounding box=with units 2]
            \draw (0cm, 0cm) rectangle (4cm, 4cm);
           \end{scope}
           \node[above] at (with units 2.north){with units};
        \end{scope}   
        \node[rotate=90,above=2em] at (nonstandard.west){nonstandard unit vectors};
    \end{tikzpicture}
\end{document}

enter image description here

Internally pgf uses pt as units, which is why the pgf key xshift=2 yields a shift by 2pt (as remarked by Zarko). However, this does not mean that the radii of circles get interpreted as pt, rather, as explained in this nice answer by LoopSpace the command \draw[ultra thick] (0,0) circle[x radius=2,y radius=2]; yields, in the default coordinate system, a circle of radius 2cm.

So a possible answer to the question

When should I type positioning values attached with a unit (e.g. \draw (0cm, 0cm)...) and when should I type them without a unit (e.g. simply \draw (0, 0)...)?

is

It depends on what you need and/or are doing. In many situations you install nonstandard coordinate systems for a reason, which is why you may not want to add cm when dealing with them.

Side-remark: the tikz-3dplot package adds cm to all coordinates, which can lead to confusion in some cases.

  • 5
    Complement: And when we mix (2,3cm) we obtain 2*x+(0cm,3cm) where x is the x-base vector. But if we mix inside the same component like (1+1cm,3) then 1 is used with the default pt unit, so is equivalent to (1pt+1cm,0cm) + 3*y. – Kpym Apr 7 at 5:39
  • @Kpym I said nothing that would contradict that, There is a sheer endless list of such examples. – marmot Apr 7 at 14:32
2

In complement to marmot's nice answer, the following picture may be helpful:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[x={(2cm,2cm)}, y={(0cm,2cm)},
                    every node/.style={inner sep=2pt}]
  \draw[blue, ->] (0,0) -- (1,0) node[below right] {$\vec{x}$};
  \draw[blue, ->] (0,0) -- (0,1) node[above]       {$\vec{y}$};

  \begin{scope}[shift={(1,0)}]  % very different from shift={(1cm,0)}
  \draw[red, ->] (0cm,0cm) -- (1cm,0cm) node[right] {$\vec{u}$};
  \draw[red, ->] (0cm,0cm) -- (0cm,1cm) node[above] {$\vec{v}$};
  \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

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.