As LaTeX requires eTeX now it would be much easier to replace all the definitions of robust commands with \DeclareRobustCommand[*]... by \protected[\long]\def..., wouldn't it?
Which advatages come along with the the complicated definition of \DeclareRobustCommand? It should be also mentioned that it is immpossible to find out the definition of a command defined this way by using \show or \meaning. Example: When you write \meaning\TeX you get macro:->\protect \TeX which seems to be nonsense as a command always expanding to itselt lets TeX crash. Please help me understandig this.
MWE:
\documentclass{article}
\begin{document}
% latex.ltx contains
% \DeclareRobustCommand\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
\meaning\TeX% macro:->\protect \TeX % This is useless.
\TeX
% define \TeX by \protected\def
\protected\def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125emX\@}
\meaning\TeX% The real definition is shown.
\TeX
\end{document}
\meaning\TeX, the expansion has a command containing a space: that's the entire trick of\DeclareRobustCommand. It might look the same, but it's not. – Joseph Wright Apr 08 '20 at 12:31\expandafter\show\csname TeX\space\endcsname. Note that\DeclareRobustCommandwas introduced long before LaTeX required e-TeX. This comment might be relevant: https://tex.stackexchange.com/questions/513337/expandable-test-for-an-empty-token-list-methods-performance-and-robustness#comment1298131_513337 :-) – Phelype Oleinik Apr 08 '20 at 13:44\protected(for example: https://github.com/latex3/latex2e/issues/203), but moving all of them would break LaTeX everywhere. Also there's the advantage that Ulrike mentioned that\protectcan have different meanings, while a\protectedmacro has the same protection everywhere. – Phelype Oleinik Apr 08 '20 at 14:18\protectedwould fail and where\DeclareRobustCommandis really necessary? – Weißer Kater Apr 08 '20 at 15:29\protected\defcannot substitute\DeclareRobustCommandto get the desired result. – Weißer Kater Apr 08 '20 at 15:34\DeclareRobustCommandmeaning. Other than that, try\protected\def\end#1{\csname end#1\endcsname\@checkend{#1}\expandafter\endgroup\if@endpe\@doendpe\fi\if@ignore\@ignorefalse\ignorespaces\fi}and then use\begin{tabular}{|cc|}a&b\end{tabular}. – Phelype Oleinik Apr 08 '20 at 16:30\end(but without thetabularno output is produced:LaTeX Error: \begin{document} ended by \end{}.(job aborted, no legal \end found)– Weißer Kater Apr 08 '20 at 17:25\end{}? Seems like you have a typo somewhere. Try this:\documentclass{article}\begin{document}\makeatletter\protected\def\end#1{\csname end#1\endcsname\@checkend{#1}\expandafter\endgroup\if@endpe\@doendpe\fi\if@ignore\@ignorefalse\ignorespaces\fi}\begin{tabular}{|cc|}a&b\\\end{tabular}\end{document}. Also, in mytabularexample it was missing a\\. It doesn't throw an error, but the output is wrong. – Phelype Oleinik Apr 08 '20 at 19:28