[Tex/LaTex] When to use or avoid grouping

groupingprogrammingtex-core

For writing my own TeX code, grouping (\begingroup, \endgroup) helps on automatically saving and restoring TeX "variables".

(La)TeX "variables" are either macros or registers. The registers can
be counters (integers), dimensions, skips (both fractional numbers
with units) or read/write handlers etc. Note that you can change them
inside a group, i.e. either { .. } or \begingroup\endgroup or a
LaTeX environment, then their old value is restored at the end of the
group automatically. (Martin Scharrer, How to save variables)

On the other hand, grouping is not expandable (since the primitives \begingroup and \endgroup are not expandable).

  1. How is TeX treating grouping? Does TeX only save and restore a specific variable when an assignment to it occurs? It seems when a global assignment (e.g. font assignment: \fontdimen, \hyphenchar, \skewchar; hyphenation assignment: \hyphenation, \patterns; box size assignment: \ht, \dp, \wd; …) occurs that TeX omits saving and restoring.

  2. (EDIT) Considering a TeX command using 5 to 10 "variables". In other words, it is temporarily assigning different values to them, for example \baselineskip=30pt. Is there any good reason to avoid grouping in this situation? Or should grouping be used here because of above advantage?

Best Answer

Just to add a bit on the other answers and to try and answer the title of the question:

When to use or avoid grouping

As David mentioned it mostly depends on your use case but here are some typical areas where using grouping is advisable:

  1. When setting the \@elt in lists.

    \def\alist{\@elt a\@elt b\@elt c}
    \begingroup
      \let \@elt\@gobble  \alist
    \endgroup
    \begingroup
      \def\@elt#1{-#1- }  \alist
    \endgroup
    
  2. When defining macros that affect paragraph(s) of text.

    \def\startlines{%
    \begingroup
      \obeylines\obeyspaces
      \leavevmode
    }
    \def\endlines{\endgroup}
    
    \startlines
    
    This is a test 
    ...   ......         ....... test and another test
    
    This is a test 
    ...   ......         ....... test and another test
    
    \endlines
    
  3. When you want to split a command into two parts, for example:

     \def\index{\@bsphack\begingroup \@sanitize\@index}
     \def\@index#1{\endgroup\@esphack}
    
  4. When changing catcodes:

      \def\MyCatcodeMagic#1{%
        \begingroup
        \catcode`\ 10 %
        \ifnum \endlinechar<256 %
           ...
        \endgroup
       }
    
  5. In math commands to avoid `leaking out' of style changes

When to avoid grouping is mostly a matter of programming style. Good programming practice dictates that globals are an evil, however, is very hard to achieve this with TeX/LaTeX. The LaTeX source2e tends to use grouping for most of the important macros, even in cases such as:

       \def\frac#1#2{{\begingroup#1\endgroup\over#2}}

Can you guess why?

Related Question