[Tex/LaTex] Responsive collage of pictures

subcaptionsubfloats

I need to make a collage of picture responsive. Below my current MWE, I cannot use any environemnt but subcaption as this is the guideline of my university:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}
    \begin{figure}
        \begin{subfigure}{.33\textwidth}
            \centering
            \includegraphics[width=.8\linewidth]{figures/cool/flock.jpg}
            \caption{1a}
            \label{fig:sfig1}
        \end{subfigure}%
        \begin{subfigure}{.33\textwidth}
            \centering
            \includegraphics[width=.8\linewidth]{figures/cool/whale_sound.jpg}
            \caption{1b}
            \label{fig:sfig2}
        \end{subfigure}
        \begin{subfigure}{.33\textwidth}
            \centering
            \includegraphics[width=.8\linewidth]{figures/cool/wolfe_hierarchies.png}
            \caption{1c}
            \label{fig:sfig3}
        \end{subfigure}
        \caption{plots of....}
        \label{fig:fig}
    \end{figure}
\end{document}

That is giving me the following result:
enter image description here

Instead, I would like to have something like that:
enter image description here

As you can see the figure 1b is bigger and 1c has been moved to the bottom. So, right now I am doing that changing the width of each subfigure environment. Is there a way to do it dynamically, hopefully, according to the height of all the images? Moreover, I can I center image 1c in the second example?

Below the code for example 2:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}
    \begin{figure}
        \begin{subfigure}{.33\textwidth}
            \centering
            \includegraphics[width=.8\linewidth]{figures/cool/flock.jpg}
            \caption{1a}
            \label{fig:sfig1}
        \end{subfigure}%
        \begin{subfigure}{.66\textwidth}
            \centering
            \includegraphics[width=.8\linewidth]{figures/cool/whale_sound.jpg}
            \caption{1b}
            \label{fig:sfig2}
        \end{subfigure}
        \begin{subfigure}{.33\textwidth}
            \centering
            \includegraphics[width=.8\linewidth]{figures/cool/wolfe_hierarchies.png}
            \caption{1c}
            \label{fig:sfig3}
        \end{subfigure}
        \caption{plots of....}
        \label{fig:fig}
    \end{figure}
\end{document}

Best Answer

You can try this, it works as follows :

  • A length \imagewidth is created to store the image width at each call
  • A macro \subgraphics taking two input arguments (image file and subfigure caption) stores the width of the input image file for a height of 2cm in this case. Then it creates a subfigure that exactly fits this width and includes the file.

With this command, the width of the subfigure derives from the width of the image for a given image height.

I would personally not use such a process, as it may result in unreadable information.

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\newlength{\imagewidth}
\newcommand{\subgraphics}[2]{
\settowidth{\imagewidth}{\includegraphics[height=2cm]{#1}}%
\begin{subfigure}{\imagewidth}%
    \includegraphics[height=2cm]{#1}%
    \caption{#2}%
\end{subfigure}%
}

\begin{document}
\begin{figure}
        \centering
        \subgraphics{example-image-a}{1a}
        \subgraphics{example-image-b}{1b}
        \subgraphics{example-image-c}{1c}
        \caption{plots of....}
        \label{fig:fig}
    \end{figure}    

\end{document}