19

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
  \@afterheading}

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

\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?

lockstep
  • 250,273
Seamus
  • 73,242

2 Answers2

14

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
  \@afterheading}

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

\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:

enter image description here

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.

yannisl
  • 117,160
8

\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?

lockstep
  • 250,273