1

I would like write a command to scale a given text-object to fit into a given area preserving the aspect ratio. That is, a maximum width and height should not be exceeded, but either width or height of the text should fit the given value. The following would not work because the maximum height could be exceeded:

\resizebox{\mywidth}{!}{#1}

Likewise, the following can exceed a maximum width:

\resizebox{!}{\myheight}{#1}
  • The adjustbox package does this. – egreg Oct 4 '15 at 18:10
0

You can do it with adjustbox, the trick is to scale up the contents so it needs to be scaled down.

\documentclass{article}
\usepackage{amsmath}
\usepackage{adjustbox}

\newlength{\myheight}
\newlength{\mywidth}
\setlength{\myheight}{3cm}
\setlength{\mywidth}{6cm}

% A box for displaying the result
\newsavebox{\FRAME}
\sbox\FRAME{%
  \setlength{\fboxsep}{0pt}%
  \kern-\fboxrule
  \fbox{\rule{\mywidth}{0pt}\rule{0pt}{\myheight}}%
  \kern-\mywidth\kern-\fboxrule
}

\begin{document}

\usebox{\FRAME}%
\adjustbox{scale=100,max width=\mywidth,max height=\myheight}{%
  \raisebox{\depth}{%
    $\displaystyle
     \int_{-\infty}^{\infty}e^{-x^{2}}\,dx=\sqrt{\pi}
    $%
  }%
}

\bigskip

\usebox{\FRAME}%
\adjustbox{scale=100,max width=\mywidth,max height=\myheight}{%
  \raisebox{\depth}{%
    $\displaystyle
     \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 0 & 0 \\ 0 & 0 \end{bmatrix}
    $%
  }%
}

\end{document}

I used \usebox{\FRAME} just for showing the intended area to fill. It's also necessary to raise the box by its depth, or it would stick below the area.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.