[Tex/LaTex] Is it possible to make an image gallery with tikz images

environmentsgraphics

I create many images with TikZ and I would like to make an image gallery. It should look like this:

enter image description here

The gallery should

  • put the image in front of a (gray) background
  • resize the image to a maximum width / height (and leave the aspect ratio intact)

It would be nice if I could put the images in seperate files and use something like

\begin{gallery}
    \input{tikz/myImage1.tex}[Some title]
    \input{tikz/myImage2.tex}[another title]
    \input{tikz/mathImage.tex}[This is a visualization of $f(x, y) = x^2 y^2$]
\end{gallery}

This is what I've got so far (code also at GitHub):

\documentclass[a4paper,12pt]{article}
\usepackage{amssymb,amsmath} % needed for math
\usepackage{standalone} % needed for includestandalone
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage{adjustbox}
\usepackage{xcolor}
\definecolor{myLightGray}{HTML}{F9F9F9}
\definecolor{borderColor}{HTML}{CCCCCC}

\usepackage{tikz} % needed for includestandalone
\usetikzlibrary{arrows,positioning, calc}

\tikzset{
    pil/.style={->,thick}
}

\newenvironment{gallery}{}{}

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam 
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam 
erat, 
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea 
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem 
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur 
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore 
et dolore magna aliquyam erat, sed diam voluptua. At vero eos et 
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum 
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam 
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. 
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum 
dolor sit amet.   

\noindent
\begin{gallery}
    \adjustbox{width=4cm,height=4cm,keepaspectratio,bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}{\includestandalone{dot-product-1}}
    \adjustbox{width=4cm,height=4cm,keepaspectratio,bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}{\includestandalone{dot-product-2}}
    \adjustbox{width=4cm,height=4cm,keepaspectratio,bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}{\includestandalone{dot-product-3-1}}
    \adjustbox{width=4cm,height=4cm,keepaspectratio,bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}{\includestandalone{dot-product-6}}
    \adjustbox{width=4cm,height=4cm,keepaspectratio,bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}{\includestandalone{equilateral-triangle}}
    \adjustbox{width=4cm,height=4cm,keepaspectratio,bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}{\includestandalone{equilateral-triangle-heights}}
\end{gallery}

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse 
molestie consequat, vel illum dolore eu feugiat nulla facilisis at 
vero eros et accumsan et iusto odio dignissim qui blandit praesent 
luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam 
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat 
volutpat.   

\end{document}

Which produces:

gallery so far

I've used \includestandalone, but with this I had to add the style defined in \tikzset in dot-product-1.tex to the main document. Thats not good.

Best Answer

To horizontally align the content in the gray rectangle use center=4cm, which will force an official width of 4cm and horizontally center the content in it without doing any further scaling. The vertical alignment can be done using valign=M followed by a set vsize={2cm}{2cm}. Alternatively you can also use raise={.5\depth-.5\height}{2cm}{2cm}. Note that both will align the middle of the boxes at the baseline, which is fine here, but you might want to raise=2cm it in other cases.

\documentclass[a4paper,12pt]{article}
\usepackage{amssymb,amsmath} % needed for math
\usepackage{standalone} % needed for includestandalone
\usepackage[utf8]{inputenc} % this is needed for umlauts
\usepackage[ngerman]{babel} % this is needed for umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf

\usepackage{adjustbox}
\usepackage{xcolor}
\definecolor{myLightGray}{HTML}{F9F9F9}
\definecolor{borderColor}{HTML}{CCCCCC}

\usepackage{tikz} % needed for includestandalone
\usetikzlibrary{arrows,positioning, calc}

\tikzset{
    pil/.style={->,thick}
}

\newenvironment{gallery}{%
    \center
}{%
    \par\endcenter
}

\def\galleryimage#1{%
    \adjustbox{width=4cm,height=4cm,keepaspectratio, center=4cm,
    valign=M, set vsize={2cm}{2cm},
    %or: raise={.5\depth-.5\height}{2cm}{2cm},
    bgcolor=myLightGray,cfbox=borderColor 1px 0px 2px}
    {\includestandalone{#1}}%
    \space\ignorespaces
}%

\begin{document}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam 
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam 
erat, 
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea 
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem 
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur 
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore 
et dolore magna aliquyam erat, sed diam voluptua. At vero eos et 
accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum 
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam 
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. 
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum 
dolor sit amet.   

\begin{gallery}
    \galleryimage{dot-product-1}
    \galleryimage{dot-product-2}
    \galleryimage{dot-product-3-1}

    \galleryimage{dot-product-1}
    \galleryimage{dot-product-2}
    \galleryimage{dot-product-3-1}

    \galleryimage{dot-product-6}
    \galleryimage{equilateral-triangle}
    \galleryimage{equilateral-triangle-heights}
\end{gallery}


Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse 
molestie consequat, vel illum dolore eu feugiat nulla facilisis at 
vero eros et accumsan et iusto odio dignissim qui blandit praesent 
luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam 
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat 
volutpat.   

\end{document}

It looks like this:

example

Related Question