# Tikz with standalone: pinning tikz coordinates to page cm

I've got something like the following:

\documentclass[tikz]{standalone}% Does not support leap years.

\usepackage{tikz}

\usepackage[paperwidth=12cm, paperheight=25cm, margin=0cm]{geometry}

\newcommand{\printdots}[1]{
\begin{tikzpicture}[
x=1cm,
y=1cm,
xscale = 1,
yscale=1,
]

%Draw dots
\foreach \y in {1, 1.5, ..., 24} {
\foreach \x in {1, 1.5, ..., 11} {
\fill[color=black] (\x,\y) circle (0.05);
}
}

\end{tikzpicture}%
}

\begin{document}
\foreach \n in {1,...,2}{%
\printdots
}
\end{document}


i.e. I'd like to make a 12x15cm standalone, with 2 pages. On each page, I'd like to print dots 0.5cm apart, starting 1cm from the edge.

(Eventually, I'd like to do more complex things, so if there's an easier way to print dots, that probably won't solve my problem).

However, when I compile this, it ends up with a large space on the right and bottom, missing dots on the top and left. In other words, the tikz coordinates aren't matching the page coordinates.

Is there any way to make my Tikz coordinates exactly match the page coordinates in cm? Basically, I want to tell tikz not to automatically scale the picture at all based on what I have in it.

I've seen lots of tikz posts on attaching specific nodes to a page position, but not how to align the coordinates with the page position.

• isn't this simpler to achieve with standard document class like article? for tikz try to use overlay ans remember picture for exact positioning your dots on page. – Zarko Nov 24 '18 at 23:49

Just use the native options of the standalone class which allows you to add a predefined margin, here 1cm. And do not use the geometry package.

\documentclass[tikz,border=1cm]{standalone}


In addition, it is not necessary to load the tikz package twice. By loading it into the options of the standalone class, each figure created by tikz is automatically croped. I quote the manual version v1.3a page 8:

For pictures drawn with TikZ a dedicated tikz option is provided which loads the tikz package and also configures the tikzpicture environment to create a single cropped page.

Thus these two commented lines can be deleted:

%\usepackage{tikz}
%\usepackage[paperwidth=12cm, paperheight=25cm, margin=0cm]{geometry}


In addition, the default unit of tikz is centimetre, so it is not necessary to specify it.

And to make a foreach of 2 iterations, the dotted lines are useless, simply write:

 \foreach \n in {1,2}{%


If I counted correctly, with margins of 1cm on each side, the points must be drawn on a figure that measures 10x13cm.

Which is easy to do by counting from 0 instead of 1:

\foreach \y in {0,.5,...,13} {
\foreach \x in {0,.5,...,10} {


\documentclass[tikz,border=1cm]{standalone}% Does not support leap years.
\newcommand{\printdots}[1]{
\begin{tikzpicture}
%Draw dots
\foreach \y in {0,.5,...,13} {
\foreach \x in {0,.5,...,10} {
\fill[color=black] (\x,\y) circle (0.05);
}
}
\end{tikzpicture}%
}

\begin{document}
\foreach \n in {1,2}{%
\printdots
}
\end{document}


Now that I've written the answer, I see @Zarko's comment. I agree with him that one should better use article (or something of this sort) to create a multipage document.

\documentclass{article}% Does not support leap years.
\usepackage{tikz}
\usepackage{eso-pic}

\usepackage[paperwidth=12cm, paperheight=25cm, margin=0cm]{geometry}
\begin{tikzpicture}[overlay,remember picture,
x=1cm,
y=1cm,
xscale = 1,
yscale=1,
]

%Draw dots
\foreach \y in {1, 1.5, ..., 24} {
\foreach \x in {1, 1.5, ..., 11} {
\fill[color=black] ([xshift=\x*1cm,yshift=\y*1cm]current page.south
west) circle (0.05);
}
}
\end{tikzpicture}}

\begin{document}
~
\clearpage
~
\end{document}


Using other than the standalone class frees you from many limitations. You can use a normal article class with the powerful pic construct from tikz. You can place the dots picture on any page you want without interfering with the page content using the [overlay, remember picture] option as noted out by @Zarko. The new macro \drawdots defined below conveniently adds the dots to any page.

\documentclass{article}
\usepackage{tikz}
\usepackage[paperwidth=12cm, paperheight=25cm, margin=0cm]{geometry}

\tikzset{
mydots/.pic = {
\foreach \y in {0, .5, ..., 23}
\foreach \x in {0, .5, ..., 10}
\fill[color=black] (\x,\y) circle (0.05);
}
}

\newcommand{\drawdots}{%
\begin{tikzpicture}[overlay, remember picture]
\pic at ([shift={(1,1)}]current page.south west) {mydots};
\end{tikzpicture}
}

\begin{document}
\drawdots
\clearpage
\drawdots
\end{document}