Well, when I said that the code was self-explanatory, I wanted to express that, by loooking at the examples, it was easy to see how to define and use your environment and to understand the effect of the new macro \AddBeforeEndtabularx. Of course, the piece of code you quoted is not obvious, even for me. To fully understand it, it is necessary to know some low level TeX commands and concepts, such as token lists. See these threads (
this one and
this other), for example, for useful references and advice.
Anyway, the macro \TX@find@end is defined in tabularx. Focusing on your environment, LaTeX starts scanning the contents of testbox and collects everything it finds into a token list (a "stream" of macros and characters) that, by now, remains unprocessed. Every time it finds an \end command, LaTeX stops scanning to check if its argument is "testbox". This check is done precisely through the following lines in the definition of \TX@find@end:
since \@tempa is the argument of the \end that has been found and \TX@ is a macro previously defined as "testbox". If true, LaTeX adds to the token list the stuff in \AddBeforeEndtabularx (this is my modification of \TX@find@end)
Code: Select all
\toks@\expandafter{\the\toks@\AddBeforeEndtabularx}%
and starts processing the token list (so really beginning to write the table):
If false, LaTeX has found the end of an environment embedded in the table (such as a minipage or an itemize environments). In this case, LaTeX adds the command "\end{found environment}" to the token list
Code: Select all
\toks@\expandafter {\the\toks@\end{#1}}
and continues scanning the contents of testbox