2

This question already has an answer here:

I have a lot of figures to put side by side in a document, and they are all slightly different in size (i.e. not all 4:3 format or so).

What I'm doing at the moment is tweak the width=0.XX\textwidth of both until they look more or less the same height.

Here I provide an example code: what I would like to be able to do is have a macro or something, where I can simply provide the two figures and they automatically scale to have the same height and fit on one line. Is that possible? And if yes, how?

Edit: since people are asking why I'm not providing specific sizes for the images: The code should work with any 2 images with any aspect ratio. Give it two images and the code scales them to fit side by side and have the same height, filling the horizontal space available.

\documentclass{scrreprt}
\usepackage{subfig}
\usepackage{graphicx}
\begin{document}
    \chapter{Figures}
    \begin{figure}[h!]
        \centering
        \subfloat[Figure one]{%
            \centering\includegraphics[width=0.45\textwidth]{example-image-a}}
        \qquad
        \subfloat[Figure two with different side proportions]{%
            \centering\includegraphics[width=0.45\textwidth]{example-image-16x9}}
        \caption{How to get the two figures to same height (respecting proportions)?}
    \end{figure}
\end{document}

What I get:

what I get

What I wish to happen automatically:

what I wish to get

marked as duplicate by leandriis, user36296, user156344, Stefan Pinnow, Mensch Mar 10 at 16:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

For the two sample graphs and the default text width and height given by the scrreprt document class, it suffices to replace width=... with height=0.21\textheight, for both subfig groups.

enter image description here

For other combinations of text widths, text heights, and proportions of the pairs of images that need to be placed next to each other, you'll probably have to experiment a bit to find out which value of height=... is about right.

I'm assuming that the objective is to make the pairs of graphs be as large as possible, i.e., span the full width of the textblock. If this assumption is valid, none of the \centering instructions are needed.

\documentclass{scrreprt}
\usepackage{subfig}
\usepackage{graphicx}
\begin{document}
\chapter{Figures}

\begin{figure}[h!]
\subfloat[Figure one]{%
\includegraphics[height=0.21\textheight]{example-image-a}}
\hspace*{\fill}
\subfloat[Figure two with different side proportions]{%
\includegraphics[height=0.21\textheight]{example-image-16x9}}
\caption{How to get the two figures to same height (respecting proportions)?}\end{figure}
\end{document}
  • 1
    The question was how I can get them to the same height and fill the page width without having to experiment a bit :) – Superuser27 Mar 10 at 15:40
  • @Superuser27 - If you rule out experimenting of the type described above, you'll have to "play it safe" by providing more horizontal whitespace than is absolutely necessary. Put differently, what you'll be showing your readers is deliberately less detailed than would be possible with just a bit of experimenting. In my experience, readers always appreciate being shown graphs that are as large as sensibly possible. Why disappoint them by showing them more whitespace instead? Naturally, you're free to do as you please -- and your readers are free to make up their own opinions... – Mico Mar 10 at 15:46
  • Ok I try to rephrase the question: I would like to get the pictures to fill the page width, yet stay side by side (so the sum of their width should be around 0.9\textwidth and they should have the same height. Automatically. :) – Superuser27 Mar 10 at 15:48
  • @Superuser27 - You've also failed to provide any exact information about the aspect ratios of the images, the width of the text block, and height of the text block. If such information were provided, one could in fact come up with a near-optimal calculation of the height=... option for various combinations of graphs. However, you chose not to provide these pieces of information. – Mico Mar 10 at 15:51
  • 1
    @Superuser27: You might be interested in Forcing subfigures to have same height and take overall X% of linewidth in LaTeX – leandriis Mar 10 at 15:55
2

with defining (only) their height. like \includegraphics[height=33mm]{example-image-a}} (select the image height according to your wish).

off-topic: don't use \centering inside of subfloats ...

edit:

In your case, since you like to have in both images the same height to use key Gin as follows in edited code below. To push images apart as much as possible, use \hfill instead of quad.

\documentclass{scrreprt}
\usepackage{subfig}
\usepackage{graphicx}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
    \chapter{Figures}
    \begin{figure}[h!]
    \setkeys{Gin}{height=44mm}
        \subfloat[Figure one]{\includegraphics{example-image-a}}
        \hfill% push sub images apart, so take all the line
        \subfloat[Figure two with different side proportions]{%
            \includegraphics{example-image-16x9}}
        \caption{How to get the two figures to same height (respecting proportions)?}
    \end{figure}
\end{document}

enter image description here

(red lines indicate text borders)

  • why not to use \centering inside a float? – user36296 Mar 10 at 15:37
  • @samcarter, because it hasn't any influence on positioning of image. \subfloat take a size of included image (as far as I know, but i can be wrong). – Zarko Mar 10 at 15:40
  • Why 33mm? How can I get them to fill most of the width as possible? – Superuser27 Mar 10 at 15:41
  • @Superuser27 - How is one supposed to determine how to "fill as much of the width as possible" if one doesn't have any information about (a) the height and width of the text block and (b) the aspect ratios of the images? – Mico Mar 10 at 15:53
  • apparently i din't understand the question correctly, what you like to have... so i left to select desired height to you ... – Zarko Mar 10 at 15:54
1

To get both images to the same height you can store the height of the first image in a dimension \imageheight. Then you can use height=\imageheight for the second image.

The culprit here is that you can not automaticly define the width of both images to fit the textwidth as good as possible. Perhaps an code with lua can do that but until now I did not learn to use lua in TeX ...

So let us do it semi automatic ...

Lets have a look into the code. With the line in the preamble

\newdimen\imageheight

we declare a new dimension \imageheight to store the height of the first image. With the code

\settoheight{\imageheight}{% <==========================================
  \includegraphics[width=0.40\textwidth,keepaspectratio]{example-image-a}%
}

you get the current height of the image for a choosen width of width=0.40\textwidth.

Now you can use the code

\subfloat[Figure one]{%
  \centering\includegraphics[height=\imageheight]{example-image-a}}
\qquad
\subfloat[Figure two with different side proportions]{%
  \centering\includegraphics[height=\imageheight]{example-image-16x9}}

to print the images with same heigth. To get them fitting the textwifth just play with the width for the first image: width=0.40\textwidth. Change the value of 0.40\textwidth to your needs.

The complete code

\documentclass{scrreprt}

\usepackage{subfig}
\usepackage{graphicx}
\usepackage{showframe}

\newdimen\imageheight % to store the actual image height <==============


\begin{document}

\settoheight{\imageheight}{% <==========================================
  \includegraphics[width=0.40\textwidth,keepaspectratio]{example-image-a}%
}

\chapter{Figures}
\begin{figure}[h!]
  \centering
    \subfloat[Figure one]{%
      \centering\includegraphics[height=\imageheight]{example-image-a}} % <=============
    \qquad
    \subfloat[Figure two with different side proportions]{%
      \centering\includegraphics[height=\imageheight]{example-image-16x9}} % <==========
  \caption{How to get the two figures to same height (respecting proportions)?}
\end{figure}
\end{document}

gives you the result:

enter image description here

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