1

I am trying to modify the look of my toc. In the end it should look like this: enter image description here I managed to insert the "Page" but i have no idea how to add the lines.

Here is a small working example which shows my current version:

\documentclass{article}
\usepackage{tocloft}
\setcounter{tocdepth}{1}
\addtocontents{toc}{~\hfill\textbf{Page}\par}
\begin{document}
\tableofcontents
\newpage
\section{section1}
\subsection{subsection1}
\subsection{subsection2}
\subsection{subsection3}
\section{section2}
\section{section3}
\end{document}
  • Welcome to TeX.SX! What should happen on multi-line entries? – TeXnician Jun 18 '18 at 10:37
  • Thanks :-) There shouldn´t be any. The names of the sections are short and the tocdepth will stay at 1. The red lines around "page" are more important than the other ones. – Florian Jun 18 '18 at 11:13
1

Here is a suggestion with package tocbasic (can not be used together with tocloft):

\documentclass{article}
\usepackage{xcolor}
\usepackage{tocbasic}

\setcounter{tocdepth}{1}

\newcommand*\tocrule[2][-1.5\dp\strutbox]{%
  \makebox[0pt][r]{\color{#2}\rule[#1]{\textwidth}{.4pt}}%
}
\newcommand*\pagenumberwithtocrule[2]{#2\tocrule{#1}}
\addtocontents{toc}{%
  ~\hfill
  \textbf{Page}%
  \tocrule{red}%
  \tocrule[\dimexpr\ht\strutbox+2\dp\strutbox]{red}%
  \par
}

\DeclareTOCStyleEntry[
  pagenumberformat=\pagenumberwithtocrule{orange}
]{tocline}{section}


\begin{document}
\tableofcontents
\newpage
\section{section1}
\subsection{subsection1}
\subsection{subsection2}
\subsection{subsection3}
\section{section2}
\section{section3}
\end{document}

Result:

enter image description here

0

Just re-define the macro that renders the toc entry. The original definition is in article.cls, i just added one line (see inline comment).

Edit: Added red lines around "Page":

\documentclass{article}
\usepackage{tocloft}
\setcounter{tocdepth}{1}
%% Need color package for red lines:
\usepackage{color}
\addtocontents{toc}{%
  \leavevmode
  \raise\baselineskip\hbox{\rlap{\color{red}\rule{\textwidth}{.4pt}}}%
  ~\hfill\textbf{Page}\par
  \rlap{\color{red}\rule{\textwidth}{.4pt}}}
\makeatletter
\renewcommand*\l@section[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
    \addvspace{1.0em \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \rlap{\rule{\textwidth}{.4pt}}%           <--- This line renders the rule
    \endgroup
  \fi}
\makeatother
\begin{document}
\tableofcontents
\newpage
\section{section1}
\subsection{subsection1}
\subsection{subsection2}
\subsection{subsection3}
\section{section2}
\section{section3}
\end{document}
  • I wanted to do the same thing, but it didn't work. I was missing the \rlap. Why do you need this here? – nox Jun 18 '18 at 12:07
  • Works really well, Thanks! Could you do the same for the 2 red lines around "page"? – Florian Jun 18 '18 at 14:01
  • @nox \rlap{x} creates a right-expanding box around x that takes no horizontal space ("right-wise overlap"). – Lupino Jun 19 '18 at 13:51
  • @FlorianWaas I edited the example accordingly. – Lupino Jun 19 '18 at 13:58
  • @Lupino I'm aware of that, but why is it needed here and fails to compile otherwise? – nox Jun 19 '18 at 14:55

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.