How not to indent the very first line on a page in LaTeX?

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.

-

migrated from stackoverflow.comMar 28 '11 at 13:10

This question came from our site for professional and enthusiast programmers.

@Daniel: You may well get a bigger choice of answers over on tex.stackexchange.com – Brent.Longborough Mar 28 '11 at 12:43
@Brent: Thanks for link, haven't known this website :). – Daniel Langr Mar 28 '11 at 13:06
That might not be a good idea, as the reader will have no way ot knowing if the text is a continuation from the page before. – daleif Mar 28 '11 at 13:16
@Daniel: By this argument, you would never need an paragraph indent. – Paŭlo Ebermann Mar 28 '11 at 15:54
Ok, ok, I agree with your arguments, will let the indentation there :). Thank a lot. – Daniel Langr Mar 29 '11 at 11:45

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}

-
How would one go about redefining \everypar in a way that does play well with other packages? – user1129682 Jul 25 '14 at 7:54

Right at the begining of the line that you don't want to indent, type:

\noindent


For example:

\section{Introduction}
\noindent
This is the introduction.

-
Sorry, I did not read that you did not want to use noindent ;) – Escualo Mar 28 '11 at 16:02
The OP wants automatic detection if the paragraph is at the top of a page. Moreover, after \section you usually don't need \noindent anyway. – Hendrik Vogt Mar 28 '11 at 16:07

ConTeXt provides a \beforeeverypagebody token list that are executed, well, before a page body is constructed. There is also a \aftereverypagebody which is executed after the page body is constructed; and \beforeeveryshipout and \aftereveryshipout that are executed before and after a page is shipped.

To do what you want, you just need to add

\appendtoks \noindentation \to \everybeforepagebody


For some reason, this does not work on the very first page. However, you can just add a \noindentation to get rid of the indent on the first page. Thus, the following gives what you want:

\appendtoks \noindentation \to \everybeforepagebody

\setupindenting[big,yes]

\starttext \noindentation

\input knuth

\page
\input knuth

\stoptext

-