# Is it possible to automatically enumerate sentences in LaTeX?

1.0.1 Is it possible to automatically enumerate sentences in LaTeX?

2.0.1 I want to thoroughly work through and criticize a longer text. 2.0.2 To facilitate quoting, I would like to typeset the text and enumerate the sentences in this way. 2.0.3 Some time ago, I saw this done in a complex game ruleset.

2.1.1 In the old ruleset, the enumeration was typeset in subscript. 2.1.2 The sentence enumeration was a continuation of the chapter enumeration. 2.1.3 In all aspects except adding the sentence number, the text should be typeset in the normal way. 2.1.4 The numbering should be added automagically without the use of a \beginsentence macro.

3.0.1 In case this works at all, is it possible to additionally reference sentences by their numbers [labels], and have the label automatically updated in case the numbering in the text changes? 3.0.2 Of course, 2.1.4 implies something along the lines of \begin{sentencenumbering} or so.

-
You could change ., ! and ? to be active (i.e. a macro) and insert the number of the next sentence. However this doesn't handles the first sentence of a paragraph and would add a number after the last sentence. Some \everypar trickery could do it, but I'm not sure. –  Martin Scharrer Apr 11 '11 at 18:40
I think having the number at the end of the sentence would be acceptable. 4.0.1 How would I turn that off for parts of the text? 4.0.2 Hmm, would I put the number before or after the punctuation 4.0.3? –  Posipiet Apr 11 '11 at 18:49
At least . is used not just for ending a sentence, as Prof. Drov Nuts from St. Anford will be quick to point out. Would it be acceptable to define special macros (or active symbols) to denote ., ?, and !? –  Christian Lindig Apr 11 '11 at 18:52
Since it is common practice to make the space after the period of an abbreviation an absolute space, could the macro take that into account? –  Posipiet Apr 11 '11 at 18:56
@Christian,Posipiet: Yes, changing . etc. would be dangerous and would not be 100% foolproof. However, with the default \nofrenchspacing the dots after Prof. etc. should be followed by something special anyway like \ , \@ or ~ which could be tested by the dot-macro. –  Martin Scharrer Apr 11 '11 at 19:04

As other have pointed out, doing it completely automatically is probably quite difficult. If you want to use the \label-\ref mechanism, you would have to insert labels anyway. Let's pick some character which is usually not used in input, such as the vertical bar |. Ten minutes of hacking and we end up with:

\documentclass{article}

\newcounter{sentence}
\newcounter{para}

\makeatletter

\catcode\|=\active
\def|{\@ifnextchar[%] to keep my editor happy
\start@label\start@nolabel}

\def\start@label[#1]{\ifvmode \start@para@label[#1]\else \start@sent@label[#1]\fi}
\def\start@nolabel{\ifvmode \start@para@nolabel\else \start@sent@nolabel\fi}

\def\start@para@label[#1]{%
\refstepcounter{para}%
\label{#1}\leavevmode}

\def\start@sent@label[#1]{%
\refstepcounter{sentence}%
\label{#1}%
\thesentence~}

\def\start@para@nolabel{%
\stepcounter{para}\leavevmode}

\def\start@sent@nolabel{%
\stepcounter{sentence}%
\thesentence~}

\makeatother

\renewcommand{\thepara}{\thesection.\arabic{para}}
\renewcommand{\thesentence}{\thepara.\arabic{sentence}}

\begin{document}

\parindent=0pt
\parskip=1em

||These rules must be followed. |The end of a paragraph is indicated
as usual with a blank line.

bar. |It follows from~\ref{parstart} and~\ref{sentstart} that a new
paragraph actually starts with two vertical bars.

Without vertical bars, nothing special happens. This might be useful
to comment on the formal rules above or below.

|[parref]|Each vertical bar takes an optional argument. |If
given, it is used as a label. |[sentref]For example, this is
sentence~\ref{sentref} of paragraph~\ref{parref}.

\end{document}


I use the fact that after a \par we're in vertical mode to distinguish the two uses of |. But then we need to explicitly \leavevmode, since the beginning of a paragraph itself does not insert material causing us to switch to horizontal mode. If you want to be able to reference both whole paragraphs and single sentences, we need the double || at the start of each paragraph (they might both have an optional argument). If you never need to reference whole paragraphs, it's easy to change the syntax so that only a single | is needed at the start of a paragraph; in fact, it would be much simpler, since | could be implemented as a single macro with optional argument (which, when in \ifvmode, steps the paragraph counter before doing the other stuff).

Added I avoided using \everypar since it's not very reliable if lots of other things are happening. However, wrapping stuff in an environment might allow one to use \everypar and provide a simpler syntax. The biggest problem is really to allow the use of labels; we have to tell LaTeX when and how to look for a label.

-
How do you tell LaTeX “how … to look for a label”? I see that you are setting \labels in the code and \ref them. But how does LaTeX know which counters to print for the label reference? –  Konrad Rudolph Apr 12 '11 at 11:34
Nice one, thanks! –  Posipiet Apr 12 '11 at 15:22
@Konrad: That's the difference between \stepcounter and \refstepcounter. A \label in itself does not know what kind of thing if is supposed to refer to (equation, section, chapter, para....); it simply refers to "the latest number generated by \refstepcounter. A common mistake inside table environments is to put the \label before the \caption; in that case, the \label is a(nother) reference to the \section (chapter, subsection, ...) containing the table. –  Villemoes Apr 12 '11 at 16:55
@Villemoes And \@addtoreset causes the whole hierarchy from section down to sentence counter being used? Then I understand. Thanks for the explanation. –  Konrad Rudolph Apr 12 '11 at 16:58
Well, not quite. The hierarchy is used because of how I explicitly defined \thepara ("use however the current section is typeset, add a dot, and add the current value of the para counter written with arabic numerals"). Similarly for \thesent. The \@addtoreset{sent}{para} causes the sent counter to be reset every time something modifies the para counter. Without it, sentences would be enumerated consecutively throughout the text, which might also be desirable in some cases. –  Villemoes Apr 12 '11 at 17:05

Not easily1 with TeX or LaTeX or anything else.

The problem is what is generally known in natural language processing as Sentence boundary disambiguation

Sentence boundary identification is difficult because punctuation marks are often ambiguous. A period may denote an abbreviation, decimal point, an ellipsis, or an email address - not the end of a sentence. In addition sentences can end by exclamation marks or question marks.

A better approach is to pre-process the file outside TeX. The NLTK written in Python might be a starting point.

1 not easily meaning, if you devote a good chunk of your time you may be able to define a TeX parser to capture 95% of cases using TeX.

-

Obviously Yiannis is right.

However, if you can live with a trade-off then you can perhaps just redefine the macros \\ and \par (which is implicitly inserted whenever you leave an empty line) and write your sentences like this:

First sentence.\\
Second sentence.\\

Third sentence.


And end up with:

1.1 First sentence. 1.2 Second sentence.
2.1 Third sentence.

This requires two counters, one counting the sentences and one counting the paragraphs.

-
Damn. Now this answer has two upvotes but I notice that I don’t have any idea how to implement it. My naive idea of redefining the commands isn’t enough. Let’s see whether somebody comes up with a solution so that I can delete this useless answer. –  Konrad Rudolph Apr 11 '11 at 20:32

## The package scrjura implements enumeration of sentences

I gave an example here: http://tex.stackexchange.com/a/87344/4736

-

I have used automatically generated labels for generating a nomenclature with references to the place where a variable is introduced and explained in the main text.

You might be able to automatically construct meaningful labels from chapter, section, subsection, ... and so on ... snippets and end up with

\label{sentence:chapSnippet:sectionSnippet:subSectionSnippet:parSnippet}


At the beginning of each of these, redefine the snippet. Like:

\chapter{Insects}
\renewcommand{\chapSnippet}{insects}


You would then end up with labels like insects:humblebee:I for the first sentence (roman I) of the humblebee section in Insects. It's a kludge, but can still use custom labels inserted everywhere. A custom ref command can give you almost any reference format you like.

ADDED: Does anyone know a solution to this question: Building labels out of counters ? I didn't test the answer provided there (no latex on this machine).

-

## Also, paralist comes to mind.

\documentclass{scrartcl}
\pagestyle{empty}
\usepackage{paralist}

\begin{document}
\begin{inparaenum}[1.]
\item \label{first:sentence} This is a test.
\item Second sentence referring to \ref{first:sentence}.
\end{inparaenum}
\end{document}
`