4

I saved the following LaTeX code in ~/Test.tex.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikz[remember picture, overlay]
    \draw let \p1=(current page.center) in
       node[circle,fill=red,label=above:(\p1)] at (\p1) {};
\tikz[remember picture, overlay]
    \draw let \p1=(current page.center) in
       node[circle,fill=blue,label=below:(\p1)] at (\p1) {};
\end{document}

The code draws, via a \tikz command, a filled red circle at the center of the page, and labels it from above with the coordinates of the center of the page. It then repeats the same thing, via a new \tikz command, but this time the circle's color is blue, and the label is placed on the circle's bottom.

I then executed the following commands in the Terminal.

> cd ~
> pdflatex Test
> pdflatex Test

The commands completed successfully, with no warnings in ~/Test.log (in particular, no instructions to rerun). The file ~/Test.pdf had now been created. When opened in a PDF viewer, it displayed as follows.

Two Tikz commands drew a circle at the center of the page

As can be seen, a single filled circle appears at the center of the page. It is colored blue. This indicates that the circles drawn by the two \tikz commands overlap each other perfectly. This implies that the two \tikz commands calculated the same coordinate as the page's center.

However, if we now zoom in on the vicinity of the blue circle, we notice that the two labels show different coordinates that differ in their first component.

The two labels differ in the first component of their coordinates

If we edit the code to add the words "Hello, world!" between the two \tikz commands, the difference between the coordinates' first components, as seen on the generated PDF file, is even more striking: 157.8775pt (top) vs. 94.5163pt (bottom).


Questions

  1. Why do the two labels display different coordinates?
  2. Is there a way of obtaining, via TikZ, literal coordinates of an arbitrary point on the page, that can be used as-is in a different TikZ picture to locate the same point, and that can be used by a human being to locate the point, with pen and ruler, on the printed PDF file?
  3. Conversely, if I find with pen and ruler a point on a physical A4 page that I'd like to draw a circle about, how can I use TikZ to draw said circle?
3
  • 5
    The coordinates reported are relative to the origin of the enclosing tikzpicture. The two pictures have slightly different locations on the page so report slightly different coordinates for the centre of the page. Dec 18, 2022 at 8:30
  • 2
    The locations are different because line ends are converted to space (I understand), which then push out the second tikz picture; doing \tikz[remember picture, overlay]% \draw let \p1=(current page.center) in% node[circle,fill=red,label=above:(\p1)] at (\p1) {};% \tikz[remember picture, overlay] \draw let \p1=(current page.center) in node[circle,fill=blue,label=below:(\p1)] at (\p1) {}; removes the spaces and both report the same coordinates.
    – Cicada
    Dec 18, 2022 at 10:31
  • 2
    +1: Good question! I enjoyed reading the two answers. Dec 19, 2022 at 21:45

2 Answers 2

5

TL;DR: Each tikzpicture uses coordinates relative to its location on the page and not absolute coordinates. In the question, the two pictures are in different places on the page and therefore report different relative position vectors for the centre of the page.


A tikzpicture is intended to be a self-contained drawing that is put together by TikZ/PGF and then handed to TeX as a box for TeX to put on the page obeying its normal placement routines. So when TikZ/PGF is putting together it doesn't know where on the page the drawing will end up. Therefore, even if it were a reasonable design choice then TikZ/PGF couldn't use an absolute coordinate system.

The expected usage of TikZ is also that it would mainly be used as way to create self-contained drawings, in which case an absolute coordinate system isn't useful. So when building a picture, TikZ interprets coordinates to be with respect to an arbitrary origin, which may or may not be within the bounding box of the picture.

So when you ask TikZ to display the coordinates of some point then it will always report them relative to the origin for the current picture.

Now, TikZ is nothing if not flexible and while the expectation is that its pictures will be self-contained, it doesn't force that on you and does provide for ways to reference points in other pictures. Its mechanism for doing that is to record where the origins of the the pictures involved are located on the page (which is what remember picture does). So then it can translate a coordinate-relative-to-another-origin into a coordinate-relative-to-this-origin.

Pseudo nodes, such as the current page node, are actually different in different tikzpictures because they are defined relative to the current picture's origin.

So the reason that your two pictures report different coordinates for the centre of the page is because they are in slightly different locations and so their origins are in slightly different places on the page. You can see this by adding in a node at the origin of each picture:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/668889/86}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikz[remember picture, overlay]
    \draw (0,0) node[circle,fill=red] {} let \p1=(current page.center) in
       node[circle,fill=red,label=above:(\p1)] at (\p1) {};
\tikz[remember picture, overlay]
    \draw (0,0) node[circle,fill=blue] {} let \p1=(current page.center) in
       node[circle,fill=blue,label=below:(\p1)] at (\p1) {};
\end{document}

circles at origins of pictures

The reason for this is a little subtle. Even though the tikzpictures are created with the overlay option, they still create a box. While that box has zero dimensions, it still counts as a box. So when TeX looks at what you have written, it sees box space box. Because of that initial box, the space is not ignored (as it would be at the start of a line). Removing that space means that the two boxes are on top of each other and so report the same coordinates for the centre of the page:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/668889/86}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikz[remember picture, overlay]
    \draw (0,0) node[circle,fill=red] {} let \p1=(current page.center) in
       node[circle,fill=red,label=above:(\p1)] at (\p1) {};% <--- get rid of the space
\tikz[remember picture, overlay]
    \draw (0,0) node[circle,fill=blue] {} let \p1=(current page.center) in
       node[circle,fill=blue,label=below:(\p1)] at (\p1) {};
\end{document}

No space, so the pictures are in the same place

5
  • 1
    +1: Great soon-to-be canonical answer. Dec 19, 2022 at 16:06
  • @Dr.ManuelKuehner Thanks! Do suggest improvements if you (or anyone else) can see places to make it better. Dec 19, 2022 at 17:28
  • I am not an expert but as a "customer" of you experts :), I would propose to add the following TD;DR: "In short, tikz uses relative coordinates and not absolute coordinates. In the OP's case, the two tikz pictures have a different position on the page and therefore report different relative distances to the absolute center of the page. " (Of course, make is sound more English.) Dec 19, 2022 at 21:42
  • 1
    @Dr.ManuelKuehner Good idea. I've added a TL;DR at the top. Dec 19, 2022 at 22:01
  • 1
    Since this has come up a few times on this site as well: This way how PGF/TikZ builds its pictures is also the reason you can't say (.5\textwidth,0)--(\textwidth,0) and expect it to draw a line from the middle to the right end of the text area. That's just the same picture as (0,0)--(.5\textwidth,0). —> Self-contained. Dec 20, 2022 at 0:06
0

Having pondered about this for over an hour, I think perhaps each TikZ picture sets up a different coordinate system for the page. These coordinate systems are local to each TikZ picture, and their origins may be mapped to different points on the physical page. (Possibly also their unit lengths are different.)

But regardless of coordinate system, all TikZ pictures configure the anchors of the current page pseudo-node to point to the same physical point on the page. So even though this physical point may have different coordinates in two different TikZ pictures, they will still resolve to the same physical point, not to two different physical points, as long as each coordinate is used inside its respective TikZ picture.

The moral of the story is that in order to specify in a TikZ picture the coordinates of a point on the physical page, the coordinates must be specified relative to one of the anchors of the current page pseudo-node.

Conversely, if you want to find out via TikZ the absolute coordinates of a point on the physical page, you need to get the relative coordinates of this point w.r.t. one of the anchors of the current page pseudo-node, together with the information about which anchor was used as the reference point, and then you can use these two pieces of information to locate the physical point.

I still don't understand why different TikZ pictures set up different coordinate systems, but this is immaterial for all practical purposes. It would still be interesting to know.

3
  • Indeed. Since Tikz pictures can start anywhere on the page, the experiment of inserting some text between the two pictures shows that the coordinates reported are likely offsets with respect to the picture origin (that is, relative), implying the picture itself is aware of absolute positioning. The aux captures pgf locations: \relax \pgfsyspdfmark {pgfid1}{9782558}{43234099} \pgfsyspdfmark {pgfid2}{9782558}{43234099}
    – Cicada
    Dec 18, 2022 at 10:46
  • All reported coordinates are relative to the the current transformation matrixof the current TikZ picture. That's just how it works and how those reported numbers should be able to be used again. If you want to know the coordinates of some point in relation to another point either shift the coordinate to that point (say shift=(current page.south west)) or calculate the difference (say ($(current page.center)-(current page.south west)$)). But note that any scaling, rotating or slanting will change the reported coordinates. (Use reset cm if needed.) Dec 18, 2022 at 14:46
  • [remember picture] does two things. It stores the origin location for this tikzpicture in the aux file (see Cicada's comment). It computes the coordinates for other tikzpictures by adding the difference between the two origins. Dec 18, 2022 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .