[texhax] grouping

Robin Fairbairns Robin.Fairbairns at cl.cam.ac.uk
Mon Mar 28 11:05:55 CEST 2005


> On 28/03/2005, at 12:27 PM, moheb missaghi wrote:
> 
> > I am reading tabularx.sty as a means to learn how to write
> > packages.

on the lines of diving off a liner in mid-ocean to learn to swim?

> > Not that it really matters for what I want to do but as a matter of
> > curiosity I don't understand the grouping opened by:
> > 
> >  {\ifnum0=`}\fi
> > 
> > in tabularx (tabularx.sty) and=A0apparently closes by:
> > 
> > \ifnum0=`{\fi}%
> > 
> > in TX at endtabularx. How can a grouping go across 2 different macros and=20
> > what does it mean?
> 
> A grouping can certainly go across different macros.
> 
> Most LaTeX environments start with something that puts a
>    \begingroup
> and finishes with something else that puts the corresponding
>    \endgroup
> 
> You can also use  \bgroup  and  \egroup  to do the same
> kind of thing.
> 
> It's very common to use these, as when the grouping closes,
> then the expansion values of any temporary macros revert
> to what they were before entering the grouping.
> This is important for scoping the values of variables and
> pointers, and for memory-management.
> 
> This looks to me like a fancy trick to include a {
> >  {\ifnum0=`}\fi
> 
> while keeping {..} balanced for an editor, while
> also keeping the \ifnum ... balanced with a \fi .

it's only keeping it balanced for an editor as an incidental matter;
the real point is to keep tex's lexical scanner happy.

> That is, the group opens, but \ifnum returns \iffalse
> so the grouping does not close right away.
> 
> Rather, when later we encounter the corresponding
> > \ifnum0=`{\fi}%
> 
> then the closing bracket is found.
> 
> This could probably have been done much more easily using
> first a  \bgroup ,  then close it later with \egroup .
> 
> However, this package was generated using  docstrip .
> I've seen this style used there quite a lot.

i don't believe it's used anywhere other than in alignments.

i've used it as a mantra when writing stuff in that context; i don't
know whether it's necessary in the slightly odd situation one finds
oneself in at the start of an alignment, but it's most certainly used
in most (or possibly all) latex macros that might be placed there.

> Perhaps it meshes better with the need to use the
> same source document for multiple purposes.
> So there could be a good reason for this apparently
> cryptic way to start and finish groupings.

the same usage appeared in latex 2.09, so i don't think it's to do
with docstrip.



More information about the texhax mailing list