Understanding Brace Hacks

The fourth case is actually 0110, the 1110 came from a typo in the code: Your test

E&=mc^2
+ {\ifnum0=`}\fi\blech{A&OK}\ifnum0=`{\fi}

is the test for the sixth case, it should be

E&=mc^2
+ \ifnum0=`{\fi\blech{A&OK}\ifnum0=`}\fi

instead.

This leaves {\iffalse}\fi...\iffalse{\fi} and \bgroup...\egroup. According to the TeXbook, the difference lies in the expanded version of the balance counter. The balance counter is used for example for macro definitions and we need expansion, so we can construct an additional test:

  1. Can be used to delimit the definition of an \edef.

This will fail for \bgroup...\egroup, because in

\edef\something\bgroup ABC\egroup

\bgroup ABC\egroup is interpreted as the parameter text, while there is no problem with

\edef\something{\iffalse}\fi ABC\iffalse{\fi}

Actually the combination of this with environments allows for a quite interesting hack: You can capture the fully expanded content of an environment in a macro:

\newenvironment{EDEF}[1]{\xdef#1{\iffalse}\fi}{\iffalse{\fi}}
\begin{EDEF}\ABCD
  Something interesting
\end{EDEF}

This defines a macro \ABCD as the full expansion of Something interesting.


Perhaps I should add some explanation about the \iffalse{\fi\ifnum`}=\z@\fi ... ifnum`{=\z@\fi\iffalse}\fi hack (note that the intended usage is with the “delimiters” in this order: I never meant to suggest using them in flipped order, that is, ifnum`{=\z@\fi\iffalse}\fi ... \iffalse{\fi\ifnum`}=\z@\fi).

Before writing my comment, I had been trying to figure out what the OP was attempting to achieve in connection with @egreg’s answer. I was too lazy to look into the code of the tabstackengine package, nonetheless I got the vague intuition that he was looking for a brace hack to be used to “wrap up” the definition of a command, or of an environment, in such a way that:

  • occurences of & (or \cr) tokens inside the argument of the command, or the contents of the environment, are “shielded” from being interpreted as belonging to an outer alignment, if the command or the environment happen to be used in the context of an outer alignment;

  • on the other hand, the braces should not be “executed” in the stomach, which means that no subgroup, or subformula, will be created, thus allowing proper interpretation of math atoms and preventing definitions issued inside the command or the environment from being regarded as local.

Given these requirements, the hack almost suggested itself. Indeed, consider, for example, the following alternative to @egreg’s suggested patch to the code of tabstackengine:

\makeatletter
\renewcommand\ensureTABstackMath[1]{%
  \iffalse{\fi\ifnum`}=\z@\fi
  \let\sv@TABmode\TAB@delim\TABstackMath#1\let\TAB@delim\sv@TABmode
  \ifnum`{=\z@\fi\iffalse}\fi
}
\makeatother

Recall that TeX does expand tokens when it is reading alignment entries (contrast this with the case of the alignment preamble). Now, \iffalse{\fi increments the master counter, even if the brace itself is discarded and is not contributed to the current token list, as documented on p. 385 of The TeXbook; on the other hand, the following \ifnum`}=\z@\fi (which, in general, we need in order to build a definition with properly balanced braces—note that it is not needed in this particular case, we could simply say

\makeatletter
\renewcommand\ensureTABstackMath[1]{%
  \iffalse{\fi
  \let\sv@TABmode\TAB@delim\TABstackMath#1\let\TAB@delim\sv@TABmode
  \iffalse}\fi
}
\makeatother

and it would work fine; note also that, since TeX does not expand tokens in the replacement text, it doesn’t even realize that the } in \ifnum`}=\z@\fi is part of a numeric costant), the following \ifnum`}=\z@\fi, we were saying, leaves the master counter unchanged; so, the net change is +1. Things go similarly for the ifnum`{=\z@\fi\iffalse}\fi case, with a net change in the master counter of -1, yet with no brace being actually contributed to the current token list.

Since it has the desired effect on the master counter, the construction is effective in “shielding” inner & tokens from an outer alignment; since no brace actually reaches TeX’s stomach, the construction doesn’t produce a subformula that would interfere with the concatenation of atoms in the current math list, or a subgroup that would keep definitions local.

Finally, it is obvious that the idiom cannot be used to delimit an expanded definition (\edef/\xdef), since it is meant exactly to behave as “a pair of braces that exist only for the purpose of nesting alignments, and for no other one”.


Addition

It should be noted that the \iffalse{\fi\ifnum`}=\z@\fi ... \ifnum`{=\z@\fi\iffalse}\fi pair is, in essence, exactly the pair of “brace-like” idioms that the mhsetup package (invoked by mathtools) defines as the \MH_group_align_safe_begin:/\MH_group_align_safe_end: pair. Indeed, the definition we find in mhsetup.sty is

\def \MH_group_align_safe_begin: {\iffalse{\fi\ifnum0=`}\fi}
\def \MH_group_align_safe_end:   {\ifnum0=`{}\fi}

which gives the same result, but using fewer tokens in the definition of the closing “brace”. Let us discuss briefly how it works.

  1. When the macros are being defined, the tokens in the replacement text are not “executed”, they are just stored away keeping count of {/} nested pair; since the replacement text of both macros contains balanced {/} pairs, both definitions are correctly carried out.

  2. When TeX is scanning the entry of an alignment, on the other hand, tokens are expanded, which means in particular that conditionals are evaluated. Now:

    a) for the purpose of deciding when the entry terminates, for which only the master counter matters, the evaluation of the numeric constants inside the conditionals \ifnum0=`}\fi and \ifnum0=`{}\fi (constants that are evaluated exactly because the conditionals are) have the effect documented on p. 385 of The TeXbook; thus \MH_group_align_safe_begin: increments the master counter, while \MH_group_align_safe_end: decrements it, as desired;

    b) for the purpose of deciding which tokens are forwarded to TeX’s stomach, note that, in the above definitions, all braces are discarded because they occur in the false branch of a conditional: this is true also of the } inside the \ifnum0=`{}\fi test, because the ASCII code of { is not zero!

    In particular, b) shows that there is no need to add a second conditional (namely, \iffalse ... \fi) around the } token in the definition of \MH_group_align_safe_end:. Unfortunately, a similar trick doesn’t work for \MH_group_align_safe_begin: (just think of it).

I hope to be able to further improve this answer during Easter vacation (if nobody else has done so by then!).