Hi I'm trying to create a document in LaTeX with a few small subsections on the same page, is there any way to remove the repeated page numbers and keep the numbers when I move onto a new page?
Table of Contents – Removing Repeated Page Numbers from Table of Contents
table of contents
Related Solutions
Solution for the use with titletoc
. Self-commented code, you have to put the code block after \usepackage{titletoc}
but before \begin{document}
.
\documentclass{article}
\usepackage{titletoc}
\titlecontents{section}[0pt]{\thecontentspage \quad \thecontentslabel \quad}{}{}{}
\titlecontents{subsection}[0pt]{\thecontentspage \quad \thecontentslabel \quad}{}{}{}
\titlecontents{subsubsection}[0pt]{\thecontentspage \quad \thecontentslabel \quad}{}{}{}
% START HERE
\makeatletter
\AtBeginDocument{
\g@addto@macro{\tableofcontents}{\let\l@lastnumber\relax}
\let\old@ttl@tocentry\ttl@tocentry
\def\ttl@tocentry#1#2#3#4#5#6#7#8{%
\edef\l@thisnumber{#8}% store current page number
\old@ttl@tocentry{#1}{#2}{#3}{#4}{#5}{#6}{#7}{%
\ifnum\ttl@b>1\relax% the value here determines which levels will ALWAYS have page numbers
\ifx\l@thisnumber\l@lastnumber% if the numbers are the same
\leavevmode\phantom{\l@thisnumber}%
\else% if the numbers differ
\l@thisnumber
\fi
\else% if the level has to have the page number always
\l@thisnumber
\fi}%
\edef\l@lastnumber{#8}% save the page number for the next ToC line
}
}
\makeatother
% END HERE
\begin{document}
\tableofcontents
\listoftables
\section{FIRST}
\subsection{FIRST first}
\subsection{FIRST second}
\subsection{FIRST third}
\subsubsection{FIRST third a}
\newpage
\subsubsection{FIRST third b}
\subsubsection{FIRST third c}
\subsection{FIRST fourth}
\section{SECOND}
\subsection{SECOND first}
\end{document}
Your minimal example produces a ToC that looks like this (in code form):
\contentsline {section}{\numberline {1}\relax $\@@underline {\hbox {The Client}}\mathsurround \z@ $\relax }{2}{section.1}
\contentsline {subsection}{\numberline {1.1}Sub section 1}{2}{subsection.1.1}
\contentsline {section}{\numberline {2}\relax $\@@underline {\hbox {Scope}}\mathsurround \z@ $\relax }{2}{section.2}
\contentsline {subsection}{\numberline {2.1}Sub Section 2}{2}{subsection.2.1}
Note that each of the \contentsline
entries for a \section
contains \@@underline
. A crude solution would be to redefine \@@underline
to be a "no-op" (do nothing) when processing \tableofcontents
:
{\makeatletter
\def\@@underline#1{#1}
\tableofcontents
\makeatother}
If only some of your sections require this "special treatment", then manual underlining is most likely the way to go, together with the correction above, or by using the optional argument for sections:
\section[The Client]{\underline{The Client}}
However, for more far-reaching or consistent requirement across your entire document, consider using a package dedicated to sectional titles. For example, sectsty
provides an example in its documentation doing exactly this:
\documentclass{article}
\usepackage{sectsty}% http://ctan.org/pkg/secsty
\usepackage[normalem]{ulem}% http://ctan.org/pkg/ulem
\usepackage{xcolor,hyperref}% http://ctan.org/pkg/{xcolor,hyperref}
\hypersetup{
colorlinks=true, %set true if you want colored links
linktoc=all, %set to all if you want both sections and subsections linked
linkcolor=black, %color for links
}
\begin{document}
\tableofcontents
\newpage
\sectionfont{\underline}
\section{The Client}
\subsection{Sub section 1}
\section{Scope}
\subsection{Sub Section 2}
\end{document}
Note the difference in the underlining between the choice of sectsty
and that of a manual underline.
Best Answer
Although I've no programming skills, it seems obvious that this possible; e.g. by adding a hook before each line of the toc: "if the pagenumer is the same as in the last line, print dots and number in
\color{white}
".There are some packages on CTAN regarding the table of contents. Do you know the "Topic Index"? Have a look on the packages about the table of contents there.
The closest candidate seems to be the package
tocloft
. If you've got a full installation, you might get the manual by typingtexdoc tocloft
on the command line. Maybe you can use\cftXleader
or something similar.However, your readers will miss the numbers and ask themselves (they can not ask you, can they? they are reading in a book!), whether it's a typo or not. So take my advice: print the 'toc' like all others do . . .