size figures according to tabular column width

I'm putting together a multi-panel figure which will also eventually get some row and column labels. In order to do this, I think the best approach for the skeleton of the structure is to \includegraphics inside a tabular environment.

At the moment I have something like this (consider this a 'pseudo-MWE'):

\begin{figure}[p]
\begin{tabular}{cccc}
\includegraphics[width=0.24\textwidth]{image1.pdf} &%
\includegraphics[width=0.24\textwidth]{image2.pdf} &%
\includegraphics[width=0.24\textwidth]{image3.pdf} &%
\includegraphics[width=0.24\textwidth]{image4.pdf} \\%
\end{tabular}
\end{figure}


I'm wondering if there is some way to express the overall dimensions (i.e. the table width) in the tabular environment, at present it spans the \textwidth as I'd like it to; but also be able to express the individual image dimensions in terms of the cell width without specifying it explicitly as above?

Ideally I'd end up with each column of the table equating to roughly 0.25*\textwidth, and then the images to fit themselves accordingly, as if I was calling something like:

\includegraphics[width=\columnwidth]{myimage.pdf}...

• The table width changes according to the contents of the table, so usually you don't know the width of the table until it's complete. If you use p column types, though, you can refer to the width of the column as \linewidth. Sorry, I don't know a better approach with tabular. – Phelype Oleinik May 8 '18 at 14:41
• OK, thanks, that's got me to a workable solution for now, I just specify 4 p columns which are 0.25\textwidth, and then \linewidth works as you say. I'll be interested to see if there is a 'proper' or more elegant solution though. – Joe Healey May 8 '18 at 14:49
• The expression \begin{tabular}{\textwidth}{cccc} can't be correct, as the tabular environment takes 1 argument, not 2. Did you mean to write \begin{tabular}{cccc}? – Mico May 8 '18 at 15:10
• Yes, sorry I just noticed that myself, I'll edit. – Joe Healey May 8 '18 at 15:11

You could use tabularx:

\documentclass{article}
\usepackage{tabularx}
\usepackage{graphicx}
\begin{document}
\begin{figure}[p]
\setkeys{Gin}{width=\linewidth}
\begin{tabularx}{\textwidth}{XXXX}
\includegraphics{example-image} &
\includegraphics{example-image} &
\includegraphics{example-image} &
\includegraphics{example-image} \\
\end{tabularx}
\end{figure}
\end{document}

• Interesting, never seen setkeys before. What's going on in that line? – Joe Healey May 8 '18 at 14:56
• It sets the key width for the group (ie. environment figure) to a default value. See the documentation of package graphicx (grfguide.pdf). – esdd May 8 '18 at 15:01
• As a bonus this solved an extra problem I was having. tabular didn't like me trying to put text in the cells above the figures for some reason (messing up cell length calculation I figure?), but tabularx has no such qualms. Thanks! – Joe Healey May 8 '18 at 15:07