0

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}
  • You can change \includegraphics options to be heigth=<someheight> instead of width=<somewidth>. If you want to center the subfigures inside the figure, add a \centering after \begin{figure} – BambOo Nov 15 '18 at 12:14
  • Ok, but I still need to change the width of the subfigure at the end. – Guido Muscioni Nov 15 '18 at 12:18
  • Then I do not think I totally understand your issue. Can you clarify ? – BambOo Nov 15 '18 at 12:22
  • Sure, so the second example is achieved modifying the line: \begin{subfigure}{.66\textwidth} with actually setting the width to be double the width of the others figures. I would like to make this process automatic. An example, saying to latex that I want the figures with 2cm height and in a collage, an let latex arrange them to keep the properties. – Guido Muscioni Nov 15 '18 at 12:25
  • Do you always want a 2 + 1 organisation, or does it have to work in the geneal case ? – BambOo Nov 15 '18 at 12:29
2

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.