87

Consider the following:

Let's say I have a dot. I would like to be able to position this dot anywhere on the page: at the corner, in the middle, anywhere.

So far, despite poring over manuals and experimenting, I can not figure out how to do this. It seems logical that there would be some system that would place a conceptual grid on the page and then allow for an exact placement within that grid.

I'm sure it exists, but I can't seem to find it.

I can make the dot, and all sorts of other shapes, but I just don't seem to have any control over where these shapes appear on the page.

I'm sure this is an obvious question and I have overlooked something.

1

2 Answers 2

127

Yes, it really does exist and is quite easy to use.

There are quite some subtleties in this as it can be cumbersome to use in several packages. The reason is that the compilation of the document has to be done several times in order to get the correct placement on the screen.

For instance, an A4-paper is not the size of an A3-paper, secondly you can not know the exact page size due to various packages in the making of the document.

One of the packages that is very nice to use this feature in is the beamer package. If you need this method in that package you are required to add fragile to the frame (ensures that the compilation will not fail with dubious error messages).

First of all you need to apply the remember picture to the tikzpicture environment. This enables tikz to save the picture size and various segments in the picture. Secondly you need to specify the overlay option which puts the picture out of the text-segment, allowing it to be placed on top of text without taking up any space. If you do not specify overlay it will be a regular text-box which is placed next to the preceding TeX-box. This is often not desirable when positioning relatively on the page. (Try and remove overlay from the example below and see the picture going out of bounds)

Now, in order to access the page you need the current page node which is referring to the rectangular shape that takes up the entire page. Thus you can refer to anchors and whatever you need via this.

Here is an example which gives you an idea...

\documentclass{article}
\usepackage{tikz}
\usepackage[paperheight=8cm,paperwidth=12cm]{geometry}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[remember picture,overlay]
  \foreach \bound in {north,south,west,east,45} {
      \node[anchor=\bound] at (current page.\bound) {I am \bound-bound...};
  }
  \node at (current page.center) {I am NOT MOOVING!};
  % You can see the border of the page node with this:
  \draw[thick] (current page.south west) rectangle (current page.north east);
\end{tikzpicture}
\end{document}

And the result is: enter image description here

Relative coordinate system

I wished to elaborate on this and also help those who wants the actual relative coordinate system of the page. You can also do this by other means, but for easy reference and clear cut code a coordinate system transformation is very easy to understand.

So basically what I have done is creating a page cs transformation which transfers the page into a grid as shown in the below code (describing the cs by (-1,-1) rectangle (1,1)).

For this you need the \tikzdeclarecoordinatesystem in order to redefine a cs. You can check out this in the manual if in doubt of how it is used.

% Defining a new coordinate system for the page:
%
% --------------------------
% |(-1,1)    (0,1)    (1,1)|
% |                        |
% |(-1,0)    (0,0)    (1,0)|
% |                        |
% |(-1,-1)   (0,-1)  (1,-1)|
% --------------------------
\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
    \parsecomma#1\endparsecomma
    \pgfpointanchor{current page}{north east}
    % Save the upper right corner
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    % save the lower left corner
    \pgfpointanchor{current page}{south west}
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    % Transform to the correct placement
    \pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
    \expandafter\pgf@x\expandafter=\pgfmathresult pt
    \pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
    \expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother

This will allow you to do the following:

\begin{tikzpicture}[remember picture,overlay,every node/.style={anchor=center}]
  \node at (page cs:0.5,0.3) {0.5,0.3};
  \node at (page cs:-0.25,0.3) {-0.25,0.3};
  \node at (page cs:0,0) {0,0};
  \draw(page cs:-0.25,0) -- (page cs:.75,-0.5);
  \draw[thick] (page cs:-1,-1) rectangle (page cs:1,1);
\end{tikzpicture}

which results in: enter image description here

4
  • 1
    How can I not position something relative to current page.east but shift it down by some amount? Commented Sep 2, 2016 at 14:32
  • 2
    I am not sure I understand your query? You can still use the calc library for moving below east? I.e. \draw (current page.east) -- ($(current page.east)+(0, -1)$);?
    – nickpapior
    Commented Sep 2, 2016 at 14:33
  • 1
    Hm, I'm not to familiar with using libraries in tikz, but now I've just added transform canvas={yshift = -.5cm} as option for the \node[…], which moves the node down by .5cm. Commented Sep 2, 2016 at 14:39
  • I would only suggest that if you want the entire canvas moved? Else I think you should look up the calc library which is really handy in almost all tikz drawings.
    – nickpapior
    Commented Sep 2, 2016 at 15:02
19

Another approach for absolute coordinate positionning: With (0,0) in the top left corner of the page and (1,1) in the bottom right corner of the page:

\documentclass{article}
\usepackage[margin=1in,showframe]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}
\begin{document}
\begin{tikzpicture}[remember picture,overlay,shift=(current page.north west)]
\begin{scope}[x={(current page.north east)},y={(current page.south west)}]
\draw[red,->,line width=2pt] (0,0) -- (1,1);
\end{scope}
\end{tikzpicture}
\end{document}

Similarly with (0,0) in the bottom left and (1,1) in the top right corner of the page. For instance, if you want to position inside the text area, knowing the width of the margins:

\begin{tikzpicture}[remember picture,overlay,shift=(current page.south west)]
  \begin{scope}[x={(current page.south east)},y={(current page.north west)}]
    \draw[red,->,line width=2pt] (0,0) -- (1,1); 
    \draw[blue,->,line width=2pt] ($(0,0)+(1in,1in)$) -- ($(1,1)+(-1in,-1in)$); 
  \end{scope}
\end{tikzpicture} 

enter image description here

9
  • 2
    Hi @AndréC, In that case if you want the usual coordinates (0,0) bottom left and (1,1) top right you just need to change a bit the lines above. See code below latex \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc,arrows} \begin{document} \begin{tikzpicture}[remember picture,overlay,shift=(current page.south west)] \begin{scope}[x={(current page.south east)},y={(current page.north west)}] \draw[thick,red,->,line width=1cm,] (0,0) -- (0.5,0.5) ; \end{scope} \end{tikzpicture} \end{document} Commented Mar 8, 2020 at 14:06
  • 1
    Please edit your answer by adding this code. You can keep the old answer and add this new code as it is customary to do on this site. As I did for example here: Nested Enumerations overlap
    – AndréC
    Commented Mar 8, 2020 at 14:26
  • 1
    @AndréC I edited my answer and added both solutions Commented Mar 9, 2020 at 13:38
  • 1
    This is great and absolutely the most reproducible answer here. Thanks! Commented Jul 12, 2020 at 17:45
  • 1
    Added a screenshot and showed how to position relative to text margins. Hope this helps.
    – PatrickT
    Commented Oct 21, 2021 at 3:43

You must log in to answer this question.

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