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.

When using images bigger than the text-area, I want to scale them but considering at the same time width and height. The problem happens to appear when both width and height are off limits. Here, I have to determine which one is the bigger, and then scale it, so the process will affect the other one too, and the image will be perfectly fitted in the text-area.

I've tried several ways to solve this. For example: (I'm using the calc package)

\makeatletter
\def\ScaleThisP{%
    \ifdim\Gin@nat@width>\Gin@nat@height
        \ifdim\Gin@nat@width>\textwidth
            \textwidth
        \else
            \Gin@nat@width
        \fi
    \else
        \ifdim\Gin@nat@height>\textheight
            \Gin@nat@width/{\Gin@nat@height/{\textheight}}
        \else
            \Gin@nat@height
        \fi
    \fi
}
\makeatother

...

\includegraphics[width=\ScaleThisP]{myfig.jpg}

The result (always considering width and height bigger than text-area) are:

  • Case 1 : width > height | Works properly!

  • Case 2 : height > width | It doesn't show anything. No images included at all!

If you notice, I'm using width to scale because, if I use the scale attribute in \includegraphics, it gives an error.

I've tried many other ways, but no one gives me the result I want. I think the problem is the line where I perform the calculation:

\Gin@nat@width/{\Gin@nat@height/{\textheight}}

If I replace that with, for example,

.5\textwidth

it shows reduced images maintaining the aspect ratio (so, I know it works... somewhat). Can anyone help me resolve this problem. I don't know what else to do, maybe use the xifthen package?

share|improve this question
add comment

1 Answer

Ignoring the question you are asking, there is a simple solution to your actual problem: \includegraphics has an optional argument keepaspectratio, that (quoting the manual),

if set to true then specifying both width and height (or totalheight) does not distort the figure but scales such that neither of the specified dimensions is exceeded.

Thus, you can simply write

\includegraphics[height=5cm,width=.5\textwidth,keepaspectratio]{some_image.jpg}
share|improve this answer
    
Ah!!!, You're right! Thank you so much! It was so simple... –  user4876 Apr 13 '11 at 20:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.