TransWikia.com

List of listings linked to incorrect page number in TOC

TeX - LaTeX Asked by yildizabdullah on October 28, 2020

I want to customize list of listings format in my document and did the following changes in my project:

% before begin{document}
makeatletter
AtBeginDocument{%
renewcommandlstlistoflistings{bgroup
  letcontentsnamelstlistlistingname
  defl@lstlisting##1##2{@dottedtocline{1}{0em}{2.3em}{Listing ##1}{##2}}
  letlst@temp@starttoc def@starttoc##1{lst@temp{lol}}%
  tableofcontents egroup}
}
makeatother

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

After applying these changes, page number for the list of listings in TOC points to the page where list of tables is placed. How can I fix this?

The MWE is as follows:

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

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

usepackage{listings}


makeatletter
AtBeginDocument{%
renewcommandlstlistoflistings{bgroup
  letcontentsnamelstlistlistingname
  defl@lstlisting##1##2{@dottedtocline{1}{0em}{2.3em}{Listing ##1}{##2}}
  letlst@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}
#include<stdio.h>

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

One Answer

you need a clearpage so that the addcontentsline is executed on the right page.

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

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

usepackage{listings}


makeatletter
AtBeginDocument{%
renewcommandlstlistoflistings{bgroup
  letcontentsnamelstlistlistingname
  defl@lstlisting##1##2{@dottedtocline{1}{0em}{2.3em}{Listing ##1}{##2}}
  letlst@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}

Correct answer by Ulrike Fischer on October 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP