2

I am looking for a way to annotate the pdf with line numbers (like we get with the lineno package). But I want these numbers to correspond to the line number in the source (tex) file, so I know where to look to change it.

Using this answer I have built the following—numbers in parenthesis are the source file number I wish was at the margin (nevermind the lorem ipsum).

\documentclass[a5paper,12pt]{article}
\usepackage[a5paper]{geometry}

% Get the source file number
\makeatletter
\ifnum\inputlineno=\m@ne
\let\showlineno\@empty
\else
\def\showlineno{line \the\inputlineno}
\fi
\makeatother

% Package to write numbers in the margin
\usepackage{lineno}

% set the file number as the number lineno will show
\renewcommand\LineNumber{\the\inputlineno}
\begin{document}
\linenumbers
(20) This is a story of a person, and another, and another.
(21) Who tried to have some fun, doing something or another.
(22) And they did, just so, until the afternoon was gone.
(23) Thus ends our little story.

(25) A second paragraph begins, like a dance.
(26) While green serenades are played at a stance,
(27) and mosquitos bite be, pray me, delicious blood!
(28) I think we should move in this tasty neighbourhood!

(30) And so we did it all again! (But in a single line.)

(32) This is a story of a person, and another, and another. Who tried to have some fun, doing something or another. And they did, just so, until the afternoon was gone. Thus ends our little story.

(34) A second paragraph begins, like a dance. While green serenades are played at a stance, and mosquitos bite be, pray me, delicious blood! I think we should move in this tasty neighbourhood!
\end{document}

The code above is good enough for my personal use, but all lines which end up in the same paragraph are "numbered" with the last line number of a line in the paragraph (i.e.: I get labels 24 (x4), 29 (x4) 31, 33 (x4) and 35 (x4)). Is there a simple way to avoid this?

Edit: I have just noticed that if we print just the first line number (of each paragraph), then we are done (or, at least, I would be completely satisfied).

2
  • 3
    You get the linenumber when lineno prints the number and this isn't done by source line. Isn't synctex enough for you? Jun 22, 2018 at 16:22
  • I had never really looked into synctex.. I guess the time has come! I'll get back to this question once I've taken a look at it (my current setup is emacs for typing and zathura for viewing).
    – Tássio
    Jun 23, 2018 at 8:32

1 Answer 1

0

This seems to do the trick, but prints line numbers ONLY for the first line of each paragraph.

Problem. The way that lineno seems to work is to write (on the margin) increasing numbers, starting from the value \linenumber has at the start of the paragraph. Setting \Linenumber to \inputlineno would only wield the correct input line number for the first line of each paragraph.

Solution (?) To avoid printing incorrect (source) line numbers, we print only the first linenumber of each paragraph, and to check whether we are at the first line of a paragraph, we use the everyhook package to set a counter (tnlinecounter) to \inputlineno at the start of each paragraph. (We can then compare the values of linenumber and tnlinecounter to figure out whether we're at the first line.)

\documentclass[a5paper,12pt]{article}
\usepackage[a5paper]{geometry}

% Package to write numbers in the margin
\usepackage{lineno}

\newcounter{tnparcounter}
\usepackage[excludeor]{everyhook}
\PushPreHook{par}{%
  \resetlinenumber[\the\inputlineno]%
  \setcounter{tnparcounter}{\the\inputlineno}}

% set the file number as the number lineno will show
\renewcommand\LineNumber{%
  \ifnum\value{linenumber}=\value{tnparcounter}%
  \thelinenumber\fi}

\begin{document}
\linenumbers
(20) This is a story of a person, and another, and another.
(21) Who tried to have some fun, doing something or another.
(22) And they did, just so, until the afternoon was gone.
(23) Thus ends our little story.

(25) A second paragraph begins, like a dance.
(26) While green serenades are played at a stance,
(27) and mosquitos bite be, pray me, delicious blood!
(28) I think we should move in this tasty neighbourhood!

(30) And so we did it all again! (But in a single line.)

(32) This is a story of a person, and another, and another. Who tried to have some fun, doing something or another. And they did, just so, until the afternoon was gone. Thus ends our little story.

(34) A second paragraph begins, like a dance. While green serenades are played at a stance, and mosquitos bite be, pray me, delicious blood! I think we should move in this tasty neighbourhood!
\end{document}

You must log in to answer this question.

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