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

3

6

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?

Commorancy answered 7/6, 2019 at 12:40 Comment(0)
D
7

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.

Duplication answered 7/6, 2019 at 13:44 Comment(0)
R
6

To have real captions in beamer format, the varwidth package can help:

\documentclass{beamer}
\usepackage{varwidth}

\begin{document}

\begin{frame}
    \begin{varwidth}{\textwidth}
        \begin{figure}
            \includegraphics[height=0.45\textheight]{example-image-duck}
            \caption{text}
        \end{figure}
    \end{varwidth}
    \hfill
    \begin{varwidth}{\textwidth}
        \begin{figure}
            \includegraphics[height=0.45\textheight]{example-image-golden-upright}
            \caption{text}
        \end{figure}
    \end{varwidth}
\end{frame} 

\end{document}

enter image description here

Roney answered 4/7, 2019 at 15:25 Comment(0)
B
0

In Beamer, I have tried both \column and \minipage environmnet to put figures side by side. But didn't work properly and issues with alignments. Finally, following solution worked by using \usepackage{subfigure}.

\begin{figure}
    \centering
    \mbox{\subfigure{\includegraphics[width=5.5cm, height=4.8cm]{suppopicture3}}\quad
        \subfigure{\includegraphics[width=3.0cm, height=4.5cm]{Figure 3_HR-TEM_result} }}
    \caption{Text pertaining to both graphs ...} \label{fig12}
\end{figure}

Ref:- https://www.johndcook.com/blog/2009/01/14/how-to-display-side-by-side-figurs-in-latex/

Bluetongue answered 21/11, 2020 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.