[Tex/LaTex] What does the ‘etex’ package do, exactly

e-texpackagesregisterstex-core

I was creating a rather large LaTeX project, so I had to use many packages. This gave me an error

No room for a new \dimen \newdimen \MPscratchDim

while my editor(Kile) opened the file supp-pdf.mkii, pointing at the line

\newdimen\MPscratchDim % will be assigned global

Searching through the net, I found that this could happen due to loading too many packages, as answered in No room for a new \dimen by egreg.

Indeed I loaded, etex and everything works fine now. I searched etex on CTAN to understand what does this particular package do, but I am not able to understand what is going on. As stated by CTAN

The pack­age pro­vides sup­port for LATEX doc­u­ments to use many of
the ex­ten­sions of­fered by e-TEX; in par­tic­u­lar, it mod­i­fies
LATEX’s reg­is­ter al­lo­ca­tion macros to make use of the ex­tended
reg­is­ter range

I believe that the bold words give the answer, but I cannot understand why there is a limited register allocation (I don't even know what that is) and how can a package extend this register range.

Any idea on that will be very educational!

Best Answer

Update

Recent LaTeX kernels have incorporated the greater part of etex, so it's quite unlikely that one needs to load it nowadays.


Original answer

TeX, as designed by Knuth, has various registers addressable with an eight bit number (that is, from 0 to 255). Registers are of type

  • \count
  • \dimen
  • \skip
  • \muskip
  • \toks
  • \insert
  • \box

Let's consider the \dimen registers, for the others the allocation mechanism is similar. Each register is addressable by its number, for instance

\dimen34=42pt
\kern\dimen34

are legal example of setting a \dimen register or retrieving its value. However, calling registers by number is a problem, because packages couldn't cooperate with each other. So all formats provide an allocation mechanism: one says

\newdimen\foo

and TeX sets up things so that \foo is the same as calling \dimen<n> where <n> represents a number that we don't need to know. Note that \newdimen is not a documented LaTeX command, but it is documented in source2e and is very useful when writing packages.

TeX/LaTeX keeps track of the most recent allocated number for each register type; since the maximum number is 255, when a \newdimen command hits the limit, the dreaded

No room for a new \dimen

error message appears. There is a slight complication because allocation of \insert registers starts from 254 down and each \newinsert also allocates the \count, \dimen, \skip and \box registers with the same number, so the upper limit is usually something less than 255, but it's not the main problem.

In case we hit the limit, there is nothing to do: we are forced to load less packages or do nasty tricks that will almost certainly bite us later on.

In order to solve this problem that became apparent several years ago when PicTeX was released, a different implementation of TeX was prepared that defines 32768 registers of each type. This is part of e-TeX, that is incorporated in pdftex, the engine used when we run LaTeX on a document.

However, the kernel of LaTeX has not been modified, because it's basically frozen apart from bug fixes. Changing the allocation mechanism might break documents or packages that have been written under the assumption that only 256 registers of each type are available.

The etex package modifies the allocation mechanism; when \newdimen would issue the No room for a new \dimen error message, it insteads jumps beyond 255 and allocates the register number 256 and goes forward from there.

Here is a simulation, where we assume that \newdimen\foo allocates the last available eight bit slot and \newdimen\baz needs to go up to fifteen bits; in the log file you'd find

\foo=\dimen233

Normal \dimen register pool exhausted, switching to extended pool.
\baz=\dimen256

provided you have \usepackage{etex} in your preamble.

Why 233 is the last? Because LaTeX allocates \insert classes from 254 to 234; they are connected with marginal notes, footnotes, figures and tables.

Related Question