views:

338

answers:

4

I'm trying to create a new environment in my LaTeX document where indentation in the next paragraph following the environment is suppressed.

I have been told (TeXbook and LaTeX source) that by setting \everypar to {\setbox0\lastbox}, the TeX typesetter will execute this at the beginning of the next paragraph and thus remove the indentation:

\everypar{\setbox0\lastbox}

So this is what I do, but to no effect (following paragraph is still indented):

\newenvironment{example}
  {\begin{list}
     {}
     {\setlength\leftmargin{2em}}}
  {\end{list}\everypar{\setbox0\lastbox}}

I have studied LaTeX's internals as well as I could manage. It seems that the \end routine says \endgroup and \par at some point, which may be the reason LaTeX ignores my \everypar setting. \global doesn't help either. I know about \noindent but want to do this automatically.

Example document fragment:

This is paragraph text. This is paragraph text, too.

\begin{example}
  \item This is the first item in the list.
  \item This is the second item in the list.
\end{example}

This is more paragraph text. I don't want this indented, please.

Internal routines and switches of interest seem to be \@endpetrue, \@endparenv and others. Thanks for your help.

A: 

You should not mess with the \everypar token list, unless you know exactly what you are doing. Use

\setlength{\parindent}{0pt}

to get rid of indenting in the whole document.

Patrick
I'd like to get rid of indentation _only_ after some environment. Since plain tex can apparently do it (see TeXbook, p. 10), LaTeX should, too.
David
+1  A: 

Can't you avoid this by not having a blank line between your environment and the next line?

This is paragraph text. This is paragraph text, too.

\begin{example}
  \item This is the first item in the list.
  \item This is the second item in the list.
\end{example}
% (No blank line)
This is more paragraph text. I don't want this indented, please.
Geoff
Of course, I can. But since the `\section` etc. commands can do what I'm looking for (no indentation in the following paragraph even if there are blank lines in between), I'm hopeful that it can be done in an elegant and general way, if somewhat low-level.
David
+1  A: 

I couldn't get anything to work without redefining \end, but I'm certainly no expert.

The following is quite hacky, but worked in my limited testing. Of course this will interfere with nested environments (you should be able to redefine \begin to restore the old \end if you have problems).

\newenvironment{example}{%
  \bgroup
  \let\oldend=\end
  \def\end##1{\oldend{##1}\csname @afterindentfalse\endcsname
                          \csname @afterheading\endcsname}
  \begin{list}{}
    {\setlength\leftmargin{2em}}
  }{%
  \end{list}
  \egroup
}
Ivan Andrus
Doesn't work for me, sorry. I get a few LaTeX errors "can't use \spacefactor in vertical mode" and missing or extra braces and $ (the syntax of my document is fine, though).
David
Oops, I forgot to mention that it should be between \makeatletter and \makeatother. I've updated the answer to reflect this by using \csname
Ivan Andrus
Oops, I had forgotten about `\makeatletter` for a second, sorry about that. Yes, this solution is quite hackish, and I have to admit that it does bug me that I can't find a more elegant solution with `\@doendpe` and friends ... In any case, your solution works, thanks!
David
+1  A: 

Include \@afterindentfalse\@afterheading at the end of your definition.

Peter Flynn