1

I'm currently working on a large document and want to add line numbers for proofreading purposes. I'd like to reset the current number after every new chapter/section/subsection. To achieve this I referred to this answer.

The problem now is that this doesn't seem to work properly. The numbers don't reset after every \chapter,\section or \subsection command, but rather at the beginning of the paragraph before the command.

To illustrate this:

\documentclass[a4paper, oneside]{book}
\usepackage{polyglossia}
\usepackage{lipsum}
\usepackage{lineno}

\linenumbers
\preto{\chapter}{\resetlinenumber}
\preto{\section}{\resetlinenumber}
\preto{\subsection}{\resetlinenumber}

\usepackage{makeidx}
\makeindex

\begin{document}
\chapter{Chapter one}
\lipsum[5]

\paragraph*{paragraph} \lipsum[5]
\section{Section one}
\lipsum[5]
\paragraph*{paragraph} \lipsum[5]
\subsection{Subsection one}
\lipsum[5]
\paragraph*{paragraph} \lipsum[5]

\chapter{Chapter two}
\lipsum[4]

\paragraph*{paragraph} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec odio ex, lacinia eu blandit in, facilisis ac nisl. Aliquam mauris arcu, porta at mi ac, molestie aliquam justo. Proin lacinia nibh vitae cursus sagittis. Suspendisse potenti. Curabitur facilisis suscipit felis et malesuada. Proin sed turpis odio. Vivamus eleifend urna tempor, pretium lacus quis, pharetra tellus. Morbi sed elementum lectus.
\section{Section two}
\lipsum[5]

\paragraph*{paragraph} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec odio ex, lacinia eu blandit in, facilisis ac nisl. Aliquam mauris arcu, porta at mi ac, molestie aliquam justo. Proin lacinia nibh vitae cursus sagittis. Suspendisse potenti. Curabitur facilisis suscipit felis et malesuada. Proin sed turpis odio. Vivamus eleifend urna tempor, pretium lacus quis, pharetra tellus. Morbi sed elementum lectus.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec odio ex, lacinia eu blandit in, facilisis ac nisl. Aliquam mauris arcu, porta at mi ac, molestie aliquam justo. Proin lacinia nibh vitae cursus sagittis. Suspendisse potenti. Curabitur facilisis suscipit felis et malesuada. Proin sed turpis odio. Vivamus eleifend urna tempor, pretium lacus quis, pharetra tellus. Morbi sed elementum lectus.
\subsection{Subsection two}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec odio ex, lacinia eu blandit in, facilisis ac nisl. Aliquam mauris arcu, porta at mi ac, molestie aliquam justo. Proin lacinia nibh vitae cursus sagittis. Suspendisse potenti. Curabitur facilisis suscipit felis et malesuada. Proin sed turpis odio. Vivamus eleifend urna tempor, pretium lacus quis, pharetra tellus. Morbi sed elementum lectus.

\paragraph*{paragraph} Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec odio ex, lacinia eu blandit in, facilisis ac nisl. Aliquam mauris arcu, porta at mi ac, molestie aliquam justo. Proin lacinia nibh vitae cursus sagittis. Suspendisse potenti. Curabitur facilisis suscipit felis et malesuada. Proin sed turpis odio. Vivamus eleifend urna tempor, pretium lacus quis, pharetra tellus. Morbi sed elementum lectus.

\end{document} 

Note:

  • The "Lorem ipsum" in text form is on purpose, if I use \blindtext or something similar the error doesn't occur.
  • I'm using XeLaTeX.

I'm not sure why this is happening, but any help in solving the problem is appreciated.

2
  • 1
    Wild guess, but I think that for \resetlinenumber to work you need to have the paragraph built. Try adding a \par token before the \resetlinenumber. For example: \preto{\chapter}{\par\resetlinenumber}. Or, of course, leave an empty line before each \chapter/\section/\subsection command. Sep 7, 2018 at 17:46
  • Solved! I wish I had such wild guesses... In any case, if you want to add your comment as answer, go ahead, I‘d mark it as answered. If it isn't too much trouble could you maybe give a short explanation why you thought of this?
    – Sito
    Sep 7, 2018 at 17:53

1 Answer 1

1

As requested :)

I imagined that the issue was the paragraph breaks because you said:

if I use \blindtext or something similar the error doesn't occur

The lipsum package, by default, inserts a \par token after every paragraph, so the paragraph break occurs. In fact, if you \usepackage[nopar]{lipsum} (which suppresses that \par token), the error occurs with \lipsum commands too.

And also because you said:

The numbers don't reset after every \chapter, \section or \subsection command, but rather at the beginning of the paragraph before the command.

The sectioning commands insert a \par token when you use them, but your \resetlinenumber is inserted before that, because \preto prepends to the command. This way, \resetlinenumber instruction is seen before the \par, so the numbering is reset exactly one paragraph too early.

And, finally, because many issues happen because of the paragraph building process :)

When you input a bunch of text TeX stores it in its memory as a single line of text. Then, only when it sees a \par (or an empty line), it will break this long strip of text into several lines. And, of course, lineno needs the lines to know how to number things.

This is why lots of apparently trivial stuff that depend on where the lines are broken don't work so easily. TeX doesn't allow you to do anything after the paragraphs are broken into lines (unless you are using LuaTeX).

Just to make sure we can try with a smaller document. This will have lines numbered from 1 to 12:

\documentclass{article}
\usepackage{lineno}
\begin{document}
\linenumbers
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\resetlinenumber

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}

enter image description here

whereas this will have two sets of lines numbered 1 to 6:

\documentclass{article}
\usepackage{lineno}
\begin{document}
\linenumbers
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

\resetlinenumber
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}

enter image description here

and the only difference is the relative position of \par and \resetlinenumber :)

TL; DR

You can simply add a \par before \resetlinenumber:

\preto{\chapter}{\par\resetlinenumber}
\preto{\section}{\par\resetlinenumber}
\preto{\subsection}{\par\resetlinenumber}
0

You must log in to answer this question.

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