[Tex/LaTex] \resizebox and page sizes

scalingtables

What is the exact operation and syntax for resizing an object?

I am writing a macro package for printing on multiple paper sizes, including A4 2-column, eBook-size, portrait and 9inx6in WEB browsable (landscape).

I want to display the floating objects in reduced size, when necessary to fit on one page. I am now using \resizebox. In most cases it works OK, but when I have portrait-shaped objects, and landscape-shaped paper, it sets the width of the object to \textwidth and crops the bottom of the object. (I mean then using arguments {!}{!}) I want to reduce the width and show the object in full height.
Is there some special parametrization, or I shall stay at checking paper format and using something like \resizebox{!}{\.65\paperheight}?

Best Answer

It should also be noted that \resizebox has roundoff issues, and often slightly exceeds its limits.

\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe}

\newlength{\testlen}

\newcommand{\maxsizebox}[3]{% #1 = max h-space, #2 = max v-space, #3 = contents
  \settowidth{\testlen}{\resizebox{!}{#2}{#3}}%
  \ifdim\testlen>#1\relax\resizebox{#1}{!}{#3}%
  \else\resizebox{!}{#2}{#3}\fi
}

\newsavebox{\tempbox}

\begin{document}
\savebox{\tempbox}{\includegraphics{example-image}}
\begin{center}
% fit to reduced width
\rule{0.5\textwidth}{1pt}\par
\maxsizebox{0.5\textwidth}{\textheight}{\usebox{\tempbox}}

%fit to reduced height
\maxsizebox{\textwidth}{0.2\textheight}{\usebox{\tempbox}}\rlap{ \rule{1pt}{0.2\textheight}}
\end{center}
\end{document}

test