# Testing for a blank line (“paragraph”) immediately after \begin{document}

(With this question, I hope to resolve an edge case relevant for my quoting package.)

Suppose I define a customized environment for displayed text. It resembles the standard LaTeX environment quote (i.e., it is based on the list environment), but as a twist, the first line of the environment should be indented if (and only if) the environment is preceded by a blank line. At the moment, I accomplish this by testing for vertical mode (and setting \itemindent accordingly). However, this test fails for environments positioned immediately after \begin{document}. Whether I write

\begin{document}

\begin{myquote}% First line of following text should be indented


or

\begin{document}
%
\begin{myquote}% First line of following text shouldn't be indented


the first line of the myquote environment is always indented. (Seee full MWE below.) How should my test for a blank line ("paragraph") be amended/modified in order to also work immediately after \begin{document}?

\documentclass{article}

\makeatletter
\newenvironment{myquote}{%
\list{}{%
\setlength{\rightmargin}{\leftmargin}%
\setlength{\itemindent}{%
\ifvmode
\parindent
\else
\z@
\fi
}%
}%
\item\relax
}{%
\endlist
}
\makeatother

\newcommand{\sometext}{Hello, here is some text without a meaning. This
text should show, how a printed text will look like at this place.}

\begin{document}
%
\begin{myquote}
\emph{This line shouldn't be indented, but it is.} \sometext
\end{myquote}

Nomal running text. \sometext
%
\begin{myquote}
This line shouldn't be indented, and it isn't. \sometext
\end{myquote}

Normal running text. \sometext

\begin{myquote}
This line should be indented, and it is. \sometext
\end{myquote}

Normal running text. \sometext

\end{document}

-
Why do you set the itemindent? – Marco Daniel Aug 31 '11 at 18:33
@Marco: Because that's the length that controls the indentation of the item's first paragraph. See the definition of quotation in article.cls -- it contains \itemindent \listparindent. – lockstep Aug 31 '11 at 18:37
:Than I think in the second myqoute should be indented? – Marco Daniel Aug 31 '11 at 18:57
@Marco: No -- there's no blank line before it, but a line with a comment sign. – lockstep Aug 31 '11 at 18:59

This seems to work, but it possibly breaks if other environments that redefine \par are used at document start.

\usepackage{etoolbox}
\makeatletter
\def\lock@reset{\global\let\iflock@check\iffalse
\global\let\par\endgraf
\global\let\lock@reset\relax}
\appto\document{\let\iflock@check\iftrue\def\par{\endgraf\lock@reset}}

\newenvironment{myquote}{%
\list{}{%
\setlength{\rightmargin}{\leftmargin}%
\setlength{\itemindent}{%
\ifvmode
\iflock@check\z@\else\parindent\fi
\else
\z@
\fi
}%
}%
\item\relax
}{%
\endlist\lock@reset
}
\makeatother


# Caveat

The code seems to work, but I don't recommend using it, for a couple of reasons. First a practical one. How many documents begin with a quotation right after \begin{document}?

Second reason. Doing stuff with \document is a Bad Thing; actually the code can go into \AtBeginDocument, but what about other packages that act in the same way on \par?

Doing nasty tricks with \par just for the sake of symmetry is not worth the labor.

@Lockstep: please, don't add such a "feature" to your package; rather add a warning and a way to avoid indentation in a quoting environment just after \begin{document}; it's easier and will probably never be used.

Besides, the quoting environment always has an initial indentation when it follows some environments, with or without an intervening blank line: for example flushleft, center, flushright or sloppypar. So a key for deciding whether an indentation is desired might prove very useful.

-
Indeed -- seems to work! Do I understand correctly that \endgraf stores (or was \let to) the original meaning of \par? – lockstep Aug 31 '11 at 21:53
It's also instructive how \lock@reset kills itself after its first application. :-) – lockstep Aug 31 '11 at 21:58
Yes, \endgraf is a copy of primitive \par; it's a Plain thing, that's been carried over. There's also a more LaTeXish \@@par; look also for \@par in latex.ltx. Once \lock@reset has done once its business (at start of document or later) it's not needed any more. – egreg Aug 31 '11 at 22:10
In my quoting package, I have defined a boolean switch quo@afterquo that reports if a quoting environment is immediately (without a paragraph in between) preceded by another quoting environment. If so, vertical mode is ignored when setting the environments first-line indentation. But it didn't occur to me to use a similar trick to hook into \document. Answer accepted -- I hope it's okay that your code (or at least its general idea) will make it into a future version of quoting -- of corse, with proper attribution. – lockstep Aug 31 '11 at 22:20
No problem with you using the code, of course; but, please, test it thoroughly: \@doendpe uses \@restorepar and this might have bad consequences. But maybe not. – egreg Aug 31 '11 at 22:24