1

I would like to create a new command to easily insert 4 pictures that will be arranged in a matrix, but I don't know wich separator to use in the main file. I always get the latex error "unknow graphics extension .jpg;pix... I tryed it with comma and others as well. Or am I doing something else wrong?

MnWE:

\documentclass{book}

\usepackage{graphicx}

\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{positioning} %required for relative positioning of nodes

%-------------------

\newcommand\twoXtwo[4]{
    \clearpage
    \begin{tikzpicture}[remember picture,overlay]
        \node [matrix] (matrix2x2) at (current page.center) {
            \node {\includegraphics[width=0.5\textwidth]{#1}}; & \node{\includegraphics[width=0.5\textwidth]{#2}}; \\
            \node {\includegraphics[width=0.5\textwidth]{#3}}; & \node{\includegraphics[width=0.5\textwidth]{#4}}; \\
            };
     \end{tikzpicture}
}

%-------------------

\begin{document}

\twoXtwo{pix1.jpg;pix2.jpg; pix3.jpg; pix4.jpg}

\end{document}
acep
  • 171
  • 1
    In principle you could just do \documentclass{book} \usepackage{graphicx} \newcommand\twoXtwo[4]{ \clearpage \begin{tabular}{c@{}c} \includegraphics[width=0.5\textwidth]{#1} & \includegraphics[width=0.5\textwidth]{#2} \\ \includegraphics[width=0.5\textwidth]{#3} & \includegraphics[width=0.5\textwidth]{#4} \\ \end{tabular} } \begin{document} \twoXtwo{example-image}{example-image-a}{example-image-b}{example-image-duck} \end{document} –  Feb 07 '19 at 16:37

1 Answers1

6

A couple of things:

  • I would recommend using the matrix tikz library as it was designed for this purpose. This defines the \matrix command which works like an array in maths mode.
  • When calling the twoXtwo command each argument should be in its own braces (this was what LaTeX was complaining about).
\documentclass{book}

\usepackage[draft]{graphicx} % Draft option for placeholder images

\usepackage{tikz} % Required for drawing custom shapes
\usetikzlibrary{positioning} %required for relative positioning of nodes
\usetikzlibrary{matrix} % Matrix library
%-------------------

\newcommand\twoXtwo[4]{
    \clearpage
    \begin{tikzpicture}[remember picture,overlay]
    % Matrix of nodes. N.B. ampersand replacement (see below)
    \matrix [matrix of nodes,ampersand replacement=\&] at (current page.center) { 
            \includegraphics[width=0.5\textwidth]{#1} \& \includegraphics[width=0.5\textwidth]{#2} \\
            \includegraphics[width=0.5\textwidth]{#3} \& \includegraphics[width=0.5\textwidth]{#4} \\
            };
     \end{tikzpicture}
}

%-------------------

\begin{document}
\twoXtwo{foo.png}{foo.png}{foo.png}{foo.png} % Arguments in individual braces
\end{document}

You would use \twoXtwo{pix1.jpg}{pix2.jpg}{pix3.jpg}{pix4.jpg}, assuming each of these images is in the same directory as your main .tex.

Note the use of ampersand replacement=\&. This is to avoid the error described here.

Output:

example output

pip
  • 1,847
  • thanks! I totally forgot to load the \usetikzlibrary{matrix} that was the problem, so it didn't work in extra braces either, bu now it is working fine. Good idea with the ampersand replacement=& and the [draft] option for graphicx is nice too! In my real document I have an extra path for the pix, but globally defined, so that is fine. – acep Feb 08 '19 at 08:16