4

The example below defines a new version of \addcontentsline which does not show \thepage. Unfortunately, the compilation fails when hyperref is introduced. I have no clue why?

\documentclass[11pt,fleqn]{book}
%\usepackage{hyperref}
\makeatletter
\def\addcontentslinepart#1#2#3{\addtocontents{#1}{\protect\contentsline{#2}{#3}{}}}%
\def\@part[#1]#2{%
    \ifnum \c@secnumdepth >-2\relax
      \refstepcounter{part}%
       \addcontentslinepart{toc}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    \markboth{}{}%
\@endpart}
\makeatother
\begin{document}
\tableofcontents
\part{Title}
\chapter{Title}
\end{document}
4
  • \contentsline expects 4 arguments when hyperref is loaded. Check the toc to see how it should look. Jan 31, 2015 at 13:13
  • @UlrikeFischer ohohoh, thank you! Completely overlooked that aspect. You can probably turn this into an answer.
    – pluton
    Jan 31, 2015 at 13:34
  • @pluton You should redefine \@part before loading hyperref.
    – egreg
    Jan 31, 2015 at 13:35
  • @egreg yes you are right. Then hyperref will do the job. Ok.
    – pluton
    Jan 31, 2015 at 13:44

2 Answers 2

3

Not really an answer to your problem (which at least requires redefining \@part before loading hyperref; there's a much simpler way: redefine \l@part to ignore its second argument.

\documentclass[11pt,fleqn]{book}

\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\patchcmd\l@part{#2}{}{}{}
\makeatother

\begin{document}

\tableofcontents

\part{Title}
\chapter{Title}

\end{document}

enter image description here

If you are loading titletoc, the macro to patch is \ttl@savel@part:

\documentclass[11pt,fleqn]{book}

\usepackage{titletoc}
\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\patchcmd\ttl@savel@part{#2}{}{}{}
\makeatother

\begin{document}

\tableofcontents

\part{Title}
\chapter{Title}

\end{document}
5
  • Almost there but fails when titletoc is used. I have to investigate why. The command is probably redefined there.
    – pluton
    Jan 31, 2015 at 14:37
  • @pluton Of course it will fail, but if you don't give essential information in the MWE, we can't know you're using it.
    – egreg
    Jan 31, 2015 at 14:38
  • Well, I am investigating at the same time on the problem that I am facing which is more complicated than the provided MWE and I just noticed that titletoc was messing around.
    – pluton
    Jan 31, 2015 at 14:40
  • @pluton I added the corresponding workaround
    – egreg
    Jan 31, 2015 at 14:41
  • Great thanks. That's what I was about to isolate in titletoc.sty.
    – pluton
    Jan 31, 2015 at 14:43
1
\documentclass[11pt,fleqn]{book}
\usepackage{hyperref}
\makeatletter
\def\addcontentslinepart#1#2#3{\addtocontents{#1}{\protect\contentsline{#2}{#3}{}{\@currentHref}}}%
\def\@part[#1]#2{%
    \ifnum \c@secnumdepth >-2\relax
      \refstepcounter{part}%
       \addcontentslinepart{toc}{part}{\thepart\hspace{1em}#1}%
    \else
      \addcontentsline{toc}{part}{#1}%
    \fi
    \markboth{}{}%
\@endpart}
\makeatother


\begin{document}
\tableofcontents
\part{Title}
\chapter{Title}
\end{document}
1
  • Your solution removes the bookmark at the part level. This is unclear why.
    – pluton
    Jan 31, 2015 at 14:05

You must log in to answer this question.

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