# LaTeX contents page: hiding specific page numbers automatically

Is it possible to have a contents page in LaTeX that doesn't show a page number of a particular subsection, if the one immediately before it shares the same page number?

e.g. If there are 3 subsections on the same page, is it possible in the contents page to show the page number only at the 1st line?

I'm also using the titletoc package in a way similar to the following:

\documentclass{article}

\usepackage{titletoc}

\begin{document}

\tableofcontents

\section{FIRST}
\subsection{FIRST first}
\subsection{FIRST third}
\newpage
\subsubsection{FIRST third b}
\section{SECOND}
\subsection{SECOND first}

\end{document}

It doesn't include dots and shows the page numbers to the left of the section/subsection/subsubsection numbers.

-
The \dotfills should be hidden too? –  tohecz May 24 '12 at 15:28
@tohecz Ideally. But I guess if there is a way of doing it without hiding \dotfill then surely in the same way we can hide \dotfill? –  hwhm May 24 '12 at 15:37
Ok, I posted the second solution for the titlesec package. –  tohecz May 24 '12 at 17:30

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}

% START HERE
\makeatletter
\AtBeginDocument{
\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}
-
Thanks a lot. Where would you suggest as a first place to learn the syntax of the codes you have used? –  hwhm May 24 '12 at 17:46
@hwhm It is not easy to say how to start. These are quite low-level changes, and with TeX it is like with any other programming languages: you have to learn by small steps. I have never used any manual for these commands except this online command catalogue. –  tohecz May 24 '12 at 17:57

For the solution, use the following well-commented script. You obviously need the part starting by \makeatletter and ending \makeatother.

\documentclass{article}

\makeatletter
% replace 'section' by 'chapter' in the 'report' class (on the following 4 lines)
\let\old@l@section\l@section
\def\l@section#1#2{%
\def\l@lastnumber{#2}%
\old@l@section{#1}{#2}% store the page number for section as well
}
\let\old@dottedtocline\@dottedtocline
\def\@dottedtocline#1#2#3#4#5{%
\begingroup% we modify \leaders in some cases
\ifx\l@thisnumber\l@lastnumber% if the page number is the same...
\let\leaders\@gobbletwo% suppress dots on this line
\let\l@thisnumber\relax% suppress page number on this line
\fi%
\old@dottedtocline{#1}{#2}{#3}{#4}{\l@thisnumber}% the original macro
\endgroup%
\def\l@lastnumber{#5}% store the page number
}
% reset the stored value at the end of the ToC/LoT/LoF
\makeatother

\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}

\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}
\begin{table}This is a table.\caption{Table caption}\end{table}

\end{document}
-
Nice, but I'm also use the titletoc package defining styles with \titlecontents. This seems to interfere with your code...? –  hwhm May 24 '12 at 16:14
It will surely interfere with my code. Btw, very nice example of a situtaion where would minimal working example save a lot of time with incorporating the answer. I'll look into that, but please edit your question and add a fully compilable document that contains the way how you use titletoc. –  tohecz May 24 '12 at 16:17
{"extraction_info": {"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8380811810493469, "perplexity": 4533.238345878347}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2014-49/segments/1416400380638.30/warc/CC-MAIN-20141119123300-00069-ip-10-235-23-156.ec2.internal.warc.gz"}