# Other than syntax what is the difference between \dimen0= and \setlength{}{}?

I have been browsing around for a while trying to find an answer to the above question, and I have found some similar questions along with there answers, so I already have a vague idea how to answer my own question. As a result I am really just looking for confirmation.

First some similar questions:

My understanding at this point:

\dimen0=value is a tex primitive, which does roughly the same thing as the following combination of latex commands \newlength{\name}\setlength{\name}{value} where the numerical identifier 0 fulfils the purpose of the name chosen with \newlength.

So my first real question is whether that is more or less correct? I also have a follow up question: When if ever is it appropriate to use the more basic version of some command?

I have recently embarked upon my first LaTeX project that really goes beyond mere usage. In my past usage, the distinction between basic Tex and LaTex commands simply never came up. I am increasingly getting the feeling that I need to read an actual book to get any further than where I am now, so for a final follow up question, I would like to ask the more experienced amongst you out there, how I might go about going beyond a competent user understanding of TeX.

-

The primitive syntax for setting the value of a length register is

<register name> = <value>


where <register name> is a symbolic name or \dimen followed by a number (eight bit normally, fifteen bit when the package etex is loaded).

Internal registers only have a symbolic name (for instance, \hsize). Length registers allocated by LaTeX or the user have both, but it's better to stick to the symbolic name. When one says

\newdimen\foo


TeX performs some calculations and then issues an instruction such as

\dimendef\foo=42


(the number will be different), so that \foo and \dimen42 will refer to the same register. But the user doesn't need and shouldn't use this number, because it may change depending on the packages loaded at startup.

To the contrary, registers numbered from 0 to 9 are never allocated, so \dimen0 (and the others) can be used as a scratch register for temporary computations.

Why did I write \newdimen instead of \newlength? Because \newlength actually issues \newskip and not \newdimen. The length registers defined by \newlength are always rubber lengths.

This explains why using \setlength is very recommendable. Saying

\newlength{\foo}


and then

\foo=10pt
Plus is a Latin word


will trigger a weird error:

! Missing number, treated as zero.
i
<*> Plus i
s a Latin word


because an assignment to a rubber length register always looks for the keywords plus and minus. With

\setlength{\foo}{10pt}%
Plus is a Latin word


this won't happen, because it is translated into

\foo=10pt\relax


The LaTeX kernel also uses \dimen registers (allocated with \dimen) and for setting them one can use \setlength. Expert (or cargo cult) programmers may decide to use the primitive syntax for saving space and macro expansions, but this is not recommended to novices. Stick to the recommended ways, you've time to learn how to safely break the rules.

-

You don't have the LaTeX syntax correct: the LaTeX equivalent is \setlength not \newlength the latter does not take a value argument, it just declares the register.

In unextended LaTeX \setlength\mylength{5pt} expands almost immediately to \mylength=5pt\relax so there really is not much difference, however packages and classes should always use the \setlength interface so that when the calc package is loaded the user benefits from the infix expression parser loaded by that package.

so for example \parbox internally uses \setlength\hsize{#1} rather than just \hsize=#1\relax to set the width of the text in the box. Because of that, if a document has

\usepackage{calc}


then the user can go

\parbox{\textwidth - 2cm}{...}


to set a box 2cm less than textwidth. If the infix subtraction were used as

\hsize=\textwidth - 2cm\relax


then \hsize would be set to \textwidth and "- 2cm" would be typeset as text at the start of the box.

-
Thanks for spotting the \setlength \newlength mixup. What I really meant to compare was \dimen0=value and the combination \newlength{\name}\setlength{\name}{\value}. I have seen those two used for identical purposes. I guess my muddled brain simply turned two commands into one. Funny how brains work sometimes. –  Psachnodaimonia Jul 12 '12 at 15:19