[Tex/LaTex] How to make a picture taking the maximum space

floatsgraphicsscale

To expand a picture imported with \includegraphics that is wider than heigh to the width of the text block, you just do this:

\includegraphics[width=\textwidth]{ ... }

When they are heigher than wide, you could do pretty much the same with the text height:

\includegraphics[height=\textheight]{ ... }

However, because pictures are often included in floats, there is a caption. This caption has a certain height and \includegraphics[height=\textheight]{ ... } make the figure overflow in the margin.

This leads to the following:

  1. Is there a way to tell to \includegraphics that the height of the picture must be the difference between \textheight and the height of the caption (with the needed space around of course)?
  2. How to generalise this kind of behaviour to the case where there are several floats or subfloats in the same “row” or “column” by saying to \includegraphics something like “take the maximum allowed space”?

I think it could be more general and maybe someone else could expand or precise this.


daleif suggested to use the adjustbox package. It is very interesting and gives access to the maximum dimensions of pictures.

I tried two interesting things:

  1. To use \adjincludegraphics in place of \includegraphics inside a figure environment:

    \begin{figure}
      \adjincludegraphics[max height=\textheight]{ ... }
    \caption{Some text}
    

    I also tried max totalheight, but I don’t see a difference (the manual doesn’t say anything about it.) This doesn’t work: the picture is scaled but the caption is still outside the margins.

  2. To use the adjustbox environment which can emulate floats:

    \begin{adjustbox}{float=figure,caption={Some text}, max height=\textheight}
      \includegraphics{ ... }
    \end{adjustbox}
    

    This does not work unless I remove float=figure (and then there is no caption.) The figure (alone) option doesn’t work either. With float=figure, there is absolutely nothing appearing in the document but a blank page.

I think the second solution is better, but I can’t get it working properly.

Best Answer

See the adjustbox package, it can add features to \includegraphics, including an max width/max height option that might be useful in your case.