[Tex/LaTex] Listings in table of contents

listingstable of contents

I'm struggeling a little bit with the display of table of contents. I had previously a problem using natbib , and now the same issue with listings. Maybe I'm mixing something up with documentclass article and how to reat it.

That's a sample stating the issue:

\documentclass
[
a4paper,
english,
twoside,
11pt
]
{article}

\usepackage[pdftex]{graphicx}
\usepackage{float}           
\usepackage{amstext}         
\usepackage{enumerate}        
\usepackage[bottom]{footmisc} 
\usepackage{array}            
\usepackage{ntheorem}
\usepackage{theorem}
\usepackage{pdfpages}         
\usepackage{parskip}          
\usepackage{xcolor}           
\usepackage[hyphens]{url}     
\usepackage{makeidx}          
\usepackage{multicol}         
\usepackage[numbers, square]{natbib}  
\usepackage{listings}
\usepackage{hyperref}
\hypersetup{%
    pdfborder = {0 0 0}
}
%\usepackage{url}
\sloppy

\makeindex 



\setlength{\unitlength}{1cm}
\setlength{\oddsidemargin}{0.3cm}
\setlength{\evensidemargin}{0.3cm}
\setlength{\textwidth}{15.5cm}
\setlength{\topmargin}{-1.2cm}
\setlength{\textheight}{23cm}
\columnsep 0.5cm

\renewcommand{\bibsection}{\section{\refname}}


\begin{document}


%%%%%%%%%%



\normalsize

%%%%%%%%%%
\pagestyle{plain}
\pagenumbering{arabic}
\setcounter{page}{3}
\tableofcontents
\cleardoublepage


%%%%%%%%%%%

\cleardoublepage
%\phantomsection

%\nocite{*}
%\bibliographystyle{plainnat} 
%\setlength{\bibsep}{3mm} 





\lstset{language=[Objective]C, breakindent=20pt, breaklines, tabsize=1,
caption={Change Identifiers}}
\small
\begin{lstlisting}

- (void) myMethod
{
    NSLog(@"Hello there");
}

\end{lstlisting}

\cleardoublepage
\addcontentsline{toc}{chapter}{Listings}
\lstlistoflistings
%\interlinepenalty=100

%%%%%%%%%%%
\newpage                                    
\pagestyle{empty}
\centering

\  






\end{document}

The Listings is dipslayed as Listings1, but the pagenumber should be right aligned.
Can someone point me to the solution, or what I'm doing wrong?

Best Answer

Instead of

\addcontentsline{toc}{chapter}{Listings}

you can use

\addcontentsline{toc}{section}{\protect\numberline{1}{Listings}}

to produce a section-like entry with the listing number and the page number, or

\addcontentsline{toc}{section}{\protect\numberline{}{Listings}}

if you don't want the listing number to appear, but you want to preserve the indentation of an section-like entry, or

\addcontentsline{toc}{section}{Listings}

if you don't want the listing number nor the indentation. A little example showing the three options:

\documentclass{article}

\begin{document}
\tableofcontents
\addcontentsline{toc}{section}{\protect\numberline{1}{Listings}}
\addcontentsline{toc}{section}{\protect\numberline{}{Listings}}
\addcontentsline{toc}{section}{Listings}
\end{document}

enter image description here

P.S.: note that the article document class doesn't support chapter-like sectional units.