# Side-by-side figures with caption, specify height but use whole textwidth

I found this answer which does exactly what I want except the fact that I can't use a caption for my figures:

Forcing subfigures to have same height and take overall X% of linewidth in LaTeX

I want two figures side by side. The problem I usually have is that they are in different sizes and that it takes alot of time to scale them manually to the same height, use the whole text width and still keep the ratio.

I tried to use this command, but it wont work as I get the error "Missing \endgroup inserted".

\resizebox{\textwidth}{!}{%
\begin{figure}[H]
\includegraphics[height=4cm,fbox]{testfigure_1.jpg}%
\caption{My figure 1}
\end{figure}
\begin{figure}[H]
\includegraphics[height=4cm,fbox]{testfigure_2.jpg}%
\caption{My figure 1}
\end{figure}
}


I have realised that I of some reason cannot put an figure enviroment within resizebox. Still I cannot solve this.

I don't know if this matter to your answers but in my code I use this to center all captions for my figures: \usepackage[justification=centering]{caption}

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}% added for test in parallel with @egreg solution

\usepackage{showframe}% only for show page layout. in real use it had to be omitted!

% new lengths and save boxes
\newlength{\twosubht}
\newsavebox{\twosubbox}
\newlength{\firstfig}
\newsavebox{\firstfigbox}
\newlength{\secondfig}
\newsavebox{\secondfigbox}

\begin{document}
\begin{figure}[htp]
% measurement of height
\sbox\twosubbox{\resizebox{\dimexpr.9\textwidth-1em}{!}{%
\includegraphics[height=3cm]{example-image-a}%
\includegraphics[height=3cm]{example-image-16x9}%
}%
}
\setlength{\twosubht}{\ht\twosubbox}
% measurement of width
\sbox\firstfigbox{\includegraphics[height=\twosubht]{example-image-a}}%
\setlength{\firstfig}{\wd\firstfigbox}
\sbox\secondfigbox{\includegraphics[height=\twosubht]{example-image-16x9}}%
\setlength{\secondfig}{\wd\secondfigbox}
% figure
\centering
\begin{tabular}{p{\firstfig}p{\secondfig}}
\usebox\firstfigbox
\caption{The first}
&   \usebox\secondfigbox
\caption{The second}
\end{tabular}
\end{figure}

% original @egreg solution

\noindent\hrulefill The text width\hrulefill
\begin{center}
\makebox[.9\textwidth]{\hrulefill 90\% of text width\hrulefill}
\end{center}

\begin{figure}[htp]
% preliminary
\sbox\twosubbox{%
\resizebox{\dimexpr.9\textwidth-1em}{!}{%
\includegraphics[height=3cm]{example-image-a}%
\includegraphics[height=3cm]{example-image-16x9}%
}%
}
\setlength{\twosubht}{\ht\twosubbox}

% typeset

\centering

\subcaptionbox{First\label{f}}{%
\includegraphics[height=\twosubht]{example-image-a}%
\subcaptionbox{Second\label{s}}{%
\includegraphics[height=\twosubht]{example-image-16x9}%
}

\caption{The caption}
\end{figure}
\end{document}


First compiling is slow due to figure size adjustment and figure size measurements.

Edit: For comparison the size of the figures, I copy @egreg solution from given link into my answer. As expected, the size of figures are the same (this is response to comment, that figures are not the same ...).

I also recommended to use geometry package for determining page layout (no used in above MWE).

• I removed the package "showframe" and added "fbox" in include graphics to make the border right. This is somewhat what I searched for except the fact that it does not use the whole textwidth. In the answer which I linked, the figures take up alot more of the page, this answer's figures are alot more compressed. – Robin Hellmers Apr 25 '16 at 7:25
• Package showframe is only for orientation about page layout (that you see, how the figures are fit in text width) and in real use you had to remove them! Use \fbox{...} is up to you, if you liked to have, i will not use it. The image is not more compressed as in @egreg answer (their appearance is copied from there), however the presented image is (cut-off from a screen) is smaller and maybe you have visual impression for this. Also I use default width of text. Put the both solution in the same page and then compare again. – Zarko Apr 25 '16 at 7:43
• Thank you for helping. I did put both solutions over eachother in my latex-file and saw that this version was more compressed than the other one. I believe it could have to do with me using this package: \usepackage[top=1in, bottom=1in, right=1in, left=1in]{geometry} – Robin Hellmers Apr 25 '16 at 7:52
• This is how it shows: i.imgsafe.org/e59e8ae.png – Robin Hellmers Apr 25 '16 at 7:58
• I extend my answer with proof that your assertion about image size is wrong. – Zarko Apr 25 '16 at 8:10

Perhaps something like this:

\documentclass[12pt]{article}
% the showframe option is used to visualize the output
\usepackage[showframe]{geometry}
% packages mwe and lipsum are simply for this example
\usepackage{mwe, lipsum, graphicx, float}
\usepackage[justification=centering]{caption}
\usepackage{subfig}

\newlength\halfwd
\newlength\halfwdsep
\setlength{\halfwdsep}{1cm}
\setlength{\halfwd}{\dimexpr 0.5\textwidth - 0.5\halfwdsep \relax}
\newcommand\setfigsep{\hspace*{\halfwdsep}}

\begin{document}

\lipsum[1]

\begin{figure}[H]
\centering
\subfloat[Example A]{%
\label{figa}%
\includegraphics[width=\halfwd, height=4cm]{example-image-a}%
}
%
\setfigsep
%
\subfloat[Example B]{%
\label{figb}%
\includegraphics[width=\halfwd, height=4cm]{example-image-b}%
}
\caption{Examples A (\ref{figa}) \& B (\ref{figb})}
\label{fullfig}
\end{figure}

Please consult Figure \ref{fullfig}; that is, please consult both Figure \ref{figa} and Figure \ref{figb}.

\lipsum[2]

\end{document}


Adjust \halfwdsep to your liking.