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

Code, edit and compile here:
\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}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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 12119 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
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

Code, edit and compile here:
\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}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Thorsten
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

Code, edit and compile here:
\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}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Thank you localghost!
bkarpuz
Post Reply