0

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


\pagestyle{headings}

\cleardoublepage
\begin{abstract}
    \thispagestyle{plain}
    \blindtext
\end{abstract}
\phantomsection \addcontentsline{toc}{chapter}{\abstractname}


\cleardoublepage
\phantomsection \addcontentsline{toc}{chapter}{\listfigurename}
\listoffigures

\cleardoublepage
\phantomsection  \addcontentsline{toc}{chapter}{\listtablename}
\listoftables


\mainmatter

\Blinddocument
\Blinddocument
\Blinddocument


\end{document}
1

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.

enter image description here

\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
\@ifclassloaded{report}{%
  % Do nothing, everything seems to be setup already
}{\@ifclassloaded{article}{%
    % 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}%
        % \addcontentsline{toc}{section}{\protect\abstractname} Or add it here....
        \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
    }
  }% End of \@ifclassloaded{article}
}% End of \@ifclassloaded{report}
\makeatother


\usepackage[addtotoc]{abstract}




\begin{document}




\frontmatter

\pagestyle{plain}

\tableofcontents


\pagestyle{headings}

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


\cleardoublepage
\listoffigures

\cleardoublepage
\listoftables


\mainmatter

\Blinddocument
\Blinddocument
\Blinddocument


\end{document}

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.