# Problem setting lengths in an environment (spilling outside of the environment)

I am having with spacing between paragraphs in my document, and I think I have been able to trace back to a custom environment I have defined.

\makeatletter
\def\remarkname{Remark}
\newcounter{counter@myremarks}
\newenvironment{remark}{\par\vspace{0.2cm}\par\noindent\begingroup\small%
\textbf{\remarkname~\arabic{counter@myremarks}.}~~\ignorespacesandallpars}{%
\newenvironment{remark*}{\par\vspace{0.2cm}\par\noindent\begingroup\small%
\textbf{\remarkname.}~~\ignorespacesandallpars}{%
\makeatother


The \ignorespacesandallpars command is by Martin Scharrer and comes from a previous StackExchange question.

(I know that there are many other ways - packages, or the definitions in amsmath - to get this sort of environment, but I have not yet found something that I'm completely happy with, and it is easier at the moment to use a custom definition.)

After I use this definition, I think I notice spacing being a bit wacky, and requiring additional \smallskip or \medskip commands. I wonder what I am doing wrong.

Is my problem somewhere else, or am I doing something wrong here?

UPDATE: The following snippet illustrates one of the problems (the spacing before the second itemize list is not the same than the first):

\documentclass{article}

\makeatletter
\def\ignorespacesandallpars{%
\begingroup
\catcode13=10
\@ifnextchar\par
{\endgroup\expandafter\ignorespacesandallpars\@gobble}%
{\endgroup}%
}
\def\remarkname{Remark}
\newenvironment{remark*}{\par\vspace{0.2cm}\par\noindent\begingroup\small%
\textbf{\remarkname.}~~\ignorespacesandallpars}{%
\makeatother

\begin{document}

Following is an item list
\begin{itemize}
\item One
\item ...
\end{itemize}

\begin{remark*}
Now a remark.
\end{remark*}

And another item list
\begin{itemize}
\item One
\item ...
\end{itemize}

\end{document}

-
Thanks for the code. Why are you using \@afterindentfalse\@afterheading? – Gonzalo Medina Aug 21 '12 at 19:45
@GonzaloMedina: if I recall correctly, what I wanted is to prevent the following paragraph from having indent, and to make it so no amount of spacing would change this (the fact that what follows not be indented). – Jérémie Aug 21 '12 at 21:37

Well \@afterheading is one of the reasons sectioning commands should never be used in groups ;-)

It gobally sets \@nobreaktrue and then locally sets \everypar to undo it. So after the end of your environment (undoing the \everypar setting) you have \@nobreaktrue sitting there waiting for the next list to catch it...

And \@nobreak makes a list not insert \topsep...

To avoid this mess, you must make sure \@afterheading is called outside your environment.

Replacing \@afterheading with \aftergroup\@afterheading in your definition moves the \@afterheading outside the group defined by your environment and makes \topsep reappear.

-
Thank you so much! It works... But I admit I don't exactly understand why (in the same way I didn't understand why the command I pasted above does what I want it to do :-). Could you perhaps elaborate on why using \aftergroup solves my problem? – Jérémie Aug 21 '12 at 20:19
@Jérémie I tried, but I'm afraid I have to suggest if all these @-commands sound greek to you, you should better leave them alone. LaTeX is famous for strange side effects of some internal commands, so they should only be used by those who know exactly what they are doing. – Stephan Lehmke Aug 21 '12 at 20:25
I think I've been using LaTeX for far too long enough to not know about these internal commands, but I can't find a coherent documentations on them! – Jérémie Aug 21 '12 at 20:28
@Jérémie source2e is a good documentation for internal stuff. – Stephan Lehmke Aug 21 '12 at 20:37
@Jérémie I think a lot of people are wondering why you are using \@afterheading in this place. See also Gonzalo Medina's comment. – Stephan Lehmke Aug 21 '12 at 20:43