[Tex/LaTex] How to add inject text into a ToC with the scope being a specific table of contents (\tableofcontents or \localtableofcontents)

bookmarkstable of contents

Question

How can I add text to a table of contents, yet limit its scope to that specific table, and whose format can be manipulated and does not have a page number?

Conditions

  • I can hide it from \localtableofcontents
  • I can show it in a specific \localtableofcontents and hide from \tableofcontents
  • I can add a bookmark to it (and a page reference to the page where the following content starts (in my case this would be the same page as Statement 1)

Attempts

I realized shortly after trying to fulfill the first criterion that I was at a standstill. The following describes my attempt.

  1. \addcontentsline{toc}{part}{Special Documents}

  2. \addtocontents{toc}{\vspace{4ex}\protect\noindent\parbox[t]{\textwidth}{\Huge\textcolor{red}{Special Documents}}\par}

Number 1 allows me to take advantage of the fact that my current setup makes parts only visible in the global ToC. This is not ideal, because it removes flexilibilty. Most importantly, this adds an entry, but the entry is not formatted and the entry also has a page number.

Number 2 allows me to add a nice, formatted, non-numbered title to my table of contents. However, it adds it to every local table of contents, which is not good.

\documentclass{article}
\usepackage{fontspec}
\usepackage{etoc}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{bookmark}
\newcommand{\inputlanguage}[2]{%
    \newpage\pdfbookmark{#1}{bkm#1}%
    \etoctoccontentsline{part}{#1}%
    #2
}%
\newcommand{\inputotherdoc}[2]{%
    \newpage\pdfbookmark{#1}{bkm#1}%
    \etoctoccontentsline{part}{#1}%
    #2
}%

\begin{document}
\setcounter{tocdepth}{0} % make ToC only contain Parts
\renewcommand{\contentsname}{\protect\noindent\parbox[t]{\textwidth}{\Huge\textcolor{red}{Language Directory}}\par} % In reality formatting is done with tocloft
\tableofcontents
\renewcommand{\contentsname}{Table of Contents}
\setcounter{tocdepth}{4} % reset back to default for local ToCs
\newpage

\inputlanguage{da}{\localtableofcontents\section{At vove er, at miste fodfæstet for en stund ikke at vove er, at miste sig selv}}
\lipsum[1]
\inputlanguage{de}{\localtableofcontents\section{Wer rastet, der rostet.}}

\lipsum[1]
\inputlanguage{en}{\localtableofcontents\section{Education is what remains after one has forgotten what one has learned in school.}}
\lipsum[1]

% Problem is here
 \addcontentsline{toc}{part}{Special Documents} % I need to inject text here and be able to: change font, change size, change, add new paragraphs, remove page number (imitate the the Language Directory text (using titlesec in original document)
 \addtocontents{toc}{\vspace{4ex}\protect\noindent\parbox[t]{\textwidth}{\Huge\textcolor{red}{Special Documents}}\par} % Second Attempt

 \inputotherdoc{Statement 1}{{\Huge Secret Information\par\bigskip}Here is some top-secret, specialized text.}
 \inputotherdoc{Statement 2}{{\Huge Specialized Information\par\bigskip}Here is some highly specialized text.}

\end{document}

Output

In the example below, the "Special Documents" should only appear in the language ToC (\tableofcontents) and not in the local ToCs.
enter image description here

Best Answer

The simplest technique is to add a command to the .toc file via \addtocontents{toc}{\protect\mycommand} and then get this \mycommand to perform some action and redefine itself to do nothing. This needs no package.

However I use etoc to illustrate that the \localtableofcontents will not have the extra heading. (not shown in the image which only illustrates the main TOC)

\documentclass{book}
\usepackage{etoc}
\usepackage{color}

% This will self-erase itself after first use
\newcommand*\SpecialDocumentsTocHeading
{\vspace{4ex}\noindent\parbox[t]{\textwidth}{\Huge\textcolor{red}{Special
      Documents}}\par
 \global\let\SpecialDocumentsTocHeading\empty }

\begin{document}

% This Main TOC will have the Special Documents Heading.
\etocsettocdepth{section}
\tableofcontents

\part{ONE}
\chapter{Test chapter one}

\etocarticlestyle % resets default to use \section*, not \chapter* for TOC heading
\localtableofcontents

\section{Section one one}
\subsection{Subsection one one one}
\subsection{Subsection one  one two}
\section{Section one two}
\subsection{Subsection one two one}
\subsection{Subsection one  two two}

\chapter{Test chapter two}

\localtableofcontents

\section{Section two one}
\subsection{Subsection two one one}
\subsection{Subsection two one two}
\section{Section two two}
\subsection{Subsection two two one}
\subsection{Subsection two  two two}
\section{Section two three}
\subsection{Subsection two three one}
\subsection{Subsection two  three two}

\part{TWO}
\localtableofcontents

\chapter{Test chapter three}
\section{Section three one}
\subsection{Subsection three one one}
\subsection{Subsection three one two}

\chapter{Test chapter four}
\section{Section four one}
\subsection{Subsection four one one}
\subsection{Subsection four one two}
\subsection{Subsection four one three}
\section{Section four two}
\subsection{Subsection four two one}
\subsection{Subsection four two two}
\subsection{Subsection four two three}


\addtocontents{toc}{\protect\SpecialDocumentsTocHeading}
\chapter{Top secret information}

\chapter{Ultra top secret information}

\end{document}

a toc

If one needs the Special Documents in a later TOC, not the first one, then one should for example do in the preamble:

\newcommand*{\SpecialDocumentsTocHeadingActive}
    {\vspace{4ex}\noindent\parbox[t]{\textwidth}{\Huge\textcolor{red}{Special
          Documents}}\par
     \global\let\SpecialDocumentsTocHeading\empty }
\newcommand*{\SpecialDocumentsTocHeading}{}
% or rather slightly more efficient \let\SpecialDocumentsTocHeading\empty

Then just before the TOC where one wants the Special Documents heading to appear one does:

\renewcommand*{\SpecialDocumentsTocHeading}{\SpecialDocumentsTocHeadingActive}
% or with a less LaTeX-like but a bit more efficient syntax:
% \let\SpecialDocumentsTocHeading\SpecialDocumentsTocHeadingActive