laughlin@fornax.UUCP (Bob Laughlin) (02/28/91)
After years of using LaTeX pretty much as is, I'm starting to write my own TeX macros. I'd like to know the best way to allocate and use dimen and count registers in a macro. I know I can reserve these with \newdimen\mydimen etc. or use certain ones as scratch registers, e.g. \dimen0 - \dimen9. The problem is \newdimen is outer and can't be used in a \def and so if I'm not mistaken \mydimen has to be global to the whole macro set. In a large macro package you could have an unwieldy set of global dimen names. In addition, since \dimen0 etc. are scratch registers if you call other macros in your macro there is no guarantee that they will not also use these registers and presumably destroy the contents. At least thats what I think the TeXbook says. Is there a mechanism for generating the equivalent of automatic local variable in C for TeX macros? Something whose name is hidden in the macro and disappears after the macro is executed? -- Bob Laughlin laughlin@cs.sfu.ca
eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (03/01/91)
laughlin@fornax.UUCP (Bob Laughlin) writes: > After years of using LaTeX pretty much as is, I'm starting to >write my own TeX macros. I'd like to know the best way to >allocate and use dimen and count registers in a macro. > I know I can reserve these with \newdimen\mydimen etc. or use >certain ones as scratch registers, e.g. \dimen0 - \dimen9. >The problem is \newdimen is outer and can't be used in >a \def Right, but you said you were using laTeX? There the macros are first defined in lplain.tex, and subsequently redefined without the \outer prefix in latex.tex. Talk about waste... Anyhow, if you are using LaTeX they have already been stripped of the outer prefix, and if not, there's nothing to prevent you from redefining those macros. >In a large macro package you could have an unwieldy >set of global dimen names. Right. Just keep your wits together and don't define the same name twice. Or build in a check in the \new... macro to double check. > In addition, since \dimen0 etc. are scratch registers if you call >other macros in your macro there is no guarantee that they will not >also use these registers and presumably destroy the contents. >At least thats what I think the TeXbook says. Right again. So you have to allocate your own temprorary registers of which you know that only your own macros will touch them. \newcount\mycounta \newcount\mycountb \nwcount ... > Is there a mechanism for generating the equivalent of automatic >local variable in C for TeX macros? Something whose name is hidden >in the macro and disappears after the macro is executed? If the macro opens a group any declaration (with some small exceptions) is local. Trouble with allocting \new... registers is that those macros do a global advance of the counter that keeps track of the first allocatable (or last allocated, look it up) register number. You might write a variant \localnew... which does not do the global advance, and you could use this in a group. Victor.