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}
}
\newcommand{\adjustedtikzsize}[1]% draw commands
{ \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
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\end{tikzgallery}
\begin{tikzgallery}{5}{7}{1}% pics per row, rows, space in cm
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\end{tikzgallery}
\begin{tikzgallery}{2}{3}{0.5}% pics per row, rows, space in cm
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\end{tikzgallery}
\begin{tikzgallery}{9}{9}{0.1}% pics per row, rows, space in cm
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\picfour}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picthree}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\picone}
\adjustedtikzsize{\pictwo}
\adjustedtikzsize{\picone}
\end{tikzgallery}
\end{document}
Output
\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.