# How to fit text in a box of given size?

Say I want to typeset small piece of text as a logo/motto/slogan or whatever. The text is not much longer than "Lorem Ipsum Dolor". I want this to fit tightly in a box of certain size (e.g. 7x2cm). Two questions here, possibly related:

How can I typeset it so that the text fits a box with certain width?
Assume fixed typeface size, and only the inter-word and inter-glyph
distance is allowed to change.


A (possibly) more advanced version of this:

How can I fit the same text in a box with a given width AND height?
Possibly also adjusting spacing and typeface size?


To illustrate:

Original Version:

Modified spacing, Q1.

Modified spacing plus scaling, Q2.

Sizes, font type, etc. are assumed to be arbitrary. I'm curious how can I do this conceptually. Bonus points for a parameterized macro to experiment with.

Also: I know there are many ways to do it without using TeX and friends (e.g. images, vector graphics, and what not), but I am only interested in ways how to typeset it with TeX, or popular macro packages.

To duplicate the answer I gave on the LaTeX Community site...

You could try \resizebox from the graphicx package. See its documentation, page 7:

\resizebox{1in}{!}{Some text}


This will give "Some text" which is exactly 1 inch long. The ! for the second argument means to vary the height by whatever it needs to in order to maintain the aspect ratio. If you did {!}{1in} it would be 1 inch tall instead. If you specify both parameters, it'll be stretched disproportionally so that it is exactly a rectangle of that size, and so on.

• Sure, that does the trick, but still no cigar -- playing with the spacing is still an issue. – Martin Tapankov Oct 20 '10 at 21:10
• Couldn't you combine it with the soul package to do whatever you wanted with the spacing? – frabjous Oct 21 '10 at 20:29
• Hmmm, I never thought of using soul to do this before.. I'll try these together and will post an update. Thanks for the hint! – Martin Tapankov Oct 22 '10 at 6:08

That's a lot of questions at once. I'll answer some of the easy ones, leaving the trickier aspects to others.

To typeset text in a box of a given width, use either

\makebox[width][s]{lorem ipsum dolor} % latex, or
\hbox to width{lorem ipsum dolor} % plain tex


where width is to be replaced by the desired width, of course. It will only stretch interword spaces, though, and not interglyph spacing. The latter is impossible in standard TeX without some macro trickery (which might involve taking the text apart and sticking stretchable spaces between the glyphs before putting it back together).

The answer from frabjous (just in) is good if you run a TeX engine such as pdftex where such resizing is supported by the graphics package. Otherwise, a possibility would be to measure the text when set in a standard box, then computing the appropriate font size for the box you want to fit it in, then typeset it using the font at that size. But again, standard TeX won't let you scale the font differently in the horizontal and vertical directions. The computation requires dividing one length by another: The only way I know of to do this requires a bit of plain TeX: Assign both lengths to count registers (which will receive the length in scaled points (sp)) and divide one by the other. But this produces an integer, so you need to rescale if you wish more accuracy. It gets involved rather quickly, especially since the rescaling can result in an overflow if overdone.Here is a quick hack, using some of latex's built-in temporary registers:

\documentclass{article}
\makeatletter
\newcommand{\scalehelper}[4][64]{%
\@tempdima=#3\relax
\multiply\@tempdima by #1
\@tempcnta=\@tempdima
\@tempdimb=#4\relax
\@tempcntb=\@tempdimb
\divide\@tempcnta by \@tempcntb
\multiply#2 by \@tempcnta
\divide#2 by #1
}
\makeatother
\begin{document}
\newlength{\foo}
\setlength{\foo}{12pt}
\scalehelper[128]{\foo}{80pt}{30pt}
\showthe\foo
\end{document}


Ideally, the new value of \foo should be 32pt; in reality, it is 31.96875pt. If you crank the optional argument of \scalehelper up, you increase the accuracy. Scale it up too much, and you get arithmetic overflow. But an accuracy on the order of a percent should be good enough for selecting a suitable fontsize. (You get the rest of the way by typesetting in a box of specified width, letting the interword spaces compensate for any inaccuracy.)

This is just an idea. Some assembly required. Batteries not included.

Couple of ConTeXt solutions:

• If you are willing to break lines manually, you can just say

\hbox to 7cm {\stretched{Some Text}}


and play around with the settings

\def\stretchedspacefactor{4}
\def\stretchedspaceamount{.25em}
\def\stretchedbreaktokens{.@/}


to change the amount of intercharacter and interword separation.

• If you want line breaks, and are only interested in rectangular boxes, Wolfgang Schuster posted a solution on the ConTeXt mailing list that loops over the value of the font size until the height equals to the desired height

• If you also want line breaks to occur at any location (and not just interword boundaries), ConTeXt wiki has a solution.

• And if you want non-rectangular shapes with line breaks, Hans Hagen posted a solution as a This Way magazine.

This is, perhaps, not directly relevant, but you could try to adapt my absurd solution to a somewhat similar problem. It just involves font size though (and centering, but you could remove that).