[Tex/LaTex] How not to indent the very first line on a page in LaTeX

indentationpage-breakingparagraphs

If the first line on a page happens to be the first line of a paragraph at the same time, I would like not to indent it (just as if it is the first line of a section). Is there a way to do this automatically? I don't want to use \noindent command, because if this line moves elsewhere, I want to indent it.

Thanks ahead for help!

Best Answer

First, I don't think this is a great idea. One problem is that if the previous paragraph ended with a full line, there will be no clue that there is a paragraph break (I suppose you could avoid this by setting \parfillskip=3em plus1fil to ensure an unfilled final line).

Anyway, to answer your question, this will be very tricky, because TeX generates paragraphs before it looks at page breaking, so by the time it knows that the first line starts on a new page it is too late to do anything about it. You will need to write something into the aux file at the beginning of each paragraph (using \everypar) that you can read back on subsequent runs to determine if this paragraph starts a new page, and if it does, to remove the indent. Unfortunately, the new linebreaks may now change the page breaking and you may need to retex repeatedly, and there is no guarantee that this process will converge.

EDIT: here is the core of a solution, to show that it is possible. It will produce a bunch of errors the first time you compile it, and it doesn't redefine \everypar in a way that plays well with other packages, but it shows the basic idea.

EDIT 2: fixed the code up a little (the previous version removed the indent for the first paragraph of the page, no matter if it started on first line of page or not)

\documentclass{article}
\raggedbottom % to allow short pages
\addtolength{\topskip}{0pt plus 10pt} % to help allow short pages
\newcounter{parno}
\makeatletter
\newcommand{\newparb}[2]{\global\@namedef{mypar@b#1}{#2}}
\newcommand{\newpare}[2]{\global\@namedef{mypar@e#1}{#2}}
\newpare{0}{0}
\let\oldpar\par
\def\par{\oldpar\protected@write\@auxout{}{\string\newpare{\theparno}{\thepage}}}
\makeatother
\usepackage{lipsum} % for dummy text
\begin{document}
\interlinepenalty=1000 % just for example, to encourage paragraphs to start on new pages
\makeatletter
\everypar={%
    \stepcounter{parno}%
    \ifnum \@nameuse{mypar@b\theparno}=\@nameuse{mypar@e\@arabic{\numexpr\value{parno}-1}}%
    \else\setbox0\lastbox\fi
    \protected@write\@auxout{}{\string\newparb{\theparno}{\thepage}}%
}
\makeatother
\lipsum[1-60]% dummy text
\end{document}