3

I have a LaTeX document with some chapter and a list of figures and tables. Something like this:

\tableofcontents
\addcontentsline{toc}{chapter}{\listfigurename}
\listoffigures
\addcontentsline{toc}{chapter}{\listtablename}
\listoftables
\chapter{Chapter 1}
\chapter{Chapter 2}
\chapter{Chapter 3}

So the result in the table of contents would be:

List of tables ................... i
List of figures ................. ii
Chapter 1 .................... 1
Chapter 2 .................... 2
Chapter 3 .................... 3

But i want to have in the document the lists at the beginning and in the table of contents i want them at the end, like this:

Chapter 1 ...................... 1
Chapter 2 ...................... 2
Chapter 3 ...................... 3
List of tables ................ i
List of figures .............. ii

I tried this:

\tableofcontents
\listoftables
\listoffigures
\chapter{section 1}
\chapter{section 2}
\chapter{section 3}
\addcontentsline{toc}{chapter}{\listfigurename}
\addcontentsline{toc}{chapter}{\listtablename}

But then the page number is wrong:

Chapter 1 .................... 1
Chapter 2 .................... 2
Chapter 3 .................... 3
List of tables ............... 4
List of figures ............. 5

Does anyone know a solution, how to change the position of the lists in the toc with the correct page number?

2 Answers 2

3

refcount allows you to extract page references in an expandable way. The following method uses the \label-\ref system to set a \label on the first page of the LoF/LoT and then add the entries to the ToC where they are needed, expanding the page number using \getpagerefnumber:

enter image description here

\documentclass{book}

\usepackage{refcount}

\begin{document}

\frontmatter
\tableofcontents

\clearpage
\label{listoffigures}
\listoffigures

\clearpage
\label{listoftables}
\listoftables

\mainmatter
\chapter{First chapter}
\chapter{Second chapter}
\chapter{Third chapter}

% Insert delayed LoF/LoT
\addtocontents{toc}{\protect\contentsline{chapter}{\listfigurename}{\getpagerefnumber{listoffigures}}}
\addtocontents{toc}{\protect\contentsline{chapter}{\listtablename}{\getpagerefnumber{listoftables}}}
\end{document}
1
  • Thank you for your nice answers! As you mentioned, i use hyperref. So if i add two {} at the end, it compiles: \addtocontents{toc}{\protect\contentsline{chapter}{\listfigurename}{\getpagerefnumber{listoffigures}}{}{}}. But now there is no hyperlink on it. I found solutions for the hyperlink to the actual chapter: \addtocontents{toc}{\protect\contentsline{chapter}{\listfigurename}{\getpagerefnumber{listoffigures}}{chapter*.\thechapter}{}}. But i don't know how to link to the LoF? I tried things like \ref{listoffigures}, \pageref{listoffigures}, but it not works. Any ideas?
    – Zeussi
    Jul 27, 2016 at 8:30
2

Below is a very special use-case for your scenario. I'm sure it'll need more work if you're using hyperref or other ToC-related packages.

The idea is to update the macro that sets every ToC-related entry to condition specifically on whether it's setting the LoF or the LoT. Here's what your typical .toc would look like:

\contentsline {chapter}{List of Figures}{ii}
\contentsline {chapter}{List of Tables}{iv}
\contentsline {chapter}{\numberline {1}First chapter}{1}
\contentsline {chapter}{\numberline {2}Second chapter}{3}
\contentsline {chapter}{\numberline {3}Third chapter}{5}

We conditionally check using a string comparison between the second argument of \contentsline whether it matches either \listfigurename or \listtablename. If that's the case, we store all the information for use later (in macros \delayedLoF and \delayedLoT). These macros are then inserted into the ToC using \addtocontents{toc}{\protect<macro>} to set the entry wherever you call it within the document.

The insertions are \protected so as to not expand, but the eventual .toc will resemble

\contentsline {chapter}{List of Figures}{ii}
\contentsline {chapter}{List of Tables}{iv}
\contentsline {chapter}{\numberline {1}First chapter}{1}
\contentsline {chapter}{\numberline {2}Second chapter}{3}
\contentsline {chapter}{\numberline {3}Third chapter}{5}
\delayedLoF 
\delayedLoT 

enter image description here

\documentclass{book}

% Update \contentsline to condition on LoF/LoT
\let\oldcontentsline\contentsline
\renewcommand{\contentsline}[3]{%
  \ifnum\pdfstrcmp{#2}{\listfigurename}=0
    \def\delayedLoF{\oldcontentsline{#1}{#2}{#3}}
  \else\ifnum\pdfstrcmp{#2}{\listtablename}=0
    \def\delayedLoT{\oldcontentsline{#1}{#2}{#3}}
  \else
    \oldcontentsline{#1}{#2}{#3}
  \fi\fi
}

\begin{document}

\frontmatter
\tableofcontents

\clearpage
\addcontentsline{toc}{chapter}{\listfigurename}
\listoffigures

\clearpage
\addcontentsline{toc}{chapter}{\listtablename}
\listoftables

\mainmatter
\chapter{First chapter}
\chapter{Second chapter}
\chapter{Third chapter}

% Insert delayed LoF/LoT
\addtocontents{toc}{\protect\delayedLoF}
\addtocontents{toc}{\protect\delayedLoT}

\end{document}

If your LoF and LoT entries will necessarily be at the end of the ToC, one could just patch \tableofcontents to insert them automatically.

You must log in to answer this question.

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