3

I have to use \addtocontents at some places to fully customize the data of some of the lines in my toc (that is, include or not include page numbers, include or not include section / chapter numbers and specify them in some cases where included) and I can't use \addcontentsline since it doesn't allow me to specify chapter or section number. Now, the entries show up in the toc but I can't manage to link them properly, any suggestions?

MWE:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{hyperref}

\begin{document}

\tableofcontents

%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
{\LARGE This is chapter 1}

\section{Section 1-1} % well linked as well
{\LARGE This is chapter 1 section 1}
\clearpage

%======== USING \addcontentsline ========
\phantomsection
\addcontentsline{toc}{chapter}{Chapter 2} % works fine
{\LARGE This is chapter 2}
\clearpage

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========
\phantomsection
\addtocontents{toc}{\protect\contentsline{chapter} % this link does not work
{\protect\numberline{3}Chapter 3}{\thepage}{}}
{\LARGE This is chapter 3}
\clearpage

\phantomsection
\addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{3-1}Section 3-1}{\thepage}{section*.2.4}} % ... neither does this
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

I understand I have to put the link as the last argument to \contentsline but I can't get it to work... I have tried with:

chapter*.\thechapter
chapter*.\thepage
section*.\thesection

... etcetera... I can make it work with \page.<PAGENO> but I don't think that's a very nice solution... Also, I can't have chapters or sections like \section*{}since I actually don't even want a section header or anything at the desired places...

  • Actually you could use \addcontentsline -- but the whole non-chapter/section approach is a little bit weird – user31729 Jul 2 '15 at 12:50
  • Why not just use \renewcommand{\thesection}{\thechapter-\alph{section}} ? – John Kormylo Jul 2 '15 at 13:06
  • @JohnKormylo: Too easy ;-) And the page numbers are still there in ToC, but the OP does not want page numbers (some times) – user31729 Jul 2 '15 at 13:10
4

The first step is to replace \phantomsection with \refstepcounter{chapter} or \refstepcounter{section}. Then you will have actual chapter and section numbers to link to instead of chapter*.1 etc.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{hyperref}

\begin{document}

\tableofcontents

%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
{\LARGE This is chapter 1}

\section{Section 1-1} % well linked as well
{\LARGE This is chapter 1 section 1}
\clearpage

%======== USING \addcontentsline ========
\refstepcounter{chapter}
\addcontentsline{toc}{chapter}{Chapter 2} % works fine
{\LARGE This is chapter 2}
\clearpage

\refstepcounter{section}
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========
\refstepcounter{chapter}
\addtocontents{toc}{\protect\contentsline{chapter}% this link does not work
{\protect\numberline{\thechapter}Chapter \thechapter}{}{chapter.\thechapter}}
{\LARGE This is chapter 3}
\clearpage

\refstepcounter{section}
\addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{\thechapter-\arabic{section}}Section -1}{}{section.\thesection}}% ... neither does this
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}
  • Yes, the crucial point is the \refstepcounter command! – user31729 Jul 2 '15 at 14:06
  • @John Kormylo Thank you! simple and straightforward. I had some problems at first to get it to work in my actual thesis but it turned out that the package option hypertexnames was set to false in my import of hyperref which made it not work and after removing that, it works like a charm! Some of these mechanisms are hard to understand but I guess an in-dept analysis of the hyperref package is OT here :) Thank you! – fast-reflexes Jul 3 '15 at 9:54
2
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{hyperref}





\begin{document}

\tableofcontents


%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
{\LARGE This is chapter 1}

\section{Section 1-1} % well linked as well
{\LARGE This is chapter 1 section 1}
\clearpage

%======== USING \addcontentsline ========
\phantomsection
\addcontentsline{toc}{chapter}{Chapter 2} % works fine
{\LARGE This is chapter 2}
\clearpage

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========
\phantomsection
\addcontentsline{toc}{chapter}{\numberline{3}Chapter}

{\LARGE This is chapter 3}
\clearpage

\phantomsection
\addcontentsline{toc}{section}{\protect\numberline{3-1}Section 3-1}
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

Edit Some improved version with \chapternohead and \sectionnohead commands. The starred variants of that commands omit the page number. I'll update with a version having the possibility to use other numbering.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{hyperref}


\makeatletter

\NewDocumentCommand{\chapternohead}{som}{%
  \refstepcounter{chapter}%
  \typeout{Chapter is \thechapter~#3}
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #2}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #2}{\thepage}{chapter.\theHchapter}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #3}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{\numberline{\thechapter} #3}{\thepage}{chapter.\theHchapter}}
    }%
  } %
  {\huge \textbf{#3}}%
}

\NewDocumentCommand{\sectionnohead}{som}{%
  \refstepcounter{section}%
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #2}{}{section.\theHsection}}
    }{%
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #2}{\thepage}{section.\theHsection}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #3}{}{section.\theHsection}}
      }{%
        \addtocontents{toc}{\protect\contentsline{section}{\numberline{\thesection} #3}{\thepage}{section.\theHsection}}
      }%
    }%
{\LARGE \textbf{#3}}%
}

\makeatother


\begin{document}

\tableofcontents


%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
\section{Section 1-1} % well linked as well

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========

%\phantomsection
\chapternohead[This goes to toc]{Chapter}

{\LARGE This is chapter 3}
\clearpage

\sectionnohead{Section 3-1}
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

Some improvements: having a 4th arguments for the section/chapter number

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{tocloft}
\usepackage{xparse}
\usepackage{hyperref}

\newlength\chapternoheadnameskip
\setlength{\chapternoheadnameskip}{0.2em}

\newlength\sectionnoheadnameskip
\setlength{\sectionnoheadnameskip}{0.2em}


\makeatletter

\NewDocumentCommand{\chapternohead}{somD<>{\thechapter}}{%
  \refstepcounter{chapter}%
  \typeout{Chapter is \thechapter~#3}
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#2}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#2}{\thepage}{chapter.\theHchapter}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#3}{}{chapter.\theHchapter}}
    }{%
      \addtocontents{toc}{\protect\contentsline{chapter}{#4\hspace{\chapternoheadnameskip}#3}{\thepage}{chapter.\theHchapter}}
    }%
  } %
  {\huge \textbf{#3}}%
}

\NewDocumentCommand{\sectionnohead}{somD<>{\thechapter-\thesection}}{%
  \refstepcounter{section}%
  \IfValueTF{#2}{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{#4\hspace{\sectionnoheadnameskip}#2}{}{section.\theHsection}}
    }{%
      \addtocontents{toc}{\protect\contentsline{section}{\numberline{#4}\hspace{\sectionnoheadnameskip}#3}{\thepage}{section.\theHsection}}
    }%
  }{%
    \IfBooleanTF{#1}{%
      % omit the page number
      \addtocontents{toc}{\protect\contentsline{section}{#4\hspace{\sectionnoheadnameskip}#2}{}{section.\theHsection}}
      }{%
        \addtocontents{toc}{\protect\contentsline{section}{\numberline{#4}\hspace{\sectionnoheadnameskip}#3}{\thepage}{section.\theHsection}}
      }%
    }%
    {\LARGE \textbf{#3}}%
}

\makeatother


\begin{document}

\tableofcontents


%======== USING REGULAR ENTRIES ========
\chapter{Chapter 1} % properly linked of course
\section{Section 1-1} % well linked as well

\phantomsection
\addcontentsline{toc}{section}{Section 2-1} % link works
{\LARGE This is chapter 2 section 1}
\clearpage

%======== USING \addtocontents ========

\chapternohead[This goes to toc]{Chapter}<\textsl{\huge \textcolor{blue}{\thechapter}}>

{\LARGE This is chapter 3}
\clearpage

\sectionnohead*[Toc entry for section 3-1]{Section 3-1}<\textcolor{violet}{\thechapter-1}>
{\LARGE This is chapter 3 section 1}
\clearpage

\end{document}

enter image description here

  • Could I exclude page numbers with this approach as well, which I so easily can do with \addtocontents approach? – fast-reflexes Jul 2 '15 at 13:00
  • @fast-reflexes: Nope, but your post did not say that you want to hide them. I'll working on it – user31729 Jul 2 '15 at 13:04
  • Sorry, I forgot to mention that... That's why I started using \addtocontents in the first place now I remember... I know it sounds weird but it's for a variety of appendices where I sometimes want page numbers or / and section numbers and want the links to work. Thank you again :) – fast-reflexes Jul 2 '15 at 13:07
  • Thank you! I picked the other answer since it was simpler which is, after all, a parameter to take into account. This works nicely too though! :) What you did in your first example, when is actually your newly defined command used? I fail to understand exactly what it does... – fast-reflexes Jul 3 '15 at 9:57
  • @fast-reflexes: Simpler is not elegant ;-) And if you have to do a lot of this stuff ... the first solution: That's a remainder of a previous version. It's not needed. I will delete that lines. – user31729 Jul 3 '15 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.