# Why do I have to put the center environment in a group for \@afterindentfalse\@afterheading to suppress indentation?

Consider the following two macros, the only difference being that one has the environment inside a group:

\documentclass{article}
\makeatletter
\newcommand\thisworks{%
{\begin{center}
$\ast$
\end{center}}
\@afterindentfalse

\newcommand\thisdoesnt{%
\begin{center}
$\ast$
\end{center}
\@afterindentfalse

\makeatother
\usepackage{lipsum}
\begin{document}
\lipsum[1]

\thisworks

\lipsum[5]

\thisdoesnt

\lipsum[42]
\end{document}


Why does the first suppress the indentation while the second doesn't?

-

Having a quick look at the definition of \end{center} we see that it just ends a trivlist,

\def\endcenter{\endtrivlist}


now as to why Lamport would use a trivlist to define the centering and indirectly the center macros is a subject for another question, it suffices at this point to just say he was very fond of them, although I am sure he had some valid reasons for doing so.

Changing your code slightly and introducing a trivlist you can observe the effect:

\documentclass{article}
\makeatletter

\newcommand\thisworks{%
$$\ast$$
\@afterindentfalse

\newcommand\thisdoesnt{%
{\begin{trivlist}\item\end{trivlist}}
$$\ast$$
\@afterindentfalse
}

\makeatother
\usepackage{lipsum}
\begin{document}
\lipsum[1]

\thisworks

\lipsum[5]

\thisdoesnt

\lipsum[42]
\end{document}


If you uncomment the grouping brackets at the \begin{trivlist} line, you will see the effect. The code is all interwoven in the source2e.pdf.

To understand better LaTeX's buggy behaviour here, I will illustrate it with another minimal:

\documentclass{article}
\begin{document}

This is the normal behavior of a:
\begin{trivlist}\item list\end{trivlist}
trivlist that is not enclosed in a group.

If you enclose the list in a group the lines after the list:
{\begin{center} center\end{center}}
are indented. LaTeX is buggy in this respect.

\end{document}


This results in:

If you group trivlist or the center commands results in the same buggy behaviour. In your example the latter allows the \@afterindentfalse\@afterheading to take effect as it can apply everypar or indent at the beginning of the paragraph, whereas in the first example above, it can't as the following text is considered the last portion of the previous paragraph.

-
Ah yes, the pain-in-the-neck hat is "let's define everything as a list". –  Joseph Wright Oct 19 '11 at 18:32
@JosephWright it fits nicely with the fact that the only data structure in TeX is (token) lists. The only typesetting structure in LaTeX is (triv) lists. –  Bruno Le Floch Oct 19 '11 at 20:52
But this doesn't answer the question of why an (ungrouped) trivlist stops the suppressing of indentation... –  Seamus Oct 19 '11 at 21:19
@Seamus ...Sorry thought it was obvious:). I added some more detail. –  Yiannis Lazarides Oct 20 '11 at 2:54

\endtrivlist calls \@endparenv which will cause \@doendpe to be executed which "suppress[es] the paragraph indentation in text immediately following a paragraph-making environment" (LaTeX2e sources, p. 227). \@doendpe will override \@afterindentfalse\@afterheading. Note that you can suppress indentation of the following paragraph by adding % (a comment sign) in the line after \thisdoesnt in the document body of your MWE.

See also What exactly does \@doendpe do?

-