Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

Approach to creating graphics in book - TikZ, SVG, PDF, Inkscape?

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?

share|improve this question
1  
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
2  
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
add comment

1 Answer

up vote 4 down vote accepted

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

enter image description here

share|improve this answer
    
In the example pictures, the aspect ratios seem different. Is there a way to force the aspect ratio to remain the same as the original picture? (Disclaimer: I have yet to run this code, so maybe it already does it). The link tex.stackexchange.com/questions/74347/… appears to have the proper aspect ratios in all examples. –  Kevin P. Kilburn Aug 27 '13 at 16:04
    
Here, the aspect ratio is not kept, while in the other example it is. –  Tom Bombadil Aug 27 '13 at 17:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.