GeneralAdd Section Heading to ToC without Page Number

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bkarpuz
Posts: 124
Joined: Thu Dec 18, 2008 4:53 pm

Add Section Heading to ToC without Page Number

Post by bkarpuz »

Dear LC members,

I want to add to TOC a section without a section number and a page number.

I have the following MWE.

Code: Select all

\documentclass{article}

%Add dots for Sections in TOC
\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\
      \leaders\hbox{$\m@th\mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}
     \hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\makeatother

\def\contentsname{\centering Contents}

\begin{document}

\addtocontents{toc}{\hfill\textbf{Page}\par}

%\clearpage\phantomsection
\addcontentsline{toc}{section}{Contents}
\tableofcontents

\newpage
\section{First Section}

\newpage
\section{Second Section}

\newpage
\subsection{First Subsection of the Second Section}

\newpage
\subsection{Second Subsection of the Second Section}

\newpage
\section{Third Section}

\newpage
\pagestyle{empty}
\section*{Section without Page Number}
\addcontentsline{toc}{section}{Section without Page Number}

\end{document}
I don't want the page number 7 appearing at the end of the last item of TOC. I would be very glad if you can help me in this regard.

Thank you very much.
bkarpuz
Attachments
tocnopp.JPG
tocnopp.JPG (27.22 KiB) Viewed 11655 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Add Section Heading to ToC without Page Number

Post by localghost »

The magic command is \addtocontents{toc}{…} to get what you are after.

Code: Select all

\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{tocloft}   % customize ToC, LoF and LoT
\usepackage{blindtext}

\renewcommand{\cftsecdotsep}{\cftdotsep}   % customize leaders after secton entries (tocloft)

\begin{document}
  \tableofcontents

  \bigskip
  \blinddocument

  \section*{Foo}
  \addtocontents{toc}{\protect\textbf{Foo}}
\end{document}

Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
bkarpuz
Posts: 124
Joined: Thu Dec 18, 2008 4:53 pm

Add Section Heading to ToC without Page Number

Post by bkarpuz »

bkarpuz wrote:Dear LC members,

I want to add to TOC a section without a section number and a page number.

I have the following MWE.
...
I don't want the page number 7 appearing at the end of the last item of TOC. I would be very glad if you can help me in this regard.

Thank you very much.
bkarpuz
Here is the solution.

Code: Select all

\documentclass{article}

\usepackage{hyperref}
\usepackage{tocloft}

%Add dots for Sections in TOC
\renewcommand{\cftsecdotsep}{\cftdotsep}

\def\contentsname{\centering Contents}

\begin{document}

\addtocontents{toc}{\hfill\textbf{Page}\par}

%\clearpage\phantomsection
\addcontentsline{toc}{section}{Contents}
\tableofcontents

\newpage
\section{First Section}

\newpage
\section{Second Section}

\newpage
\subsection{First Subsection of the Second Section}

\newpage
\subsection{Second Subsection of the Second Section}

\newpage
\section{Third Section}

\newpage
\pagestyle{empty}
\section*{Section without Page Number}\label{swpn}
\addtocontents{toc}{\protect\addvspace{10pt}}
\addtocontents{toc}{\noindent\hyperref[swpn]{\textbf{Section without Page Number}}}

\end{document}
Thank you localghost!
bkarpuz
Post Reply