# How can I patch the abstract, list of figures, and list of tables macros to be included in the ToC automatically?

We can definitely use \phantomsection \addcontentsline{toc}{chapter}{<name>} right after we begin the environments or type the relevant macro, which would include abstract, list of figures and list of tables in the ToC. Is here a way to patch these commands so that we no longer need to type \phantomsection \addcontentsline{toc}{chapter}{<name>}?

This question is meant to teach me how to patch commands with an example, not that I don't want to manually use \phantomsection and \addcontentsline

\documentclass[notitlepage]{book}
\usepackage[no-math]{fontspec}
\usepackage[inline]{enumitem}
\usepackage{blindtext}
\usepackage[showframe]{geometry}
\usepackage{tocloft}
\usepackage{etoolbox}
\patchcmd{\frontmatter}{\pagenumbering{roman}}{\pagenumbering{Roman}}{}{}

% ===== Define abstract environment =====
\makeatletter
\if@titlepage
\newenvironment{abstract}{%
\titlepage
\null\vfil
\@beginparpenalty\@lowpenalty
\begin{center}%
\bfseries \abstractname
\@endparpenalty\@M
\end{center}}%
{\par\vfil\null\endtitlepage}
\else
\newenvironment{abstract}{%
\if@twocolumn
\section*{\abstractname}%
\else
\small
\begin{center}%
{\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%
\end{center}%
\quotation
\fi}
{\if@twocolumn\else\endquotation\fi}
\fi
\makeatother

\begin{document}

\frontmatter

\pagestyle{plain}

\tableofcontents

\cleardoublepage
\begin{abstract}
\thispagestyle{plain}
\blindtext
\end{abstract}

\cleardoublepage
\listoffigures

\cleardoublepage
\listoftables

\mainmatter

\Blinddocument
\Blinddocument
\Blinddocument

\end{document}


There is nothing to patch inside abstract for the book class -- there is no such environment at all for book.

If report or article is used, the abstract package by Peter Wilson/Will Robertson can be applied with the addtotoc option.

If there is no abstract environment, it has to be either defined in a free manner or the code must be copied over from the aforementioned classes as well as \abstractname, then say \usepackage[addtotoc]{abstract}.

The solution checks whether the class is report, article or something else and defines the abstract environment, then loads the abstract package afterwards to give it a chance modifying the environment.

The ToC entries for list of figures and list of tables can be achieved with tocbibind as mentioned by Johannes_B.

\documentclass[notitlepage]{book}
\usepackage[no-math]{fontspec}
\usepackage[inline]{enumitem}
\usepackage{blindtext}
\usepackage[showframe]{geometry}
\usepackage{tocbibind}
\usepackage{tocloft}
\usepackage{etoolbox}
\patchcmd{\frontmatter}{\pagenumbering{roman}}{\pagenumbering{Roman}}{}{}

% ===== Define abstract environment =====
\makeatletter
% Do nothing, everything seems to be setup already
% Do nothing, everything seems to be setup already
}{%
% Any other class that does not provide an abstract environment

% We have to define \abstractname and the abstract environment (most likely)
% After that, load the abstract package

\@ifundefined{abstractname}{\newcommand{\abstractname}{Abstract}}{}
\@ifundefined{abstract}{%

\if@titlepage
\newenvironment{abstract}{%
\titlepage
\null\vfil
\@beginparpenalty\@lowpenalty
\begin{center}%
\bfseries \abstractname
\@endparpenalty\@M
\end{center}}%
{\par\vfil\null\endtitlepage}
\else
\newenvironment{abstract}{%
\if@twocolumn
\section*{\abstractname}%
\else
\small
\begin{center}%
{\bfseries \abstractname\vspace{-.5em}\vspace{\z@}}%
\end{center}%
\quotation
\fi}
{\if@twocolumn\else\endquotation\fi}
\fi

% Eventually a check about the existence of \endabstract, i.e. \@ifundefined{endabstract}{}{}

}{%
% abstract environment presumably exists
}
\makeatother

\begin{document}

\frontmatter

\pagestyle{plain}

\tableofcontents

\cleardoublepage
\begin{abstract}
\thispagestyle{plain}
\blindtext
\end{abstract}

\cleardoublepage
\listoffigures

\cleardoublepage
\listoftables

\mainmatter

\Blinddocument
\Blinddocument
\Blinddocument

\end{document}