# Automatically reduce size of text to fit inside a frame

I need a code (and so, created that:P) for reducing the font size in order to fit it inside a frame (or a minipage of a frame) and not let it overflow from there to the footer's area.

lets say we have a simple beamer-frame with two minipages like below that we don't want to let the text overflow from there into the footer:

\documentclass[20pt]{beamer}
\usetheme{Warsaw}
\begin{document}
\section{test section}
\begin{frame}
\begin{minipage}{0.45\textwidth}
%\begin{autotext}%Need to define this to do nothing if text is not too tall
This is a random text that will be added in this first column of the presentation under a bold title as seen here. The text will automatically reduce the font size to fit the page's height if it is too large\ldots
%\end{autotext}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
%\begin{autotext}%Need to define this to reduce the font size as far as needed in order to not let the text overflow
This is a random bigger text that in the previous column that will be added in this second column of the presentation under a bold title as seen here. The text this time will really reduce the font size to fit the page's height because it is  too large\ldots To make this text to large I have to add some more text like doing here and so, you don't really have to read this random text from this place and after\ldots It is just random text to make the text really tall. But this is difficult as I just realized and I have to add more and more text in order to see this effect here and to let the size be reduced into \texttt{\textbackslash small} size. This is the final words I had to add because befor this lines I can't see this resizing.
%\end{autotext}
\end{minipage}
\end{frame}
\end{document}


How can I make this autamated environment?

One option is to put the content in a fitted box using the fitting library and the tcolorbox package (see chapter 21 of the tcolorbox manual). Several fitting algorithms are available to fit text to the nominated height and width of a box.

\documentclass[20pt]{beamer}
\usetheme{Warsaw}
\usepackage[fitting]{tcolorbox}
\newcommand*{\mytext}{This is a random text that will be added in this first
column of the presentation under a bold title as seen here.
The text will automatically reduce the font size to fit the
page's height if it is too large.}
\newtcboxfit{\mybox}{height=6.5cm,boxsep=0mm,top=2mm,bottom=2mm,left=2mm,right=2mm,
nobeforeafter,width=\linewidth}
\begin{document}
\section{test section}
\begin{frame}[t]
\frametitle{Outline}
\begin{columns}[totalwidth=\textwidth]
\begin{column}[t]{0.48\textwidth}
\mybox{\mytext\ \ldots}
\end{column}
%
\begin{column}{.04\textwidth}
\end{column}
%
\begin{column}[t]{0.48\textwidth}
\mybox{\mytext\ \ldots \mytext\ \ldots \mytext\ \ldots \mytext\ \ldots}
\end{column}
\end{columns}
\end{frame}
\end{document}


• Thanks for this alternative @Ross... Didn't really knew about that. Do you know if this way uses "acceptable" sizes? (I mean \small, \footnotesize etc)? – koleygr Oct 6 at 3:06
• Hi @koleygr. You could try playing with the environment keys, \tcbfitdim and fit fontsize macros, to adapt the fitted text to text sizes proportional to text sizes set with the standard macros \Large etc. I've not tried this myself but there are examples in the manual. – Ross Oct 6 at 3:16
• Thanks @Ross ... Was just curious... – koleygr Oct 6 at 3:29
• There is also the shrink key available in Beamer. From the manual (p63): \begin{frame}[shrink=5] Some evil endless slide that is 5\% too large. \end{frame}. In this case you have to specify the amount of shrinkage you want on a particular frame. – Ross Oct 6 at 3:40

By using environ package and pgffor (just to simplify the \loop):

\documentclass[12pt]{beamer}
\usetheme{Warsaw}
\usepackage{environ}
\usepackage{pgffor}

\newcounter{boxCounter}
\newsavebox{\boxA}
\newsavebox{\boxB}
\newsavebox{\boxC}
\newsavebox{\boxD}

\newlength{\availafter}

\NewEnviron{autotext}[1][0.5cm]
{\setcounter{boxCounter}{0}\foreach \mysize in {\normalsize,\small,\footnotesize,\scriptsize}{\stepcounter{boxCounter}\expandafter\savebox\csname box\Alph{boxCounter}\endcsname{\vbox{\mysize\BODY}}\setlength{\availafter}{\dimexpr\textheight-\expandafter\ht\csname box\Alph{boxCounter}\endcsname-\pagetotal\relax}\ifdim\availafter>#1\expandafter\usebox\csname box\Alph{boxCounter}\endcsname\breakforeach\fi}}

\begin{document}
\section{test section}
\begin{frame}
\begin{minipage}{0.45\textwidth}
\begin{autotext}%Need to define this to do nothing if text is not too tall
This is a random text that will be added in this first column of the presentation under a bold title as seen here. The text will automatically reduce the font size to fit the page's height if it is too large\ldots
\end{autotext}
\end{minipage}\hfill
\begin{minipage}{0.45\textwidth}
\begin{autotext} %Need to define this to reduce the font size as far as needed in order to not let the text overflow
This is a random bigger text that in the previous column that will be added in this second column of the presentation under a bold title as seen here. The text this time will really reduce the font size to fit the page's height because it is  too large\ldots To make this text to large I have to add some more text like doing here and so, you don't really have to read this random text from this place and after\ldots It is just random text to make the text really tall. But this is difficult as I just realoized and I have to add more and more text in order to see this effect here and to let the size be reduced into \texttt{\textbackslash small} size. Thi is the final words I had to add because befor this lines I can't see this resizing.
\end{autotext}
\end{minipage}
\end{frame}
\end{document}


The result is:

PS: See also answer of @Ross with tcolorbox and not only.