[Tex/LaTex] Redefining ToC, LoF, and LoT

table of contents

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

\newpage
\tableofcontents

\newpage
\listoffigures
\phantomsection
\addcontentsline{toc}{section}{\listfigurename}

\newpage
\listoftables
\phantomsection
\addcontentsline{toc}{section}{\listtablename}

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
    \addcontentsline{toc}{section}{\listfigurename}
}

\let\oldlistoftables\listoftables
\def\listoftables{
    \newpage
    \oldlistoftables
    \phantomsection
    \addcontentsline{toc}{section}{\listtablename}
}

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

Best Answer

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
  \addcontentsline{toc}{section}{\listfigurename}%
}
\preto\listoftables{%
  \clearpage
  \csname phantomsection\endcsname
  \addcontentsline{toc}{section}{\listtablename}%
}

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.