Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Possible Duplicate:
Best figure size adjustment when dealing with different image sizes

Given: a large figure, whose height:width ratio is unknown.
Todo: Include this fig in LaTeX, such that:

  • if the figure is very tall, then fit its height to page
  • if the figure is very wide, then fit its width to page

How to do it?

Or in other words, is there way for LaTeX to get (or compare) the width and length of a figure?

share|improve this question

migrated from stackoverflow.com Oct 27 '11 at 13:27

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Marco Daniel, Aditya, diabonas, Jake, lockstep Oct 28 '11 at 5:59

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.

    
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  N.N. Oct 27 '11 at 13:32
2  

2 Answers 2

How about

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{myfig.png}

EDIT: added keepaspectratio

share|improve this answer
    
But it will change the ratio of the figure and it looks distorted. –  lukmac Oct 27 '11 at 13:17
    
@lukmac: if you want the picture with the height equal to text height, remove width=\textwidth keyvalue and viceversa. –  Azoun Oct 27 '11 at 13:48
16  
Add keepaspectratio in order not to distort the figure. –  Yiannis Lazarides Oct 27 '11 at 13:54
3  
@ArrayIndexOutOfBoundsException: Thanks for your answer. While this is relative common known, it is good to have it here for beginners to find. You should also mention that you need the graphicx package (not graphics) for this, even if it is standard package. –  Martin Scharrer Oct 27 '11 at 17:06
    
Great to know! Thanks to all. –  lukmac Jan 20 '12 at 23:06

Given that the figure is large, the adjustbox package can be used scale the image to a specified maximum width and height and keep the aspect ratio:

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

\newcommand*{\MyIncludeGraphics}[2][]{%
\begin{adjustbox}{max size={\textwidth}{\textheight}}
    \includegraphics[#1]{#2}%
\end{adjustbox}
}

\begin{document}
\MyIncludeGraphics{images/eiffel_tower}
\end{document}

As Martin pointed out, a simpler solution would be to use \usepackage[export]{adjustbox}:

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}

\begin{document}
\includegraphics[max size={\textwidth}{\textheight}]{images/eiffel_tower}
\end{document}
share|improve this answer
    
Simply use \usepackage[export]{adjustbox} and \includegraphics[max size={\textwidth}{\textheight}]{<image>}. Note that this will only scale it down if it is larger (as you assumed). –  Martin Scharrer Oct 27 '11 at 18:50
    
This is very useful also for cases where \includegraphcs is somehow involved, but not directly available for passing keyword arguments to it. This is the case with SVG files exported to PDF for LaTeX using Inkscape‌​. In that case only the image width is adjustable through \svgwidth, whereas the height is not. So using an encapsulating environment like adjustbox seems to be the only solution. –  johntex Jul 23 '13 at 3:38

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