[Tex/LaTex] Adding some text in the page containing the table of contents

table of contents

How to add some text somewhere (middle and at a given distance say 3cm above of the beginning of the table of contents) at the top of the page containing the Table of contents?

Something like the image below.

\documentclass[12pt]{book}
\title{hello}
\begin{document}
\tableofcontents
\chapter{one}
\chapter{two}
\end{document}

enter image description here

Best Answer

Usually, \tableofcontents uses a \chapter* heading in book, i.e. the heading formatter macro \@makeschapterhead has to be slightly modified, and an injection of an 'arbitrary' \mytextbeforetocheading command to executed.

In order to restrict the modifications to this toc only (and not affecting any further \chapter*), all has to be done a group.

\documentclass[12pt]{book}

\usepackage{xpatch}

\newcommand{\mytextbeforetocheading}{%
  \begingroup
  \centering%
  \Huge  \bfseries Some Text

  \endgroup
  \vskip3\baselineskip% 
}

\makeatletter
\xpatchcmd{\tableofcontents}{%
  \chapter%
}{%
  \begingroup
   \def\@makeschapterhead##1{%
    \vspace*{50\p@}%
    \mytextbeforetocheading%
    {\parindent \z@ \raggedright
      \normalfont
      \interlinepenalty\@M
      \Huge \bfseries  ##1\par\nobreak
      \vskip 40\p@
    }}
  \chapter%
}{\typeout{success}}{\typeout{failure}}
\xapptocmd{\tableofcontents}{\endgroup}{}{} % Close the group
\makeatother

\title{hello}
\begin{document}
\tableofcontents
\chapter{one}
\chapter{two}
\end{document}

enter image description here