Using the suggestion in Fitting and centering text (both!) in a constrained area, together with Martin's answer that uses the environ
package, the following provides the environment
\begin{fitbox}{<width>}{<height>}
<stuff>
\end{fitbox}
which typesets <stuff>
using a form of binary search to fit the text within the given height <height>
while under a fixed width <width>
constraint set by a minipage
. This is required in order to maintain a proportionate scaling of the font and leading (\baselineskip
).
\documentclass{article}
\usepackage{lmodern}
\usepackage{environ}% http://ctan.org/pkg/environ
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newdimen\fontdim
\newdimen\upperfontdim
\newdimen\lowerfontdim
\newif\ifmoreiterations
\fontdim12pt
\makeatletter
\NewEnviron{fitbox}[2]{% \begin{fitbox}{<width>}{<height>} stuff \end{fitbox}
\def\buildbox{%
\setbox0\vbox{\hbox{\minipage{#1}%
\fontsize{\fontdim}{1.2\fontdim}%
\selectfont%
\stuff%
\endminipage}}%
\dimen@\ht0
\advance\dimen@\dp0
}
\def\stuff{\BODY}% Store environment body
\buildbox
% Compute upper and lower bounds
\ifdim\dimen@>#2
\loop
\fontdim.5\fontdim % Reduce font size by half
\buildbox
\ifdim\dimen@>#2 \repeat
\lowerfontdim\fontdim
\upperfontdim2\fontdim
\fontdim1.5\fontdim
\else
\loop
\fontdim2\fontdim % Double font size
\buildbox
\ifdim\dimen@<#2 \repeat
\upperfontdim\fontdim
\lowerfontdim.5\fontdim
\fontdim.75\fontdim
\fi
% Now try to find the optimum size
\loop
%\message{Bounds: \the\lowerfontdim\space
% \the\fontdim\space \the\upperfontdim^^J}
\buildbox
\ifdim\dimen@>#2
\moreiterationstrue
\upperfontdim\fontdim
\advance\fontdim\lowerfontdim
\fontdim.5\fontdim
\else
\advance\dimen@-#2
\ifdim\dimen@<10pt
\lowerfontdim\fontdim
\advance\fontdim\upperfontdim
\fontdim.5\fontdim
\dimen@\upperfontdim
\advance\dimen@-\lowerfontdim
\ifdim\dimen@<.2pt
\moreiterationsfalse
\else
\moreiterationstrue
\fi
\else
\moreiterationsfalse
\fi
\fi
\ifmoreiterations \repeat
\box0% Typeset content
}
\makeatother
\begin{document}
\lipsum[1]
\begin{fitbox}{.5\textwidth}{0.5\textheight}
\lipsum[1-2]
\end{fitbox}
\lipsum[2]
\clearpage
\lipsum[1]
\begin{fitbox}{300pt}{300pt}
\lipsum[1-2]
\end{fitbox}
\lipsum[2]
\end{document}
In the figure below, two pages are typeset, each starting with \lipsum[1]
and ending with \lipsum[2]
to provide some frame of reference. The left page has a fitbox
of dimension .5\textwidth x .5\textwidth
while the page on the right is set at 300pt x 300pt
(square).

Interestingly enough, I'm having trouble compiling this under TeXLive 2011. Although, there is no problem compiling it using the online LaTeX compiler ScribTeX, which runs TeXLive 2009. I don't know what the cause behind this is... This has been fixed by the replacement of \protected@edef\stuff{\BODY}
with \def\stuff{\BODY}
. The original code used this form since it provided two macros - one for parsing the content (called \fillthepage{<stuff>}
) and another for updating a resized version of the content (called \buildbox
). I assume the coding structure required this. However, with everything contained in a single environ
ment fitbox
above, this is not needed anymore.
A popular mistake is to say something like
{\fontsize{30}{32}\selectfont A long title, spanning two lines}
And now the text.
Due to the fact that the line spacing in (La)TeX is uniform across the paragraph, and that the settings at the end of the paragraph count, you should end the paragraph before resetting the font, either by an empty line, or by a \par
, e.g.:
{\fontsize{30}{32}\selectfont A long title, spanning two lines\par}
And now the text.
Edit: as Marc van Dongen noticed in the comment, leading of 32 is also probably too small - you may try to increase it. IMHO, a good rule of thumb is leading = 1.2*font size
- 36pt in your case.
Best Answer
The command
\@setfontsize
has a@
in its name, which means it's a "low level" or, perhaps better, internal command. It shouldn't appear in a normal document apart perhaps in the preamble to make some particular definition.The definition of
\fontsize
iswhich means that the user level command
\fontsize
hands control to\set@fontsize
. This is a common technique in LaTeX.The internal command
\@setfontsize
has a different purpose from\fontsize
:It's typically found in "class option files" such as
size10.clo
, for instanceLet's see what happens when
\normalsize
is called (most of the times implicitly); LaTeX doesthat, in view of the definition above, becomes
The first line
\normalsize
tells LaTeX to disallow\normalsize
in math mode; the second line sets\@currsize
to be equivalent to\normalsize
, but only during normal typesetting (it does nothing, for instance, in write operations). Finally the command\fontsize
is called with font size 10pt and baselineskip 12pt, then the font is actually selected.Thus you see that
\@setfontsize
is an internal utility and calling it in a document doesn't make sense.What's the purpose of
size10.clo
? Classes such asarticle
andreport
are usually written in a "size independent" way; a size option (that may be the default one) causes LaTeX to readsize10.clo
,size11.clo
orsize12.clo
(it'sbk10.clo
,bk11.clo
orbk12.clo
for thebook
class). These files set up the font selections and the parameters for lists and many other things that depend on the main body font size.