8

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}
\titlecontents{section}[0pt]{\thecontentspage\quad\thecontentslabel\quad}{}{}{}
\titlecontents{subsection}[0pt]{\thecontentspage\quad\thecontentslabel\quad}{}{}{}
\titlecontents{subsubsection}[0pt]{\thecontentspage\quad\thecontentslabel\quad}{}{}{}

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

4
  • The \dotfills should be hidden too?
    – yo'
    May 24, 2012 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, 2012 at 15:37
  • Have you read this page I linked in the comment to my answer?
    – yo'
    May 24, 2012 at 16:31
  • Ok, I posted the second solution for the titlesec package.
    – yo'
    May 24, 2012 at 17:30

2 Answers 2

4

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}
2
  • Thanks a lot. Where would you suggest as a first place to learn the syntax of the codes you have used?
    – hwhm
    May 24, 2012 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.
    – yo'
    May 24, 2012 at 17:57
5

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
  \def\l@thisnumber{#5}% store this page number
  \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
\g@addto@macro{\tableofcontents}{\let\l@lastnumber\relax}
\g@addto@macro{\listoftables}{\let\l@lastnumber\relax}
\g@addto@macro{\listoffigures}{\let\l@lastnumber\relax}
\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}
2
  • Nice, but I'm also use the titletoc package defining styles with \titlecontents. This seems to interfere with your code...?
    – hwhm
    May 24, 2012 at 16:14
  • 3
    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.
    – yo'
    May 24, 2012 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .