1

I'm a beginner in TikZ trying to understand the coordinate system. Why do the first two examples give me the same result (a line close to the text, without the 1cm space that I expected in the second example)? I can get the result I expected with the simple presence of a non visible shape in (0,0), like the third example. What's the explanation for the behavior in the second example, where (0,0) doesn't act as the corner anymore? It seems that the lowest coordinates that are used in the code become the "origin" (lower left corner), is that the case?

Line1:\tikz \draw (0,0) -- (1,1);

Line2:\tikz \draw (1,0) -- (2,1);

Line3:\tikz \draw (0,0) -- (0,0) (1,0) -- (2,1);

  • 1
    TikZ crops and puts the bottom of the bounding box at the baseline. So in the end the first two examples is just a line going rightward. See the baseline option for how to alter this behavior. Another way to fix Line2 is \tikz\draw(0,0)(1,0)--(2,1); because (0,0) expands the bounding box. – Symbol 1 Apr 14 '21 at 02:16
  • 1
    https://tex.stackexchange.com/questions/354765/how-to-place-nodes-in-an-absolute-coordinate-system-in-tikz?rq=1 – js bibra Apr 14 '21 at 03:45
  • Tks, indeed just a (0,0) is the minimum amount of code that solves my issue. The answer to that question is exactly what I was looking for. Just one thing: if the bounding box is adjusted to fit all elements, it seems that my suspect was right and the lowest numbers of all used coordinates become the coordinate for the low left corner, right? If the figure has elements with (-4,3), (0,0), (1,5), the lower corner becomes (-4,0). Can I think like this without exceptions? – Allan Felipe Apr 14 '21 at 04:24
  • 1
    Almost true, but only for very simple drawings. Obviously things change if you use baseline or even rotate or scale . Moreover, in spline elements (curves) the control points are also taken into account, so sometimes the bounding box is bigger than expected. overlay modifiers makes thing invisible to the bounding box so you can have smaller than expected ones... – Rmano Apr 14 '21 at 05:47
  • @Rmano Tks. Well, all of this could be in a possible answer, since this last information is not present in the link provided. – Allan Felipe Apr 14 '21 at 06:31

1 Answers1

1

The normal behavior of TikZ is to crop and then align the baseline at the bottom of the figure (there is a bit of extra space normally, unless you use the baseline option.).

Obviously things change if you use baseline or even rotate or scale . Moreover, in spline elements (curves) the control points are also taken into account, so sometimes the bounding box is bigger than expected. overlay modifiers makes thing invisible to the bounding box so you can have smaller than expected ones...

To check what happen, you can add an overlayed grid with a coordinate marker:

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\tikzset{
  background rectangle/.style={draw, dashed, black!50}
}
\newcommand{\dogrid}[1][cyan]{% notice that you have to add a ; after
    \draw [ultra thin, #1, overlay] (-2,-2) grid[step=1cm] (2,2) 
    (0,0) node[font=\tiny]{(0,0)}%
}
\begin{document}

Line1:\tikz[show background rectangle, baseline]{ \dogrid; \draw (0,0) -- (1,1);}

Line2:\tikz[show background rectangle]{ \dogrid[red]; \draw (1,0) -- (2,1); }

Line3:\tikz[show background rectangle]{ \dogrid[green]; \draw (0,0) -- (0,0) (1,0) -- (2,1);}

\hspace*{3cm} Line4:\tikz[show background rectangle, rotate=45]{ \dogrid[blue]; \draw (0,0) -- (0,0) (1,0) -- (2,1);} \end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125