链接到目录中错误页码的列表

链接到目录中错误页码的列表

我想在我的文档中自定义列表格式,并在我的项目中做了以下更改:

% before \begin{document}
\makeatletter
\AtBeginDocument{%
\renewcommand\lstlistoflistings{\bgroup
  \let\contentsname\lstlistlistingname
  \def\l@lstlisting##1##2{\@dottedtocline{1}{0em}{2.3em}{Listing ##1}{##2}}
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lol}}%
  \tableofcontents \egroup}
}
\makeatother

% after \begin{document}
\tableofcontents
\listoffigures
\listoftables
\addcontentsline{toc}{chapter}{Listings}
\lstlistoflistings

应用这些更改后,目录中列表的页码指向放置表格列表的页面。我该如何修复此问题?

MWE如下:

\documentclass[a4paper,12pt]{report}
\usepackage[utf8]{inputenc}

\title{TEST}
\author{Abdullah YILDIZ}
\date{October 2020}

\usepackage{listings}


\makeatletter
\AtBeginDocument{%
\renewcommand\lstlistoflistings{\bgroup
  \let\contentsname\lstlistlistingname
  \def\l@lstlisting##1##2{\@dottedtocline{1}{0em}{2.3em}{Listing ##1}{##2}}
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lol}}%
  \tableofcontents \egroup}
}
\makeatother

\begin{document}

\maketitle

\tableofcontents
\listoffigures
\listoftables
\addcontentsline{toc}{chapter}{Listings}
\lstlistoflistings

\chapter{INTRODUCTION}\label{chp:Introduction}

Interest in exploring the genetic data of organisms is growing everyday.

Miusov, as a man man of breeding and deilcacy, could not but feel some inwrd qualms, when he reached the Father Superior's with Ivan: he felt ashamed of havin lost his temper. He felt that he ought to have disdaimed that despicable wretch, Fyodor Pavlovitch, too much to have been upset by him in Father Zossima's cell, and so to have forgotten himself. "Teh monks were not to blame, in any case," he reflceted, on the steps. "And if they're decent people here (and the Father Superior, I understand, is a nobleman) why not be friendly and courteous withthem? I won't argue, I'll fall in with everything, I'll win them by politness, and show them that I've nothing to do with that Aesop, thta buffoon, that Pierrot, and have merely been takken in over this affair, just as they have."

He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had becom much less valuable, and he had indeed the vaguest idea where the wood and river in quedtion were.

\begin{lstlisting}[caption={My Code},language=C]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}
\end{document}

答案1

您需要一个 clearpage,以便在正确的页面上执行 \addcontentsline。

\documentclass[a4paper,12pt]{report}
\usepackage[utf8]{inputenc}

\title{TEST}
\author{Abdullah YILDIZ}
\date{October 2020}

\usepackage{listings}


\makeatletter
\AtBeginDocument{%
\renewcommand\lstlistoflistings{\bgroup
  \let\contentsname\lstlistlistingname
  \def\l@lstlisting##1##2{\@dottedtocline{1}{0em}{2.3em}{Listing ##1}{##2}}
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lol}}%
  \cleardoublepage %<---------------
  \addcontentsline{toc}{chapter}{Listings}%
  \tableofcontents \egroup}
}
\makeatother

\begin{document}

\maketitle

\tableofcontents
\listoffigures
\listoftables

\lstlistoflistings

\chapter{INTRODUCTION}\label{chp:Introduction}

Interest in exploring the genetic data of organisms is growing everyday.

Miusov, as a man man of breeding and deilcacy, could not but feel some inwrd qualms, when he reached the Father Superior's with Ivan: he felt ashamed of havin lost his temper. He felt that he ought to have disdaimed that despicable wretch, Fyodor Pavlovitch, too much to have been upset by him in Father Zossima's cell, and so to have forgotten himself. "Teh monks were not to blame, in any case," he reflceted, on the steps. "And if they're decent people here (and the Father Superior, I understand, is a nobleman) why not be friendly and courteous withthem? I won't argue, I'll fall in with everything, I'll win them by politness, and show them that I've nothing to do with that Aesop, thta buffoon, that Pierrot, and have merely been takken in over this affair, just as they have."

He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had becom much less valuable, and he had indeed the vaguest idea where the wood and river in quedtion were.

\begin{lstlisting}[caption={My Code},language=C]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}
\end{document}

相关内容