Automatically assign a number to every paragraph

I don't think that overriding \everypar would give you the result that you want (at least not with changing a lot of other commands that also produce \pars in places that you don't want to have numbered).

An easy solution, that does however involve typing three additional characters per paragraph is to define a small command (say \p) that inserts a marginpar with the counter. In that way you can easily specify which paragraphs you want to have numbered. For example:

\documentclass{book}
\usepackage{mparhack}   % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum}     % for dummy text

% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}

\newcounter{parcount}
\newcommand\p{%
    \stepcounter{parcount}%
    \leavevmode\marginpar[\hfill\parnum]{\parnum}%
}

\begin{document}    
\p \lipsum[1]

\p \lipsum[2]

\section{A section}

\p \lipsum[3-4]

\begin{enumerate}
  \item \p \lipsum[3-4]
  \item \p \lipsum[5]
\end{enumerate}
\end{document}

Alternatively, you can keep the definition of \everypar local by creating an environment:

\documentclass{book}
\usepackage{mparhack}   % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum}     % for dummy text

% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}

\newcounter{parcount}
\newenvironment{parnumbers}{%
   \par%
   \everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]{\parnum}}%
}{}

\begin{document}
\begin{parnumbers}
    \lipsum[1-10]
\end{parnumbers}

\section{A section}

\begin{parnumbers}
  \lipsum[4-5]
\end{parnumbers}
\end{document}

Keep in mind that inside the parnumbers environment you shouldn't use any complicated formatting (like lists or such).


As noted in another answer \everypar is used for multiple things in the LaTeX core. It's often redefined in one lexical scope or another, so using it for this might work, but you'd be fighting special cases all the way, which means it probably wouldn't be robust. If you do manage to get that to work, though (I may be pessimistic here), I'd like to see that.

If I recall correctly, a per-paragraph hook is useful enough that I think there's to be support for it in LaTeX 3, but that won't help just now, obviously.

When I've had to do a similar thing, I've just defined a simple macro which I invoke at the beginning of each paragraph:

\newcounter{paranum}
% The following isn't quite right,
% as it seems to slightly change the spacing after the first line,
% in some circumstances.
% Good enough for the moment, however.
\newcommand\p{\refstepcounter{paranum}%
  \hskip0pt
  \vadjust{%
    \vbox to 0pt{%
      \vss
      \ifodd\thepage
        \hbox to \textwidth{%
          \hfil
          \hbox to 0pt{\quad\emph{\tiny\theparanum}\hss}}
      \else
        \hbox to \textwidth{%
          \hbox to 0pt{\hss\emph{\tiny\theparanum}\quad}\hfil}
      \fi
      \vskip3pt}}}

This isn't ideal, as the comment notes, but it's worked for me, and been more fully adequate than I expected, so I've never been driven to improve it. Having an explicit \p macro to call isn't as neat as having it happen automatically, but it means I never get a paragraph being numbered gratuitously.

The \refstepcounter means that paragraphs are labellable.

The \ifodd test is likely to fail near the tops of pages. The TeX FAQ explains the problem and points to possible solutions.

Edited to add an even/odd distinction.


I don't think that hacking \everypar is really a good idea, as it might break all sorts of other things. It seems to me you need explicit markup for paragraphs. Here's an example using the titlesec package.

\documentclass[twoside]{report}
\usepackage{titlesec}
\usepackage{chngcntr}
\usepackage{lipsum} % for dummy text
\titleclass{\numpar}{straight}[\section]
\newcounter{numpar}
\renewcommand{\thenumpar}{\arabic{numpar}}
\counterwithout{numpar}{section} % from the chngcntr package
\titleformat{name=\numpar,page=odd}[rightmargin] {\normalfont
\bfseries\filright}
{\thenumpar}{.5em}{}
\titleformat{name=\numpar,page=even}[leftmargin] {\normalfont
\bfseries\filleft}
{\thenumpar}{.5em}{}
\titlespacing{\numpar}
{1pc}{0ex plus .1ex minus .2ex}{1pc}
\newcommand*{\newpar}{\numpar{}}
\begin{document}
\chapter{}
\section{A section}
\newpar\lipsum[1]
\newpar\lipsum[2]
\newpar\lipsum[3]
\newpar\lipsum[4]
\newpar\lipsum[5]
\end{document}

This is set up to put the numbers in the outer margins in a two-sided document (which would be normal for a book.)