# (Re)newDocumentCommand interferes with the layout

While preparing a paper using the IEEEtran class in conference mode, I was overloading the \end command using \RenewDocumentCommand (for personnal use). The result is that an extra blank line is added to the authors:

\documentclass[conference]{IEEEtran}
\usepackage{xparse}

\title{An unimportant paper}
\author{\IEEEauthorblockN{First author}

% \let\oldend\end
% \RenewDocumentCommand\end{m}{\oldend{#1}}

\begin{document}
\maketitle
-------------------------
\end{document}


When uncommenting the two commented line, an extra blank line appears below the authors (and above the ---------line). It comes from a \crcrcommand when displaying the authors. This phenomenon does not occur when \renewcommand is used instead of \RenewDocumentCommand.

My questions are thus the following: - Why does this happen? - Though it is very easy to solve in this case, is there a way to know in advance when such phenomena can occur?

• Do you really want to modify \end? – egreg Jan 16 '17 at 9:20
• I am tracking the structure of the document with as much precision as possible. The simplest way I have found to do so is to overload \begin and \end (as well as \section, \item, ...). – Thomas Colcombet Jan 16 '17 at 9:23
• Track how? and for what? – daleif Jan 16 '17 at 9:40
• @daleif: This would require a long answer. For short, I am writing/using a package for hyperlinking a lot of information inside a document: for instance the use of a term poiting to the location of its definition. However, a variable or a term may only be used in, say, some proof, or locally in some section, and have another meaning in some other place of the document. The package (helped with some writer's command) should be able to understand things like "the variable x I am talking about here is the one that was introduced in Theorem 3.2". – Thomas Colcombet Jan 16 '17 at 10:05

The problem shows essentially with tabular, because the \endtabular command is seen too late if \end has been redefined with \RenewDocumentCommand.

\documentclass[conference]{IEEEtran}
\usepackage{xparse}

\let\oldend\end
\RenewDocumentCommand\end{m}{\oldend{#1}}

\begin{document}

\hrule
\begin{tabular}{c}
xyz \\
uvw \\
\end{tabular}
\begin{tabular}{c}
xyz \\
uvw \\
foo \\
\oldend{tabular}
\begin{tabular}{c}
xyz \\
uvw \\
\oldend{tabular}
\hrule

\end{document}


As you see, the second tabular is aligned to the first one, whereas the third one has no additional row.

It's not because of some \crcr as you suspect.

• \\ ends a row.

• Now LaTeX looks whether there's \hline or similar command and expands tokens as it goes.

• If it finds \omit or \noalign, it performs the appropriate actions (the former would come from \multicolumn, the latter from \hline).

• Other commands start a cell.

Among the “other commands” are all those defined as \protected: by programmer's choice, they stop the search for \omit and \noalign.

Thus \end is expanded when a table cell has already started, so a new row appears.

Every macro defined with \NewDocumentCommand, \RenewDocumentCommand or \DeclareDocumentCommand is \protected. Not those defined with \DeclareExpandableDocumentCommand.

By the way, you see the issue with \maketitle because there is a tabular in its workings.

\end has to be expandable.

\documentclass[conference]{IEEEtran}
\usepackage{xparse}

\title{An unimportant paper}
\author{\IEEEauthorblockN{First author}

\let\oldend\end
\DeclareExpandableDocumentCommand\end{m}{\oldend{#1}}

\begin{document}
\maketitle
-------------------------
\end{document}


N.B.: Why don’t you use \AtEndDocument{...}?

• I do not undersand why \end has to be expandable. Is it an obvious fact? In fact, this is the first time I see this phenomenon occur, while I am using similar code overloadig \end for quite some time. N.B: as I was answering @egreg, the goal is to catch all the \begin and \end in the document, not just the last one! – Thomas Colcombet Jan 16 '17 at 9:39
• @ThomasColcombet The author block is typeset in a construction called @IEEEauthorhalign. The \end part of this command is \def\end@IEEEauthorhalign{\crcr\egroup\egroup}. The \crcr inside this is not “seen” if it is protected and added too late resulting in an extra newline. – Henri Menke Jan 16 '17 at 9:46
• @Henri: Thanks. egreg's post is describing the phenomenon in detail, and now everything is clear to me. – Thomas Colcombet Jan 16 '17 at 9:55

Without xparse

\let\oldend\end
\renewcommand\end{\oldend}

• I know this solution, of course. But it does not hlp me forsee when it may hapen again... – Thomas Colcombet Jan 16 '17 at 9:40