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.

Is it possible to make \newcount, \newtoks, etc. respect grouping, so that, e.g., \newcount\temp\temp=2{\newcount\temp\temp=1}\showthe\temp gives 2 (instead of 0)?

Relatedly, is it possible to undefine a register so that the appropriate \new command (\newcount, \newtoks, etc.) will use it?

share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

In addition to TH.'s comments, I'd point out that the etex package for LaTeX does include functions such as \loctoks for doing a local definition. This has two parts:

  • Creating the new name only within the current group
  • Setting the tracking number for allocations locally

That's not quite the same as your request, but I think it would be quite possible to use it as the basis for what you want. You'd do something like:

  \let#1\undefined
  \loc<thing>#1

So that the global definition was temporarily removed, meaning that the allocator would then create a new local version pointing to a different register.

For the question of freeing up a register, undefining the name is easy enough. The problem is the tracking number. You could just decrease it by 1, but it's quite likely that other allocations will have taken place between setting up the register you are interested in and deleting it again. It would be possible to do this by extending the allocation system to include a list of freed registers, and checking this list before the main tracking number.

In the work on expl3, we did try out local allocation of registers, but decided in the end it did not really work so well in TeX. In most languages, a local variable is local to some function. So you can nest functions knowing that multiple 'new' variables with the same name can be created. That is not the case in TeX, where local means 'within the current group'. The need to watch groups, rather than functions, seemed overall to make it less useful to have locally-declared variables. Of course, using grouping to have local values is very much day to day TeX programming.

share|improve this answer
    
One possibility would be to use a METAFONT style approach to saving variables, but TeX's grouping does that (although there is a maximum group depth which can be annoying). –  TH. Dec 15 '10 at 9:30
    
I'm playing with writing macros to (partially) sandbox code (by saving values, executing the code, and then restoring them after). (This to give more granularity than local to a group and global.) To make nesting sandboxes work, I push the values onto a(n imaginary) stack, and pop them off at the end. An easy way to do this is just keep adding a few characters to the macro name, and have the bottom of the stack be when the macro is undefined. I don't see a way to reuse global registers nicely, but I think the \loc<thing> will help somewhat. Thanks! –  Jason Gross Dec 15 '10 at 12:04
add comment

There is no way to do that without reimplementing the various \newX. Actually, the majority of the allocation is done by a single macro \alloc@, so I suppose you'd just need to reimplement that.

That said, with the use of ε-TeX, a document has access to many more registers than the 256 per class that one gets with TeX. So it doesn't really hurt for each package to allocate several private registers for its own use.

Of course, even though the allocation is global, the use can be local, so with appropriate uses of \begingroup/\endgroup, one can generally use just a few registers to hold many values at the same time.

share|improve this answer
add comment

Eplain comes with \innernewcount, \innernewtoks etc.

share|improve this answer
    
This simply strips the \outer off of plain TeX's \newX macros. LaTeX's macros are not \outer and are thus equivalent to eplain's \innernewX. –  TH. Dec 15 '10 at 5:56
    
@TH.: Ah, I see. Thanks! –  morbusg Dec 15 '10 at 8:18
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.