# Automatically assign a number to every paragraph

I would like to add a marginal number (don't know the right term; in german „Randnummer“) to every paragraph of text I write. I don't see any other solution, than to write every single number manually with \marginpar. This is impossible, especially when you write longer texts. Does anyone know a good solution?

This image shows what I'd like to achieve. (from: grundrechteforum.de)

-
This is very possible. You just need to add counter and change the new paragraph hook to automatically step the counter and add a marginpar with the number of the counter. Or Perhaps the lineno package could do it... –  Seamus Feb 7 '11 at 20:28
The lineno package doesn't look like it can do it. But it does something harder, so it should be possible... –  Seamus Feb 7 '11 at 21:26
Since the example in the photograph is a German legal text, I wonder if the scrjura (Komascript) document class would contain that functionality. Unfortunately, the manual isn't available in English. –  user26732 Aug 22 '13 at 13:30

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).

-
I just noted parnumbers should start with \par before \everypar to ensure that the current paragraph is closed. Otherwise you run intro trouble when it is used direct after e.g. itemize. –  Martin Scharrer Feb 7 '11 at 22:30
Why not put \everypar{} inside the end of the environment and use it in macro form: \parnumbers and if its necessary to disable it just use \endparnumbers. In my tests itemize doesn't bother it but changes \everypar itself, so that it has to be reactivated using a \parnumbers afterwards. IMHO writing it as environment is just an unnecessary restriction. –  Martin Scharrer Feb 7 '11 at 22:30
@Martin, thanks, I added the \par. The environment idea is just for easy typesetting of books like the one shown in the picture. One way or another, the standard lists and sectioning commands are going to make trouble. If I had to write such a book, I'd probably first write a document class with modified versions of all the formatting elements I'm using. –  Caramdir Feb 8 '11 at 1:49
@Caramdir: I agree, modifying the formatting elements would be the safest way here. The idea popped into my mind too. –  Martin Scharrer Feb 8 '11 at 8:16
@Caramdir: Would it be possible to modify the second example (with the environment) to use it with the book-class? Just like an even/odd page distinction. –  Thorsten Feb 8 '11 at 11:23

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
\vbox to 0pt{%
\vss
\ifodd\thepage
\hbox to \textwidth{%
\hfil
\else
\hbox to \textwidth{%
\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.

-
How easy would this be to modify to get numbers in the outer margins for twosided documents as opposed to just left margin? –  Alan Munn Feb 8 '11 at 4:24
Not too hard, and I've edited the answer accordingly –  Norman Gray Feb 8 '11 at 10:21

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.)

-
Well, just using a \marginpar also does that. –  Caramdir Feb 8 '11 at 4:43
@Caramdir I've removed the comment about other solutions now that everyone seems to have updated their's. :-) –  Alan Munn Feb 8 '11 at 13:19
\documentclass[12pt]{article}
\usepackage{lipsum}

\makeatletter
\newenvironment{numberedpars}{%
\setcounter{secnumdepth}{4}
\renewcommand\theparagraph{\arabic{paragraph}}
\renewcommand\@seccntformat[1]
{\expandafter\ifx\csname##1\endcsname\paragraph\csname
\let\old@par=\par
\def\new@par{\let\par=\old@par\paragraph{}\let\par=\new@par}
\let\par=\new@par
\par
}{}
\makeatother

\begin{document}

\begin{numberedpars}
foo \par
\lipsum
\end{numberedpars}

\end{document}


and the same for outer par numbers in twoside mode

\makeatletter
\newenvironment{numberedpars}{%
\setcounter{secnumdepth}{4}%
\renewcommand\theparagraph{%
\protect\marginpar[\hfill\bfseries\arabic{paragraph}]
{\bfseries\arabic{paragraph}}}%
\renewcommand\@seccntformat[1]%
{\expandafter\ifx\csname##1\endcsname\paragraph\csname%
the##1\endcsname\else\csname the##1\endcsname\fi}%
\let\old@par=\par%
\def\new@par{\vspace*{-\normalbaselineskip}%
\let\par=\old@par\paragraph{}\hspace*{-1em}\let\par=\new@par}%
\let\par=\new@par%
\par
}{}
\makeatother


-
Is it possible to shift the numbers to the left and add an even/odd distinction to use it with the book class? –  Thorsten Feb 8 '11 at 11:26
@Thorsten: yes, see edit –  Herbert Feb 8 '11 at 12:30
Thanks for your answer. Unfortunately there's always an empty line space between the paragraphs. –  Thorsten Feb 8 '11 at 19:53
@Thorsten: right, I changed the code in the last example for twoside. Now there is additional space. –  Herbert Feb 8 '11 at 20:43

The \everypar-"hook" could be used I suppose. But I have a sneaking suspicion there might be issues.

\newcount\parcount
\hsize=.5\hsize % just to show the effect
First paragraph with some lorem ipsum: Lorem ipsum dolor sit amet.

Second paragraph with some more of that gool old loprem ipsum goodness. Lorem ipsum dolor sit amet.

This probably isn't very robust
\bye


-
This will but a number besides headlines. And it seems that LaTeX sectioning commands reset \everypar. –  Caramdir Feb 7 '11 at 21:06
\everypar is overwritten each time someone does \everypar{...}. So we need to do \everypar\expandafter{\the\everypar...} instead, to keep the old \everypar, and add to it your hook. @Caramdir: but I don't see any other way than \everypar... –  Bruno Le Floch Feb 7 '11 at 21:09

Here's another answer along the lines of putting the paragraphs in an environment.

\documentclass{article}
\usepackage{lipsum}
\usepackage[excludeor]{everyhook}
\newcounter{parcount}
\newif\ifnumberedpars
\newenvironment{numberedpars}{%
\PushPreHook{par}{%
\ifnumberedpars
\stepcounter{parcount}%
\numberedparsfalse
\marginpar{\arabic{parcount}}%
\numberedparstrue
\fi
}%
\numberedparstrue
}{%
\PopPreHook{par}%
}
\begin{document}
\begin{numberedpars}
\lipsum
\end{numberedpars}
\end{document}


The code is fairly straight forward. At the beginning of the environment, it pushes a new hook for \everypar. At the end of the environment, it pops that hook off.

-
Looks quite nice but the problem is that it also adds a number to every section. –  Thorsten Feb 8 '11 at 11:14
@Thorsten: Yes. That is a problem. I don't have an easy fix for it though. –  TH. Feb 8 '11 at 11:35
I wonder if there is some kind of attribute which could be tested, ie. if all the sectioning commands make some kind of change which could be detected. –  morbusg Feb 8 '11 at 12:57
@morbusg: I thought about that, glanced briefly at source2e, but didn't really have time to spend on it. –  TH. Feb 8 '11 at 19:49
Most readable answer for this question yet. For future readers: to make the numbers appear on the left, use \reversemarginpar stackoverflow.com/a/1358953/39974 –  Ivan Vučica May 12 '13 at 14:13

If you want to restart the counting in every environment, add a line to the end of environment.

   \setcounter{parcount}{0}


The whole codes are here:

\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}}%
\setcounter{parcount}{0}%to restart numbering in every envirement
}{}

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

\begin{parnumbers}
\lipsum[5-8]
\end{parnumbers}

\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}

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

-