# Redefining ToC, LoF, and LoT

Before I was generating ToC, LoF, and LoT using the following:

\newpage
\tableofcontents

\newpage
\listoffigures
\phantomsection

\newpage
\listoftables
\phantomsection


Now I am trying to writing a class file, I want to make them "automatically" begin with a new page as well as adding LoF and LoT to ToC when:

\tableofcontents
\listoffigures
\listoftables


are called.

I am trying to "redefining" above 3 commands using the original commands but without fortune it doesn't work (there's no new page, and LoF and LoT will not be added to ToC):

\let\oldtableofcontents\tableofcontents
\def\tableofcontents{
\newpage
\oldtableofcontents
}

\let\oldlistoffigures\listoffigures
\def\listoffigures{
\newpage
\oldlistoffigures
\phantomsection
}

\let\oldlistoftables\listoftables
\def\listoftables{
\newpage
\oldlistoftables
\phantomsection
}


I will be appreciated if someone can tell me if there is anything wrong on my approach (I did some same stuff to \appendix and it works properly)

Thanks,

Golson

You're placing \phantomsection and \addcontentsline in the wrong place: they should be immediately after going to the new page, otherwise, if the list of figures occupies more than one page, the page reference in the table of contents would be wrong. Also, the anchor will be at the start of the list, rather than at the end.

The easiest method for doing what you want is to load etoolbox:

\usepackage{etoolbox}
\preto\listoffigures{%
\clearpage
\csname phantomsection\endcsname
}
\preto\listoftables{%
\clearpage
\csname phantomsection\endcsname
}


I chose \clearpage because it's “better programming”, but there should be no real difference; I used also

\csname phantomsection\endcsname


so that it works independently of loading hyperref.

Your code should work as long as the \phantomsection and \addcontentsline instructions are placed ahead of the \listoffigures and \listoftables instructions:

\let\oldtableofcontents\tableofcontents
\renewcommand\tableofcontents{%
\newpage
\oldtableofcontents}

\let\oldlistoffigures\listoffigures
\renewcommand\listoffigures{%
\newpage
\phantomsection
\oldlistoffigures}

\let\oldlistoftables\listoftables
\renewcommand\listoftables{%
\newpage
\phantomsection
\oldlistoftables}


Assuming the Table of Contents, the List of Figures, and the List of Tables should occur at the very start of the document, you can automate their generation by encasing your existing instructions in an \AtBeginDocument{...} instruction.

Here's a full MWE (the "body" of the document is obviously contrived, as it's just meant to generate entries in the ToC, the LoF, and the LoT):

\documentclass{report} % select the document class that's right for you
\AtBeginDocument{%
\newpage
\tableofcontents
\newpage
\phantomsection
\listoffigures
\newpage
\phantomsection
\listoftables
\begin{document}
\chapter{First chapter}
\begin{figure}\caption{A figure} \label{fig:first} \end{figure}
\begin{table} \caption{A table}  \label{tab:first} \end{table}
\end{document}


Addendum: To give a bit more structure to your preamble, you could (a) create a separate macro called, say, \tocloflot, that generates the ToC, LoF, and LoT, and (b) call that macro inside the \AtBeginDocument call. E.g., something like the following:

\newcommand\tocloflot{%
\newpage
\tableofcontents
\newpage
\phantomsection
\listoffigures