51

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) enter image description here

3
  • 3
    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
    Commented Feb 7, 2011 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
    Commented Feb 7, 2011 at 21:26
  • 1
    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
    Commented Aug 22, 2013 at 13:30

9 Answers 9

17

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

6
  • 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. Commented Feb 7, 2011 at 22:30
  • 1
    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. Commented Feb 7, 2011 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
    Commented Feb 8, 2011 at 1:49
  • @Caramdir: I agree, modifying the formatting elements would be the safest way here. The idea popped into my mind too. Commented Feb 8, 2011 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
    Commented Feb 8, 2011 at 11:23
13

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.

3
  • 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
    Commented Feb 8, 2011 at 4:24
  • Not too hard, and I've edited the answer accordingly Commented Feb 8, 2011 at 10:21
  • @davidcarlisle is there a way to number of all paragraphs by integer stepwise incrementer values i.e. 1 to N at document level from top to bottom rather than adding scripts to each paragraph?
    – bonCodigo
    Commented yesterday
11

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

2
  • Well, just using a \marginpar also does that.
    – Caramdir
    Commented Feb 8, 2011 at 4:43
  • @Caramdir I've removed the comment about other solutions now that everyone seems to have updated their's. :-)
    – Alan Munn
    Commented Feb 8, 2011 at 13:19
8
\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 
  the##1\endcsname\else\csname the##1\endcsname\quad\fi}
  \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

enter image description here

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

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}

enter image description here

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.

5
  • Looks quite nice but the problem is that it also adds a number to every section.
    – Thorsten
    Commented Feb 8, 2011 at 11:14
  • @Thorsten: Yes. That is a problem. I don't have an easy fix for it though.
    – TH.
    Commented Feb 8, 2011 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
    Commented Feb 8, 2011 at 12:57
  • @morbusg: I thought about that, glanced briefly at source2e, but didn't really have time to spend on it.
    – TH.
    Commented Feb 8, 2011 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 Commented May 12, 2013 at 14:13
7

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

\newcount\parcount
\everypar{\global\advance\parcount by 1\llap{\bf\the\parcount\qquad}}
\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

2
  • This will but a number besides headlines. And it seems that LaTeX sectioning commands reset \everypar.
    – Caramdir
    Commented Feb 7, 2011 at 21:06
  • 2
    \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... Commented Feb 7, 2011 at 21:09
2
+25

FWIW, ConTeXt has a built-in command \setupparagraphnumbering to enable numering of paragraphs. It works correctly with section titles.

\setupparagraphnumbering[state=start,style=bold,distance=0.5em]

\starttext
\dorecurse{2}{
\startsection[title={Knuth quote}]
  \input knuth 
\stopsection}
\stoptext

enter image description here

1

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}
0
0

There is also a class called jurabook in German to achieve numbered paragraphs easily.

2
  • 2
    Welcome to TeX.SX! When you say 'easily' do you mean to say that it is the standard when using jurabook? Or do I have to set some options? Anyway, your answer might very well benefit from a short MWE that shows hos exactly one gets numbered paragraphs.
    – moewe
    Commented Mar 6, 2017 at 17:34
  • 1
    @Hakan I just skipped through the documentation but was not able to find an option that automatically numbers each paragraph. I do not think that this class provides this functionality. As far as I can see you always need to use \rn{...} at each paragraph, as the documentation says: "Es mag zwar sehr nervig erscheinen, jeden Absatz mit \rn{foo} einleiten zu müssen. Andere – im bescheidenen Rahmen meiner LATEX-Kenntnisse :-) – getestete Möglichkeiten, die mehr Automation bieten (zB mit \everpar), haben sich jedoch als kaum praktikabel erwiesen, [...]."
    – Thorsten
    Commented Mar 12, 2017 at 8:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .