Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Let's see the following screenshot taken from an answer of Efficient ways to anonymize a document and the given comment.

enter image description here

Is it possible to remove length, counter, dim, etc, that have been declared in a macro, before leaving the macro?

share|improve this question
    
add comment

1 Answer 1

up vote 10 down vote accepted

No, it's not possible: \newlength allocates a register and this allocation is permanent. If you do the \newlength inside a group, you'll lose the register name at group end, but the register will not be reusable.

Actually it might be possible: define a macro \startlocalallocations that modifies \newlength and \newcounter in such a way that allocations from that point on are stored in a convenient macro, say \allocationsmade, and then define \removelocalallocations that uses \allocationsmade to restore the status as it was before the starting command.

While feasible (at least in principle), this is completely useless. Registers should be allocated outside macro definitions: they can be set and used as many times as one wants. So the correct way to define \blind is

\newlength{\sowide}
\newlength{\sohigh}
\def\blind#1{%
  ...
  \settowidth{\sowide}{#1}%
  \settoheight{\sohigh}{H}%
  ...}

and all will work. There's no need to allocate a temporary variable.

Note: the package etex allows for local allocations. But this is useful in contexts very different from this one: one can think to the allocation of a block of registers for some particular use, when they are not called by name, but by numbers. See the implementation of the float mechanism for a slightly different example in which a block of registers is allocated and they are called \bx@A, \bx@B, ..., \bx@R in order that the next free register can be used by knowing the number of the last one used.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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