[Tex/LaTex] Having trouble dividing lengths with calc package

calculationsscaling

What I want is something like \resizebox but which I can give a maximum height and width to, it will make it fit into those bounds, and not distort the image. (\resizebox, of course, always makes it fit the exact size you give it unless you use !. If you do use !, you have to settle on whether you want to set the height or width dimension.)

So I'm trying to write one, but I'm having problems with the \ratio command from the calc package. Whenever I use \ratio, I get "Missing number, please try again."

What am I doing wrong? (If you have any better suggestions for my original problem, I'm open to those too, of course.)

\documentclass{article}

\usepackage{settobox}
\usepackage{etoolbox}
\usepackage{calc}

\newsavebox{\theCurrentImage}
\newlength{\theCurrentImageHeight}
\newlength{\theCurrentImageWidth}
\newlength{\heightIfWidthExpanded}
\newlength{\widthIfHeightExpanded}

\newrobustcmd{\image}[1]{%
  \sbox{\theCurrentImage}{#1}%
  \settoboxheight{\theCurrentImageHeight}{\theCurrentImage}%
  \settoboxwidth{\theCurrentImageWidth}{\theCurrentImage}%
  % If the aspect ratio of the image is wider than the aspect ratio of the
  % screen, we are limited by the text width. Otherwise, we are limited by
  % the text height.
  Image width: \the\theCurrentImageWidth  \\
  Image height: \the\theCurrentImageHeight  \\
  Text width: \the\textwidth  \\
  Text height: \the\textheight  \\
  \ifnumcomp{\ratio{1pt}{1pt}} %\ratio{\theCurrentImageWidth}{\theCurrentImageHeight}
            {>}{\ratio{1pt}{1pt}} %\ratio{\textwidth}{\textheight}
            {% Width limited
              Width limited \\
              \resizebox{\textwidth}{!}{\usebox{\theCurrentImage}}%
            }%
            {% Height limited
              Height limited \\
              \resizebox{!}{\textheight}{\usebox{\theCurrentImage}}%
            }%
}

\begin{document}

\image{Hello world!}

\end{document}

Best Answer

Ian's answer pointed me into the right direction: This is easily possible with the adjustbox package by Martin Scharrer. It basically gives you an \includegraphics-like interface to be used with any (boxable) material:

\documentclass{article}
\usepackage{adjustbox}

\begin{document}

  \adjustbox{width=400pt,height=800pt,keepaspectratio=true}{Hello, World}

\end{document}