0

For my AP Stats notes this year, I've been citing everything with line numbers instead of page numbers, as most sections wind up being less than a page and I want to be able to find things quickly. I've been using the lineno package for this. But when I generate a table of contents, everything's still cited in page numbers. Obviously, this has something to do with how sections and soforth are entered into the table of contents because they naturally use page numbers. I tried redefining \thepage to be the line number, but that just resulted in each section being cited as the first line on the next page. So for instance on this page: enter image description here All three subsections that start here are listed in the table of contents as starting on line 265, even though they very much don't. Does anyone know how to make this work? I was thinking about basically making a new .toc file that would work with line numbers, but I have absolutely no idea how that would work. I've included an MWE of what I have right now below; please let me know if anything's off.

%Using XeLaTeX for font reasons
\documentclass{article}
\usepackage{lineno}
\renewcommand{\thepage}{\thelinenumber}

\begin{document}
\tableofcontents %lists ``a section" and ``another section" as starting on line 37
\section{a section}
A bunch of text that would get rendered starting on line one
\section{another section}
A bunch more text that would get rendered starting on line ten
\newpage
Even more text that would get rendered starting on line thirty-seven
\end{document}

1 Answer 1

1

To replace the page number with the line number in the table of contents, the \addcontentsline command must be redefined.

The command is issued after composing the section name, so the file number must be reduced by one to get the correct number line.

It takes two runs to get the numbers correct on the TOC.

a

b .

c

    % !TeX TS-program = xelatex

%%%Using XeLaTeX for font reasons
\documentclass{article}
\usepackage{lineno}
\linenumbers

%%******************* added <<<<<<<<<<<<<<<
\makeatletter
\def\addcontentsline#1#2#3{%
    \addtocounter{linenumber}{-1} %  the line number of the section name
    \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thelinenumber}{}%
    \protected@file@percent}
    \stepcounter{linenumber} % back to right linenumber
}
\makeatother

\renewcommand{\contentsname}{Contents~\hfill\textbf{Line}}
%%*******************

\usepackage{kantlipsum} %dummy text

\begin{document}
\tableofcontents %

\newpage

\section{A section (line \#\thelinenumber)} 

\textbf{A bunch of text that would get rendered starting on line \#\thelinenumber}

1. \kant[1-5]

\section{Another section (line \#\thelinenumber)}

\textbf{A bunch more text that would get rendered starting on line  \#\thelinenumber}

2.  \kant[2-7]

\section{Another section (line \#\thelinenumber)}

\textbf{Even more text that would get rendered starting on line \#\thelinenumber}

3.  \kant[9]

\end{document}

This is a test with shorter sections

    % !TeX TS-program = xelatex

%%%Using XeLaTeX for font reasons
\documentclass{article}
\usepackage{lineno}
\linenumbers

%%******************* added <<<<<<<<<<<<<<<
\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{1.0ex}{3.5ex}} % add dots to TOC
\def\addcontentsline#1#2#3{%
    \addtocounter{linenumber}{-1} %  the line number of the section name
    \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thelinenumber}{}%
    \protected@file@percent}
    \stepcounter{linenumber} % back to right linenumber
}
\makeatother

\renewcommand{\contentsname}{Contents~\hfill\textbf{Line}}
%%*******************


\begin{document}
\tableofcontents %

\newpage

\section{A section (line \#\thelinenumber)} 

A bunch of text that would get rendered starting on line \#\thelinenumber

\section{Another section (line \#\thelinenumber)}

A bunch more text that would get rendered starting on line  \#\thelinenumber

\section{Another section (line \#\thelinenumber)}

Even more text that would get rendered starting on line \#\thelinenumber

\end{document}

d

1
  • Thanks!! This is exactly what I was looking for how to do. Commented May 18, 2021 at 14:02

You must log in to answer this question.

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