# Arrange / align external images in grid with TikZ

I'm trying to use TikZ to combine four raster images into a single figure and annotate the images. If you feel like asking why, see *.

My first problem is arranging / aligning the images in a grid.

The code below sort of gets the job done, but the order in which the images appear is not what I would expect. I would expect

[a][b]
[c][d]


but I get

% remember usepackage{tikz}
\begin{figure}
\begin{tikzpicture}
\node[anchor=north west ,inner sep=0] (frame1) at (0,0)
{\includegraphics[width=0.45\textwidth]{a.png}};

\node[anchor=north east,inner sep=0] (frame2) at (0,0)
{\includegraphics[width=0.45\textwidth]{b.png}};

\node[anchor=south west,inner sep=0] (frame3) at (0,0)
{\includegraphics[width=0.45\textwidth]{c.png}};

\node[anchor=south east,inner sep=0] (frame4) at (0,0)
{\includegraphics[width=0.45\textwidth]{d.png}};
\end{tikzpicture}
\caption{My caption}
\end{figure}


How would you arrange these four images in a 2x2 grid, preferably in a predictable order?

*Scientific journals often expect a single pdf or tiff file for each figure, combining images with the subfigure environment or similar is not an option since they do not support exporting the figure you created to a seperate pdf file, but TikZ does. And I need this single pdf file to submit as the final figure. Combining and annotating in an external graphics editor like Inkscape introduces the problem of fonts, font sizes etc. Much nicer to have LaTeX do the annotation to get the right font and size.

• When setting achor=north west it means that the image (a) will have its upper left corner at position 0.0. For (b) the same would be its top right corner. So you see that if you just change the anchors, it will work. But for a bigger grid, your solution wouldn't work. you could use a tabular. why can't you use a subfigure or subfig? – Runar Aug 11 '16 at 20:56
• @RunarTrollet On why I can't use subfigure, my original post contained an "If you feel like asking why, see *." – Jonas Camillus Jeppesen Aug 11 '16 at 21:04
• It doesnt say why, just that it "is not at option" – Runar Aug 11 '16 at 21:07
• Right, my bad. I assumed that it was clear I meant to externalize the tikz picture so i had this single pdf file to submit as a figure, but I didn't write it, so how could it be clear? Also I assumed it was clear that there is no way to have figure/subfigure generate a single pdf representing that figure. I'll update my *-section. – Jonas Camillus Jeppesen Aug 11 '16 at 21:11
• Also, you are right about the anchor point. I thought "anchor=north west" meant place the node north west of the coordinate. If that was the case "anchor=north west" would place the image above (0,0) to the left, which is where I wanted image A to go (top left). – Jonas Camillus Jeppesen Aug 11 '16 at 21:13

Try:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[
image/.style = {text width=0.45\textwidth,
inner sep=0pt, outer sep=0pt},
node distance = 1mm and 1mm
]
\node [image] (frame1)
{\includegraphics[width=\linewidth]{example-image-a}};
\node [image,right=of frame1] (frame2)
{\includegraphics[width=\linewidth]{example-image-b}};
\node[image,below=of frame1] (frame3)
{\includegraphics[width=\linewidth]{example-image-c}};
\node[image,right=of frame3] (frame4)
{\includegraphics[width=\linewidth]{example-image}};
\end{tikzpicture}
\caption{My caption}
\end{figure}
\end{document}


As you can see, I add TikZ library positioning, by which I control distance between picture. Also I use it in placement of nodes with images.

• Better solution. This seems to be extendable to n x m by simply continuing "right=of frame1", "right=of frame2" etc. There will always be manual labor involved in something like this, after all I want to control which image goes where. – Jonas Camillus Jeppesen Aug 11 '16 at 21:27
• I have to confess I drew up my image A, B, C, and D in inkscape. This is my first use of TikZ and pgf, so I didn't know about "example-image-a" etc. – Jonas Camillus Jeppesen Aug 11 '16 at 21:28
• @JonasCamillusJeppesen, nodes you can concatenated in chains. This has sense, if you have more that two images in row or column. In this case you need to position "manually" only the first node in each chain. example-image are from packages graphcx, unfortunately only A, B C and without letter name. – Zarko Aug 11 '16 at 21:37

I think tcbraster provides an easier solution. You fix the columns number and add as many figures you need with tcbincludegraphics. The appearance order is the same you introduces into the tcbraster:

\documentclass{article}
\usepackage[most]{tcolorbox}

\begin{document}
\begin{figure}[htb]
\centering
\begin{tcbraster}[raster columns=2, enhanced, blankest]
\tcbincludegraphics{example-image-a}
\tcbincludegraphics{example-image-b}
\tcbincludegraphics{example-image-c}
\tcbincludegraphics{example-image}
\end{tcbraster}
\caption{My caption}
\end{figure}
\end{document}


• +1, very useful answer! I've added complements based on this technique for people who want to produce an overview of the various pages of a document (each image put in the tcbraster is a page extracted from a “master” document). This can be useful if you have code that produces several pages and you want to show them in an answer, but don't want to upload several screenshots, because it often occupies a lot of visual space in the answer—plus, uploading several screenshots takes non-negligible time. – frougon Jul 19 at 9:05

A variant of Ignasi's nice answer that can be useful if the images you want to arrange are pages from a given PDF file (let's call it foobar.pdf).

# Preparing the tcbraster box

In this example, we prepare a tcbraster box containing thumbnails for pages 1 to 11 of foobar.pdf, arranged in four columns using English reading order (i.e., left to right, then top to bottom).

\documentclass{article}
\usepackage{pgffor}
\usepackage{tcolorbox}
\tcbuselibrary{raster, skins}

% No page numbers. This is useful in case you want to use pdfcrop
% afterwards to extract just the tcbraster box (see below).
\pagestyle{empty}

\begin{document}
\centering
\begin{tcbraster}[raster columns=4, enhanced, blankest]
\foreach \page in {1,...,11} {%
\tcbincludegraphics[graphics options={page=\page}]{foobar}%
}
\end{tcbraster}
\end{document}


Note: the background image sequence in the original file is really A, B, C, A, B, C, A, B, A, B, A (see below for the source); this is not a bug.

The graphics options option of \tcbincludegraphics was used to pass option pages=... to the underlying \includegraphics command, in order to select each page separately.

In case you want to reproduce exactly this screenshot, the foobar.tex file used to produce foobar.pdf can be found in this answer.

# Automatic cropping of the result

The previous code wrote the tcbraster box to a PDF file in letter format (this is the default, since we didn't pass any option such as a4paper to \documentclass, nor did we use a package such as geometry). In case you have the pdfcrop tool, you can automatically produce a PDF file whose format is a tight fit around the tcbraster box (we add a margin of 3bp, where a bp is 1/72 of an inch):

pdfcrop --margins 3 foobar.pdf


This writes the result to foobar-crop.pdf (one could have passed an additional argument to pdfcrop in order to specify the name of the output file).

Finally, one can create a PNG file at resolution, e.g., 384 dpi, from this foobar-crop.pdf:

pdftoppm -png -r 384 foobar-crop.pdf >foobar-crop.png


Don't forget the redirection (the >foobar-crop.png` part)!
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.691367506980896, "perplexity": 2204.758915528671}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-35/segments/1566027316021.66/warc/CC-MAIN-20190821131745-20190821153745-00223.warc.gz"}