# TikZ and PGF — Design considerations and approach to sizing pictures?

The first pages of the TikZ/PGF manual specifies that measurements are in points, centimeters, or are relational to other lines/objects, but I don't understand how to determine that up front.

I have a solution (provided by Qrrbrbirlbel) here for some graphics I'm using:

This does exactly what I want, but I would like to have smaller grids and the ability to position them on the page in (for example) a 5 column / 8 row matrix. I'm assuming there is some "automatic" way to do this, but from what I've learned from the TikZ & PGF Manual, it seems that the writer must know the dimentions up front.

I know that there are scaling options, but with my very limited knowledge of TikZ/PGF, they didn't seem to address the scaling of multiple pictures and arranging them in a grid on the paper.

What is the approach to creating TikZ graphics without the concern of their dimensions?

Edited to add: I did find the \matrix part of the manual that discusses this, so perhaps this is the way to approach it?

-
Probably have a look at this, and this might also be helpful. –  Tom Bombadil Aug 16 '13 at 21:23
The second link does what I'm looking for -- an image gallery. I'm assuming this can be used with TikZ pictures generated from within the same source (i.e., not from external files)? –  Kevin P. Kilburn Aug 16 '13 at 22:09
Indeed. Just use \adjustedtikzsize{<TikZ draw commands>}{<caption>}. If you need further help with this, please don't hesitate to update your question or ask a new one entirely. –  Tom Bombadil Aug 16 '13 at 23:55
@TomBombadil if you want to put this as an answer, I'll accept it. Thanks for the help. –  Kevin P. Kilburn Aug 17 '13 at 14:25

I fiddles a little with my linked answer, now you can specify the number of picture rows, the pictures per row and the spacing. Please note that spaces between pictures are crudely canceled by things like \hspace{-1.026cm}, which might be font and font size sensitive and probably need adjustment (improvement proposals more than welcome). Also, due to limited accuracy, the pictures are not all the exact same height, it varies by a few pixels. If increased accuracy is needed, probably the fpu library of TikZ could be a remedy. Otherwise, just don't make the spaces to small, or disable the grayback command.

## Code

\documentclass{scrartcl}
\usepackage[inner=10mm,outer=35mm,top=20mm,bottom=30mm,twoside]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newcommand{\getsizes}%
{   \path (current bounding box.south west);
\pgfgetlastxy{\xsw}{\ysw}
\path (current bounding box.north east);
\pgfgetlastxy{\xne}{\yne}
\pgfmathsetlengthmacro{\picwidth}{\xne-\xsw}
\pgfmathsetlengthmacro{\picheight}{\yne-\ysw}
\pgfmathsetmacro{\maxwidth}{(0.98*\textwidth-(\picturesperrow-1)*\pictureseparation*29)/\picturesperrow}
\pgfmathsetmacro{\xscale}{\maxwidth/\picwidth}
\pgfmathsetmacro{\maxheight}{(0.98*\textheight-(\picturerows-1)*\pictureseparation*29)/\picturerows}
\pgfmathsetmacro{\yscale}{\maxheight/\picheight}
\xdef\xscalefactor{\xscale}
\xdef\yscalefactor{\yscale}
}

\newcommand{\grayback}%
{   \begin{pgfonlayer}{background}
\fill[left color=gray!20,right color=gray!5,opacity=0.5] (current bounding box.south west) rectangle (current bounding box.north east);
\fill[top color=gray!5,bottom color=gray!70,opacity=0.5] (current bounding box.south west) rectangle (current bounding box.north east);
\end{pgfonlayer}
}

{   \stepcounter{galleryitem}
\smash{\vphantom{
\begin{tikzpicture}
#1
\getsizes
\end{tikzpicture}
}}
\begin{tikzpicture}[xscale=\xscalefactor, yscale=\yscalefactor]
#1
\grayback
\end{tikzpicture}
\hspace{-1.026cm}
\pgfmathtruncatemacro{\breaktest}{mod(\thegalleryitem,\picturesperrow)}
\ifthenelse{\breaktest=0}{\par\vspace{-0.53cm}\vspace{\pictureseparation cm}}{\hspace{\pictureseparation cm}}
}

\newcounter{galleryitem}
\setcounter{galleryitem}{0}

\newenvironment{tikzgallery}[3]% pics per row, rows, space in cm
{\clearpage\xdef\picturesperrow{#1}\xdef\picturerows{#2}\xdef\pictureseparation{#3}\setcounter{galleryitem}{0}}%
{}

\newcommand{\picone}
{   \draw[rotate=30,left color=red,right color=blue] (0,0) rectangle (5,2);
}

\newcommand{\pictwo}
{   \draw[rotate=20,left color=green,right color=red] (0,0) rectangle (5,2);
}

\newcommand{\picthree}
{   \draw[rotate=40,left color=orange,right color=blue] (0,0) rectangle (5,2);
}

\newcommand{\picfour}
{   \draw[left color=black!10,right color=black!90] (-1,-7) rectangle (1,7);
\draw[left color=yellow,right color=violet] (0,0) circle (5);
}

\begin{document}

\parindent0mm
\parskip0mm

\begin{tikzgallery}{4}{3}{0.1}% pics per row, rows, space in cm
\end{tikzgallery}

\begin{tikzgallery}{5}{7}{1}% pics per row, rows, space in cm
\end{tikzgallery}

\begin{tikzgallery}{2}{3}{0.5}% pics per row, rows, space in cm
\end{tikzgallery}

\begin{tikzgallery}{9}{9}{0.1}% pics per row, rows, space in cm