带有行号的目录?

带有行号的目录?

对于我今年的 AP Stats 笔记,我一直使用行号而不是页码来引用所有内容,因为大多数章节最终都少于一页,我希望能够快速找到内容。我一直在使用这个lineno包来实现这一点。但是当我生成目录时,所有内容仍然以页码引用。显然,这与章节等如何输入目录有关,因为它们自然使用页码。我尝试重新定义\thepage为行号,但这只会导致每个章节被引用为下一页的第一行。例如,在这一页上: 在此处输入图片描述 从这里开始的所有三个小节在目录中都列为从第 265 行开始,尽管它们实际上并非如此。有人知道如何做到这一点吗?我原本想制作一个.toc可以使用行号的新文件,但我完全不知道它是如何工作的。我在下面附上了我现在拥有的 MWE;如果有任何错误,请告诉我。

%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

要用目录中的行号替换页码,\addcontentsline必须重新定义命令。

该命令是在编写节名后发出的,因此必须将文件号减一才能得到正确的行号。

需要运行两次才能使目录上的数字正确。

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}

这是一个部分较短的测试

    % !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

相关内容