2

The original question is about placing figures inside a tabular and trying to adjust them like this:

2

with a code similar to this:

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\centering\arraybackslash}p{17.5mm}|>{\centering\arraybackslash}p{17.5mm}|c|}
  \includegraphics[width=35mm]{example-image-a} &   \multicolumn{2}{c|}{\includegraphics[width=35mm]{example-image-b}} &  \includegraphics[width=35mm]{example-image-c} \\
\multicolumn{2}{|c}{\includegraphics[width=35mm]{example-image-a}} & \multicolumn{2}{|c|}{\includegraphics[width=35mm]{example-image-b}}
\end{tabular}
\end{document}

That for some reason gives this output:

3

Why this happens?

Of course there are better options for placing the figures like that and the answer in the linked question is one of them. But trying to develop the code with the tabular will help as to understand some things about the additional cell that if will be used in combination with \multicolumn command will reduce our columns and this can be unexpected!

1
  • I would ask to reviewers to scroll down and read the PS before flagging/recommending deletion because I think it is a useful post and that if one (of double) answers should be deleted... the linked answer is sure less useful at that place. Thanks.
    – koleygr
    Mar 17, 2019 at 0:19

2 Answers 2

2

You should be aware of the recent addition of the w column type in the package array: for columns of fixed width with center alignment (and no flowing text), you can use

w{c}{17.5mm}

instead of the clumsy >{\centering\arraybackslash}{p{17.5mm}.

Now back to your problem.

The second column is merged in both rows: first with the third column, then with the leading one. So, by rule, the table is built as if that column had never been specified to begin with. This explains your output.

You want six equal columns:

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}

\begin{tabular}{@{} *{6}{w{c}{17.5mm}} @{}}
  \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image}} &
  \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image}} &
  \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image}} \\
  &
  \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image}} &
  \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image}} &
\end{tabular}

\end{document}

enter image description here

3
  • Thanks @egreg... I am going to check your answer soon...
    – koleygr
    Mar 17, 2019 at 18:22
  • Thanks... Just found the time to check your answer (+1). The sentence "The second column is merged in both rows: first with the third column, then with the leading one. So, by rule, the table is built as if that column had never been specified to begin with. " is actually tru in your code too, but your code works! I think that this situation (of the question) is funny but nice for thinking like LaTeX is "thinking" because the first impression you have when merging cells in general, is the impression that have been created by Word-like applications... And this is not the way that LaTeX works!
    – koleygr
    Mar 18, 2019 at 18:43
  • Thanks for the w column too, I didn't knew but I haven't a machine to test it. Anyway, here the column could be p with exactly the same result... I just used it in my answer for the "debugging" part.
    – koleygr
    Mar 18, 2019 at 18:46
1
  1. First step...

    Ok, I have 5 figures and I need 3 on top and two on bottom, I want to control their placement... A tabular seems a good solution to me...

Let's think:

Three on top: {ccc} First row normal centered and I will split cells via \multicolumn in second to share the space... But...

\begin{tabular}{ccc}
\include..&\include..&\include..\\
\multicolumn{2}{c}&\multicolumn{2}{c}
\end{tabular}

Oh! writing the second \multicolumn I realized that they became 4 columns... Don't even compiling...

Seems I need a fourth column:

Let's try:

  1. Second step... four columns:

I will split the top middle columns with \multicolumn:

\begin{tabular}{cccc}
\include...&\multicolumn{2}{c}{\include...}&\include...\\ %Nice... Let's finish with the next row
\multicolumn{2}{c}{\include...}&\multicolumn{2}{c}{\include}
\end{tabular}

4 in the up row 4 in the next... perfect! Let's compile

enter image description here

Seems I didn't count correct... Lets count...

  • Above row 4 cells middle 2 merged
  • Below row 2 & 2 cells...

Why first cell of second row doesn't merge with the second? This should give the fourth figure centered in two first columns like fifth... Why?

Do I need to add a fifth column?

Lets see:

\begin{tabular}{cccc}
1&2&3&4\\
\include...&\multicolumn{2}{c}{\include...}&\include...\\ %Nice... Let's finish with the next row
\multicolumn{2}{c}{\include...}&\multicolumn{2}{c}{\include}
\end{tabular}

enter image description here

Fourth image stops before 2nd column... Lets think... Do I need 5 columns?

  1. Third step... Let's see the vertical lines there? before reporting a bug... But I should!

Full code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\begin{document}
\begin{tabular}{|c|c|c|c|}
1&2&3&4\\
  \includegraphics[width=35mm]{example-image-a} &   \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image-b}} &  \includegraphics[width=35mm]{example-image-c} \\
\multicolumn{2}{|c}{\includegraphics[width=35mm]{example-image-a}} & \multicolumn{2}{|c|}{\includegraphics[width=35mm]{example-image-b}}
\end{tabular}
\end{document}

enter image description here

They need the bug report... But...

Ok... I think I got it:

The second column has no the width of the third... Of course... No width needed there... Lets add a width:

... Ok I will add centered 17.5mm in both the middle columns:

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\centering\arraybackslash}p{17.5mm}|>{\centering\arraybackslash}p{17.5mm}|c|}
  \includegraphics[width=35mm]{example-image-a} &   \multicolumn{2}{c|}{\includegraphics[width=35mm]{example-image-b}} &  \includegraphics[width=35mm]{example-image-c} \\
\multicolumn{2}{|c}{\includegraphics[width=35mm]{example-image-a}} & \multicolumn{2}{|c|}{\includegraphics[width=35mm]{example-image-b}}
\end{tabular}
\end{document}

enter image description here

Why me??? Let's ask at stackexchange... I am out of here!

  1. No more steps: Some explanation:

Let's add the 1&2&3&4 row back there:

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\centering\arraybackslash}p{17.5mm}|>{\centering\arraybackslash}p{17.5mm}|c|}
1&2&3&4\\
  \includegraphics[width=35mm]{example-image-a} &   \multicolumn{2}{c|}{\includegraphics[width=35mm]{example-image-b}} &  \includegraphics[width=35mm]{example-image-c} \\
\multicolumn{2}{|c}{\includegraphics[width=35mm]{example-image-a}} & \multicolumn{2}{|c|}{\includegraphics[width=35mm]{example-image-b}}
\end{tabular}
\end{document}

enter image description here

Perfect... But what happened?

LaTeX used just 3 columns since the maximum of the cells needed there was 3 per row... So, the first column was a single column and not really a multicolumn... By adding these help numbers... we are actually lucky and found out what happened...

Let's see it in a simpler example:

\documentclass{article}
\usepackage{graphicx}
\usepackage{parskip}
\usepackage{array}
\setlength{\parskip}{0.5cm}

\begin{document} 


Just four columns:

\begin{tabular}{|c|c|c|c|}\hline
test&\multicolumn{2}{c|}{test}&test\\\hline
test&test&test&test\\\hline
\end{tabular}

\vspace{1cm}

If we decide to replace with a \verb|\multicolumn| the first two columns of the second row\ldots we can (inocently) imagine the above table to be like:

\begin{verbatim}
    | test |     test     | test |
    ----------------------------
    |     test     | test | test |
\end{verbatim}

BUT we get:

\begin{tabular}{|c|c|c|c|}\hline
test&\multicolumn{2}{c|}{test}&test\\\hline
\multicolumn{2}{|c|}{test}&test&test\\\hline
\end{tabular}

Because now the first \verb|multicolumn| makes the first row three columns size and the second places the ``\verb|multicolumned|'' cells as one cell under the first cell of the first row!
\end{document}

enter image description here

So, in our previous "failed" case we can:

add a full row with empty cells (but all the cells)...

Then remove the extra vertical space of the line with [-1em] ...

Also, with some simple thoughts the second row cells should not be centered because of the extra width of the first and last column... May be r and l is a better choice ... and a \tabcolsep fixed width would help... same distance too (as \tabcolsep between rows):

\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\setlength{\tabcolsep}{1mm}
\begin{tabular}{c>{\centering\arraybackslash}p{17.5mm}>{\centering\arraybackslash}p{17.5mm}c}
    &&&\\[-1em]
\includegraphics[width=35mm]{example-image-a} &   \multicolumn{2}{c}{\includegraphics[width=35mm]{example-image-b}} &  \includegraphics[width=35mm]{example-image-c} \\[1mm]
\multicolumn{2}{r}{\includegraphics[width=35mm]{example-image-a}} & \multicolumn{2}{l}{\includegraphics[width=35mm]{example-image-b}}
\end{tabular}
\end{document}

enter image description here

But why didn't made it in a simpler tabular (of 3 columns)?

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\setlength{\tabcolsep}{1mm}
\begin{tabular}{ccc}
\includegraphics[width=35mm]{example-image-a}& \includegraphics[width=35mm]{example-image-b}&\includegraphics[width=35mm]{example-image-c}\\[1mm]
\multicolumn{3}{c}{\includegraphics[width=35mm]{example-image-a}\hspace{2mm}\includegraphics[width=35mm]{example-image-b}}
\end{tabular}
\end{document}

Nice... Clever!

But @egreg's answer is more clever!!! At least I found the problem!

PS: My "double post" needs moderator attention ... but I think it is an exception and have to offer to this site since the first question was about placing figures and I just found out I have something to say here that didn't found somewhere else and it is useful and about tabulars with \multicolumn... If one of the two posts should be deleted I suppose that deleting the first one (my answer on the original question) would be a better choice because here seems more useful for me. So, please (reviewers) add a comment before flagging for deletion.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .