While commands are defined and set in one step with \newcommand, lengths need two steps for this: \newlength and then \setlength. Because


would be easier than


I wonder if there is a special reason for \newlength to not have been profiled as \newcommand.

  • 1
    In general you want to give commands always a definition, while it often happens that you only reserve lengths for later uses. expl3 offers \tl_new:N and \tl_set:Nn for commands. And there is \dim_const:Nn to set a dimension directly (the "const" in the name makes it a bit awkward to use for dimension you want to change later). – Ulrike Fischer Nov 14 '14 at 8:23
  • 1
    @UlrikeFischer If you do \tl_const:Nn, then the variable should not change its value (across a LaTeX run); similarly for \dim_const:Nn and the other similar functions. Exemplary is \int_const:Nn \c_x_int: you can't even say \int_incr:N \c_x_int. The fact that changing the value of a constant dimension is possible with, say, \dim_add:Nn is just implementation dependent and shouldn't be relied on. – egreg Nov 14 '14 at 9:42
  • 1
    In my experience, the cases where one allocates a length register and wants to set the value immediately are a small minority. – egreg Nov 14 '14 at 9:45
  • 1
    @egreg: Yes I wouldn't \dim_const:Nn etc for register I want to change a lot (a \dim_new:Nn would be better to declare and set in one go). Would you use a constant for a value that is changed once? First a default is set and at the begin of the document it is perhaps changed? (An example is the font size of a document). – Ulrike Fischer Nov 14 '14 at 10:04
  • 1
    @UlrikeFischer Once set, a constant should not change. – egreg Nov 14 '14 at 10:10

macro names do not need to be allocated, you can have (up to implementation defined limits) arbitrarily many csnames with macro definitions. But classic TeX only has 256 registers of each type (extended to 32768 in etex) so length (skip) registers need to be allocated from a fixed pool with separate commands for allocating a register (which just reserves the next free slot) which you only do once for each register and setting its value which you presumably do often.

  • 1
    I think this doesn't answer the “Why not \newlength\foo{2cm}”; one could make a macro that automatically does that, why hasn't it been done? – Manuel Nov 14 '14 at 11:20
  • 3
    @Manuel one of the biggest errors with registers that people do is that they allocate a new register each time. A macro that allocates and sets would just make that more likely and not have any real benefits – David Carlisle Nov 14 '14 at 11:40
  • @DavidCarlisle Making use of \newsetlength defined by \newcommand{\newsetlength}[2]{\newlength{#1}\setlength{#1}{#2}} wouldn't be worse than making use of usual \newlength, isn't it? – Denis Bitouzé Nov 14 '14 at 13:15
  • 1
    @DenisBitouzé It would be used instead of \setlength and that would be much worse. (If it is just used once per length to allocate and set then obviously it is not bad) – David Carlisle Nov 14 '14 at 13:16

\newlength is defined as a \skip, i.e. register provided also to calculations (\newlength reserves a new skip, \setlength sets its value), while \newcommand in this case would be only a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.