6

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.

  • 1
    Of course you cannot use max-height=\textheight, then there is no room left for the caption. Perhaps something like max height=0.9\textheight,max width=\textwidth,keepaspectratio is better for your needs – daleif Feb 6 '15 at 16:55
  • Also if you use the export (AFAIR) option for adjustbox those options also apply directly to \includegraphics – daleif Feb 6 '15 at 16:55
3

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

  • Thank you for your answer, but I have issues with adjustbox. I edited my first post with more details. – Zoxume Feb 6 '15 at 16:47
0

The order of the used keys is important. As you create a float the resizing must be done beforehand, i.e. max height first, then define the caption and finally tell adjustbox that you want a figure (figure key or float=figure). If you use the caption key after figure it comes to late. max height after figure will create a figure environment inside a \resizebox which does not work.

You should use \textheight-\baselineskip as maximum height as you still need space for the caption (increase to 2\baselineskip if you have a two line caption and so on).

Note that there is also \adjustimage which reduces the amount of code.

\documentclass{article}

\usepackage{adjustbox}
\usepackage{mwe}% for example text and image only

\begin{document}
\blindtext

\begin{adjustbox}{max height=\textheight-\baselineskip,center,caption={Some text},float=figure}
  \includegraphics{example-image-a3}
\end{adjustbox}
% or just
%\adjustimage{max height=\textheight-\baselineskip,center,caption={Some %text},float=figure}{example-image-a3}

\blindtext

\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.