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?
Best Answer
Ignoring the question you are asking, there is a simple solution to your actual problem:
\includegraphics
has an optional argumentkeepaspectratio
, that (quoting the manual),Thus, you can simply write