In my (sub)section title I use \hfill to add some text to the title on the right side of the page. I want this text to also show up in the table of contents next to the page number. This works well for the section but not for the subsection. I would also prefer to keep the dots for the subsection in the table of contents.

\section{Test section \hfill{{\normalsize\normalfont Text}}}

\subsection{Test subsection \hfill{{\normalsize\normalfont Text}}}


The following provides an interface for adding "tags" to specific units within the ToC.

\addsectiontag{<unit>}{<tag>} creates a macro for that specific section <unit>. Then, within each unit's ToC entry, the existence of this macro is checked. If it exists, the tag is set in a box with white background (so as to overlay with possible leaders within the ToC) immediately to the left of the page number box.

\documentclass{article}

\usepackage{tocloft,etoolbox,xcolor,hyperref}

\makeatletter
\AtBeginDocument{%
\patchcmd{\tableofcontents}% <cmd>
{\@starttoc}% <search>
{\noindent\hfill{\bfseries Page}\par%
\@starttoc}% <replace>
{}{}% <success><failure>
}

}
\renewcommand{\cftsecafterpnum}{%
\ifcsname sectiontag\endcsname
\makebox[0pt][r]{\smash{\colorbox{white}{\strut\sectiontag}\hspace{\@pnumwidth}}}%
\global\let\sectiontag\relax
\fi
}
\renewcommand{\cftsubsecafterpnum}{%
\ifcsname subsectiontag\endcsname
\makebox[0pt][r]{\smash{\colorbox{white}{\strut\subsectiontag}\hspace{\@pnumwidth}}}%
\global\let\subsectiontag\relax
\fi
}
\makeatother

\begin{document}

\tableofcontents

\section{A section}

\subsection{A subsection}

\section{Another section}

\subsection{Another subsection}

\end{document}


Due to the processing of the ToC, the tag addition is called before the actual section unit.

For adding the sectional tag to the document body section heading/title as well, the following is sufficient:

\documentclass{article}

\usepackage{tocloft,etoolbox,xcolor,hyperref}

\makeatletter
\AtBeginDocument{%
\patchcmd{\tableofcontents}% <cmd>
{\@starttoc}% <search>
{\noindent\hfill{\bfseries Page}\par%
\@starttoc}% <replace>
{}{}% <success><failure>
}
\newcommand{\glet}{\global\let}
\let\sectiontag\relax
\let\subsectiontag\relax

\patchcmd{\H@old@sect}% <cmd>
{#8}% <search>
{#8\hfill\@nameuse{#1tag}\expandafter\glet\csname #1tag\endcsname\relax}% <replace>
{}{}% <success><failure>

\@namedef{#1tag}{#2}% Define tag for section title in document body
}
\renewcommand{\cftsecafterpnum}{%
\ifcsname sectiontag\endcsname
\makebox[0pt][r]{\smash{\colorbox{white}{\strut\sectiontag}\hspace{\@pnumwidth}}}%
\global\let\sectiontag\relax
\fi
}
\renewcommand{\cftsubsecafterpnum}{%
\ifcsname subsectiontag\endcsname
\makebox[0pt][r]{\smash{\colorbox{white}{\strut\subsectiontag}\hspace{\@pnumwidth}}}%
\global\let\subsectiontag\relax
\fi
}
\makeatother

\begin{document}

\tableofcontents

\section{A section}

\subsection{A subsection}

\section{Another section}

\subsection{Another subsection}

\end{document}


No testing is done to see whether the tag will fit within the line. As such, titles that are too wide may show a left-aligned tag.

• Thanks for your help! Is there also a way to add the text to the (sub)section title itself? Like this – Kay H Feb 26 '16 at 7:01
• @KayH: so the title in the ToC and the title set inside the document? – Werner Feb 26 '16 at 7:16
• Yes, I would like 'Test' on the left next to the page number in the ToC, what your solution does perfectly. I would also like 'Test' on the right side of the page on the same line as the (sub)section title. To clarify, my document has different Scopes, to notify the reader what the scope of the (sub)section is, I want to display the scope next to the (sub)section title. – Kay H Feb 26 '16 at 7:35
• Do you think that is possible? I thought myself of not breaking the line after the section title and outline it on the same line on the right. Problem I then get is that the first line after that is indented and that looks weird. – Kay H Mar 1 '16 at 8:17
• @KayH: See my updated answer. – Werner Mar 1 '16 at 16:39

I redefined \@dottedtocline to be more like \l@section. I can't figure out why the last Texts don't quite line up. Both should be exactly \@pnumwidth from the right margin.

\documentclass{article}

\makeatletter
\def\@dottedtocline#1#2#3#4#5{%
\ifnum #1>\c@tocdepth \else
\vskip \z@ \@plus.2\p@
{\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
\parindent #2\relax\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\@tempdima #3\relax
{#4}\nobreak\hfil\nobreak% removed \leaders and replaced \hfill
\hb@xt@\@pnumwidth{\hfill\normalfont \normalcolor #5}%
\par}%
\fi}
\makeatother

\begin{document}
\tableofcontents

\section{Test section \hfill{\normalsize\normalfont Text}}

\subsection{Test subsection \hfill{\normalsize\normalfont Text}}

\end{document}