3

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

3

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.

3

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

\let\oldlistoftables\listoftables
\renewcommand\listoftables{%
    \newpage
    \phantomsection
    \addcontentsline{toc}{section}{\listtablename}
    \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
\usepackage[colorlinks]{hyperref}
\AtBeginDocument{%
   \newpage
   \tableofcontents
   \newpage
   \phantomsection
   \listoffigures
   \addcontentsline{toc}{section}{\listfigurename}
   \newpage
   \phantomsection
   \listoftables
   \addcontentsline{toc}{section}{\listtablename}}
\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}

The table of contents that results from this test document looks like this:

enter image description here

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
   \addcontentsline{toc}{section}{\listfigurename}
   \newpage
   \phantomsection
   \listoftables
   \addcontentsline{toc}{section}{\listtablename}}
\AtBeginDocument{\tocloflot}
  • Thank you. And adding it a new command is quite a good idea~ – oldfatcrab Feb 13 '14 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.