[Tex/LaTex] the difference between \@@par and \par

documentclass-writingmacrospackage-writingparagraphs

In LaTeX internal code I came accross \@@par. How is this different from \par or \newline. When should I use \@@par if I am writing a package or class file?

In general, there are so many other internal commands/macros (e.g., \z@, \p@, \protected@edef, etc) in LaTeX which I come accross while studying the internal code. Is there some documentation available regarding when and how to use them if I am writing a package or class file? Should I really bother about them at all?

Best Answer

In LaTeX the \par command gets redefined several times during a run over a document. For example, inside a tabular it does nothing: an input such as

\begin{tabular}{l}
\ttfamily\meaning\par
\end{tabular}

would print

macro:->.

(after the colon the parameter text is shown; after -> up to the period the replacement text is shown). This way, users aren't bothered with empty lines inside a tabular.

In a center, flushleft or flushright environment, the definition is more complicated, but eventually the macro \par will execute {\@@par}, where \@@par is defined in the kernel by

\let\@@par\par

at a moment when \par still has its primitive meaning. In normal situations, \par will have the primitive meaning and, in general, one should use \par in macros.

Another example: when a \parbox or minipage is started, the \@parboxrestore command is executed, which also does \let\par\@@par. Why? Because the \parbox or minipage could be in a tabular or any other place where \par has been redefined.

The kernel has an interesting set of macros:

\def\@setpar#1{\def\par{#1}\def\@par{#1}}
\def\@par{\let\par\@@par\par}
\def\@restorepar{\def\par{\@par}}

The macro \@setpar changes the meaning of \par and \@par; why making a copy of the new meaning of \par into \@par? Because one can always say \@restorepar which will restore the meaning of \par as defined with \@setpar, in cases when it's not certain that \par has the wanted meaning. The default definition of \@par is to restore the primitive meaning.

Of course, grouping plays an essential role here. For instance, \par does nothing in tabular, but as soon as \end{tabular} is executed, \par will get back the meaning it had before \begin{tabular}. So a restoration with \let\par\@@par is rarely needed; an exception is \@parboxrestore, that's intended to bring LaTeX into a fresh state as if a new document should be started (we're in a \parbox or minipage and this indeed makes sense).

Your question has probably been prompted by the line

\newenviron{solution}{}{\xappto{\temp@solnlist}{\solutionbody {\@@par}}}

in your code at Problems using listxadd and xappto

Well, this is a place where a plain \par should be used:

\newenviron{solution}
  {}
  {\xappto{\temp@solnlist}{\expandonce{\solutionbody}\noexpand\par}}

The \noexpand is needed because of \xappto and the fact that we want \par to be stored, not its expansion at the uncontrolled moment the \xappto command is performed.

Related Question