# Use \textwidth for Image width Only when it outgrows the page [duplicate]

Possible Duplicate:
Shrink figure only when necessary?

Consider that you have this:

\includegraphics{some_image_in_pdf.pdf}


This will now take its original width, but if you want it to take up 100% of the text width you do it like this:

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


How about if you want to include images and use their original size if they fit, otherwise have it resized?

In this example, the second image should get [width=\textwidth] because it is going to exceed the bounds of the document.

\includegraphics{some_image_in_pdf.pdf}
\includegraphics{some_image_in_pdf_that_is_too_wide.pdf}

-

## marked as duplicate by Werner, Peter Grill, qubyte, Joseph Wright♦Jan 21 '12 at 9:19

The adjustbox package provides a max width=\textwidth key option for this. So, you can use

\usepackage[export]{adjustbox}% http://ctan.org/pkg/adjustbox
...
\includegraphics[max width=\textwidth]{some_image_in_pdf.pdf}
\includegraphics[max width=\textwidth]{some_image_in_pdf_that_is_too_wide.pdf}

-
Does not work in twocolumn mode of document class book. – gentlesea Nov 22 '15 at 7:23
@gentlesea: For twocolumn mode you're probably interested in max width=\columnwidth. – Werner Nov 22 '15 at 12:24

works also for images which are wrongly set to a width greater than the text width:

\documentclass{minimal}
\usepackage{graphicx}
\newsavebox\IBox
\let\Includegrfx\includegraphics
\renewcommand\includegraphics[2][]{%
\sbox\IBox{\Includegrfx[#1]{#2}}%
\ifdim\wd\IBox>\textwidth\resizebox{\textwidth}{!}{\usebox\IBox}\else
\usebox\IBox\fi}
\parindent=0pt % for demo
\begin{document}
\rule{\textwidth}{1pt}

\includegraphics[width=3\textwidth]{tiger}

\end{document}

-