Search code examples
latexbeamer

Put two images with captions side by side and control their height in Latex


I want to align two images in a latex/beamer presentation. They do not have the same width/height ratio and I'd like to control the overall height.

If I don't need captions, the following works fine :

\includegraphics[height=0.35\textheight]{im1.png}
\hfill
\includegraphics[height=0.35\textheight]{im2.png}

Note here that I don't need to input the image width, and I don't want to.

But as soon as I need to put a caption, using two figure environments put the figures on two lines. The solution would be to insert the two figure environments into minipages but I would then have to compute the width of the two minipages to be in accordance with the averall height I want.

Is it possible to avoid calculating the width of the minipages?


Solution

  • You can put your images in a tabular.

    You can add small captions and they will adapt to the width of your column.
    The only problem is when your caption spans on several lines. In that case, columntype must be p, but it is not possible to define or change column type to a paragraph without knowing its width.

    One solution is to use tabulary. It is basically identical to tabular* and requires the total width of the table. But columns will be adjusted to the larger natural width of their cells and formatted as paragraphs depending on the column specifier : centered (C), left ragged (L), right (R) or justified (J).

    Here is an example that illustrates both methods.

    \documentclass[array]{article}
    
    \usepackage{graphicx}
    \usepackage{tabulary}
    
    \begin{document}
    \begin{tabular}{cc}
      \includegraphics[height=0.25\textheight]{mushr1}
      &
      \includegraphics[height=0.25\textheight]{mushr2}
       \\                                                     
       Mushroom 1&Mushroom 2
     \end{tabular}
    
    \begin{tabulary}{\linewidth}{CC}
      \includegraphics[height=0.25\textheight]{mushr1}
      &
      \includegraphics[height=0.25\textheight]{mushr2}
       \\                                                     
       Look how beautiful are these mushrooms!&
       Some others beautiful mushrooms. But these ones are very dangerous. Never eat them!
     \end{tabulary}
    \end{document}
    

    enter image description here

    Centering and justification are far from perfect, but this may be a starting point.