The textfit package for scaling up text to a desired size^{∗}

Sebastian Rahtz  Phil Taylor

1994/04/15

Contents

1 Introduction 1

1 Introduction

The user needs to be able to specify the width or height for text, and use the largest size necessary to fill that width or height. You can choose whether to have the size increased by simple points, or by using magsteps. Usage:

\scaletowidth{3in}{This is the way of the world}\scaletoheight{7mm}{This is the way of the world}

Details are controlled by two options: ‘magstep’ will make the system use magsteps, not points, and ‘noisy’ will produce useful messages.

Examples; note that the base font is taken from the current state when you enter the macros. so if you start off with a large font (eg cmr17), it will give a different result from that obtained by scaling up cmr5.

This is the way of the world

This is the way of the world

This is the way of the world

This is the way of the world

Cowabunga!

∗This file has version number 5, last revised 1994/04/15.

Gloucestershire Warwickshire Railway

This is the way of the world

This is the way of the world

This is the way of the world

This is the way of the world

Cowabunga!

1 ⟨∗package⟩

 2 \NeedsTeXFormat{LaTeX2e}
 3 \ProvidesPackage{textfit}[\filedate]
 4 \newif\ifScalebyMagsteps
 5 \newif\ifNoisyFitting
 6 \NoisyFittingfalse
 7 \ScalebyMagstepsfalse
 8 \DeclareOption{noisy}{\NoisyFittingtrue}
 9 \DeclareOption{magstep}{\ScalebyMagstepstrue}
10 \ProcessOptions
11 \newcount \mags@f@r
12 \newdimen \desired@size
13 \newcount \r@mainder
14 \newcount \m@gstepcount
15 \newcount \m@gsteplimit
16 \newcount \m@gstepvalue
17 \newdimen \m@gstepdimen

Code for working out the right magstep (this is Phil’s bit).

18 \def \magsteps {\afterassignment \m@gsteps \m@gsteplimit = }
19 \def \m@gsteps
20  {\let \@r = \or
21  \def \or {\noexpand \or}%
22  \m@gstepdimen = 0.166667 \maxdimen
23  \m@gstepvalue = 1000
24  \m@gstepcount = 0
25  \def \magstep {}%
26  \loop
27  \ifnum     \m@gstepcount < \m@gsteplimit
28  \advance \m@gstepcount by 1
29  \ifnum \m@gstepvalue > \m@gstepdimen
30  \advance \m@gstepcount by -1
31  \message {Sorry --- integer overflow would occur if

32  I went any further; stopped at
33  \the \m@gstepcount.}
34  \m@gsteplimit = \m@gstepcount
35  \else
36  \multiply \m@gstepvalue by 12
37  \multiply \r@mainder by 12
38  \divide \r@mainder by 10
39  \advance \m@gstepvalue by \r@mainder
40  \r@mainder = \m@gstepvalue
41  \divide \m@gstepvalue by 10
42  \multiply \m@gstepvalue by 10
43  \advance \r@mainder by -\m@gstepvalue
44  \divide \m@gstepvalue by 10
45  \ifnum \r@mainder < 5
46  \edef \magstep {\magstep \or \the \m@gstepvalue}%
47  \else
48  \advance \m@gstepvalue by 1
49  \edef \magstep {\magstep \or \the \m@gstepvalue}%
50  \advance \m@gstepvalue by -1
51  \fi
52  \fi
53  \repeat
54  \edef \magstep ##1%
55  {\noexpand \ifcase ##1
56  1000\magstep
57  \noexpand \else 0%
58  \noexpand \message
59  {Sorry --- \string \magstep \space ##1
60  is not in range 0 .. \the \m@gsteplimit}%
61  \noexpand \fi
62  \noexpand \relax
63  }%
64  \let \or = \@r

65 }

66 \magsteps 63
67 \def \Fontname #1{\expandafter \strip@size \fontname #1 }
68 \def \strip@size #1 #2#{#1\void@@@}
69 \def \void@@@ #1{}
70 \gdef\n@d#1.#2:{#1}
71 \def \@scaletofit[#1]#2#3{%
72  \def\Hb@x{h}\def\H@rV{#1}%
73  \ifx\H@rV\Hb@x\def\C@mpare{\wd}\else\def\C@mpare{\ht}\fi
74  \desired@size #2
75  \def\curr@fontname{\Fontname \font {} }%
76  \ifScalebyMagsteps
77  \mags@f@r \z@
78  \setbox 0 = \hbox{#3}%
79  \def\@increment{1}%
80 \def\@test{<}%
81  \ifdim \C@mpare 0 > \desired@size
82  \message{When scaling by Magsteps, you cannot go downwards!}%
83  \fi
84  \loop
85  \font \temp@font = \curr@fontname  scaled \magstep \mags@f@r

 86 \setbox 0 = \hbox{{\temp@font #3}}%
 87  \ifNoisyFitting
 88  \message {Trying \noexpand \magstep \the \mags@f@r...}%
 89  \fi
 90  \ifdim \C@mpare 0 \@test \desired@size
 91  \advance \mags@f@r by \@increment
 92  \repeat
 93  \ifnum \mags@f@r=\z@
 94  \mags@f@r1
 95  \fi
 96  \advance \mags@f@r by -\@increment
 97  \else
 98  \font\temp@font=\curr@fontname at1pt
 99  \setbox 0 = \hbox{{\temp@font #3}}%
100  \@tempdima\C@mpare0
101  \multiply\@tempdima by 500 %\@m
102  \def\foo@@{\expandafter\n@d\the\@tempdima:}%
103  \@tempdimb\desired@size
104  \divide\@tempdimb by \foo@@
105  \multiply\@tempdimb by 500 %\@m
106  \fi
107  \ifScalebyMagsteps
108  \ifNoisyFitting
109 \message {The calculated font is \curr@fontname
110  scaled \noexpand \magstep \the \mags@f@r}%
111  \fi
112  \font \temp@font = \curr@fontname scaled \magstep \mags@f@r
113  \else
114  \ifNoisyFitting
115  \message {The calculated font is \curr@fontname ->\the\@tempdimb}%
116  \fi
117  \font\temp@font=\curr@fontname at \the\@tempdimb
118  \fi
119  \ifx\H@rV\Hb@x
120  \hbox to \desired@size {\hss{\temp@font #3}\hss}%
121  \else
122  \hbox {{\temp@font #3}}%
123  \fi

124 }

125 % user interface
126 \def\scaletowidth{\@scaletofit[h]}
127 \def\scaletoheight{\@scaletofit[v]}

128 ⟨/package⟩