2

In order to get a figure made by two different size images automatically scaled to the same height and to full text width, with a single caption, I tried to adapt some examples from floatrow package. Unfortunately, as shown by the following MWE, the results were unsatisfactory. In one case the caption is missing, in the other there is no separation between the two images.

\documentclass{article}
\usepackage{mwe}
\usepackage{floatrow}

\begin{document}

\begin{figure}[htb]
\CommonHeightRow{\begin{floatrow}
\ffigbox[\FBwidth]{\includegraphics[height=\CommonHeight]{example-image-16x10}}{}
\ffigbox[\FBwidth]{\includegraphics[height=\CommonHeight]{example-image-10x16}}{}
\end{floatrow}}
\caption{This is a caption.}
\end{figure}

\blindtext

\begin{figure}[htb]
\CommonHeightRow{\begin{floatrow}
\ffigbox[\FBwidth]{\includegraphics[height=\CommonHeight]{example-image-16x10}}{\includegraphics[height=\CommonHeight]{example-image-10x16}}
\end{floatrow}}
\caption{This is a caption.}
\end{figure}

\end{document}

![enter image description here

I know that there are many ways to obtain the desired result, but I found none, apart from floatrow, able to automatically scale two different size images to same height and full text width.

Thanks in advance for any help.

Follow up

To summarize, a great solution to obtain automatically scaled double image figures that addittionally let you choose the total width (90% of text width in the following snippet) can be

\newlength\mylength
\begin{adjustbox}{width=0.9\textwidth,center=\textwidth,caption={This is a caption.},label={some:label},figure=htb}
\adjustimage{gstore height=\mylength}{example-image-16x10}%
\hspace{1em}%
\adjustimage{height=\mylength}{example-image-10x16}%
\end{adjustbox}%

which has the only drawback that separation between images changes slightly depending on images aspect ratio (other solutions addressing such drawback are welcome!). If you absolutely want a fixed separation you can stick with a more traditional solution in which you have to manually set the height, usually by iterative tentatives:

\begin{figure}[htb]
\centering
\includegraphics[height=0.4\textwidth]{example-image-16x10}%
\hspace{1em}%
\includegraphics[height=0.4\textwidth]{example-image-10x16}%
\caption{This is a caption.}%
\label{some:label}%
\end{figure}

Here is a MWE.

  • 1
    Please use example-image-a and example-image-b (and others) for code examples. These are provided by the mwe package on CTAN. This way others can easily compile your code examples without the need for the demo option. – Martin Scharrer Apr 17 '18 at 9:55
  • 1
    If you need different image sizes just use example-image-16x10 and example-image-10x16. See mwe package for more details. – Martin Scharrer Apr 17 '18 at 9:59
3

You can use the adjustbox package for this. Just use gstore height to store the original height of the first image, scale the second image height to it and then scale the whole thing to the wanted width. Use a \hspace to get some distance between the images (which will be scaled however!).

You can even get rid of the figure environment by using the figure key of the outer adjustbox.

\documentclass{article}
\usepackage{adjustbox}
\usepackage{lipsum}

\newlength\mylength

\begin{document}

\lipsum

\begin{figure}[htb]
\begin{adjustbox}{width=\linewidth}
\adjustimage{gstore height=\mylength}{example-image-16x10}%
\hspace{1em}%
\adjustimage{height=\mylength}{example-image-10x16}%
\end{adjustbox}
\caption{This is a caption.}
\end{figure}

\lipsum


\begin{adjustbox}{width=\linewidth,rotate=0,caption={This is a caption},figure=htb}
\adjustimage{gstore height=\mylength}{example-image-16x10}%
\hspace{1em}%
\adjustimage{height=\mylength}{example-image-10x16}%
\end{adjustbox}
% Note the `rotate=0` key is required for `adjustbox` v1.0 to enforce the resizing to happen before the `figure` environment is applied, not after. Starting with v1.1 this is done automatically.

\lipsum

\end{document}

enter image description here

  • 1
    Also, you can use the margin key to add some white space to the right of the first image. This way you don't need to use \hspace. – Martin Scharrer Apr 17 '18 at 10:34
  • The solution works perfectly for full width, but if I set a fixed margin (say 4em) I get different figure widths across the document, probably due to variable aspect ratio of images. The best solution would be to use a reduced width for adjustbox environment (say 0.8\linewidth) and center the figure, but I could not make center option work. – mmj Apr 17 '18 at 13:09
  • 1
    @mmj: Try \textwidth instead of \linewidth, it should be more constant. The center option needs to be placed after width (but before any figure key). Best use explictly center=\textwidth. – Martin Scharrer Apr 17 '18 at 21:38
2

Maybe using adjustbox would be a workarund?

\documentclass{article}
\usepackage{graphicx}
\usepackage{adjustbox}

\begin{document}

\begin{figure}[htbp]
\begin{adjustbox}{width=\linewidth}
    \includegraphics[height=1cm]{example-image}
    \includegraphics[height=1cm]{example-image-16x10}
\end{adjustbox}
\caption{caption}
\end{figure}


\end{document}

enter image description here

Drawback: if your images do not contain white space, you have to manually insert space between the images, e.g. by using \quad or \hspace{...}. This space will also be scales, so may be different for different images.

  • Interesting solution for two images figures, but it's not what I'm looking for, because you have to provide the height. – mmj Apr 17 '18 at 10:05
  • @mmj: Well, the height is scaled anyway due to the width scaling. As all scaling does not change the quality this is not a problem. The only problem might be if you need to scale a very small image to a very large one, so that the scaling factor is to large. However, in that case providing a fixed height for the first scaling is actually better! – Martin Scharrer Apr 17 '18 at 10:12
  • @mmj You don't really have to provide the height, you just have to provide the same length for both images, the value does not matter. – user36296 Apr 17 '18 at 10:28
0

Finally I also found a solution using the floatrow package, here is the MWE:

\documentclass{article}
\usepackage{mwe}
\usepackage{floatrow}
\usepackage{subcaption}

\begin{document}
\blindtext

\begin{figure*}[htbp]
\ffigbox[1.0\linewidth]{}{\CommonHeightRow{\begin{subfloatrow}[2]% this comment needed, otherwise an unprotected space shifts slightly the figure to the right
\ffigbox[\FBwidth]{\includegraphics[height=\CommonHeight]{example-image-10x16}}{\label{fig:01a}}
\ffigbox[\FBwidth]{\includegraphics[height=\CommonHeight]{example-image-16x10}}{\label{fig:01b}}
\end{subfloatrow}}
\caption{A figure caption.}\label{fig:01a+01b}}
\end{figure*}

\blindtext
\end{document}

The total width can be set by changing 1.0\linewidth to any fraction you wish.

This solution is interesting because with the same floatrow package you are able to automatically scale many combinations of multiple images/figures with or without subcaptions, like this one and this one.

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.