# How to link \addtocontents entry from toc to actual chapter / section

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

\phantomsection
{\LARGE This is chapter 2}
\clearpage

\phantomsection
{\LARGE This is chapter 2 section 1}
\clearpage

\phantomsection
{\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

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

\refstepcounter{chapter}
{\LARGE This is chapter 2}
\clearpage

\refstepcounter{section}
{\LARGE This is chapter 2 section 1}
\clearpage

\refstepcounter{chapter}
{\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
\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

\phantomsection
{\LARGE This is chapter 2}
\clearpage

\phantomsection
{\LARGE This is chapter 2 section 1}
\clearpage

\phantomsection

{\LARGE This is chapter 3}
\clearpage

\phantomsection
{\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

\refstepcounter{chapter}%
\typeout{Chapter is \thechapter~#3}
\IfValueTF{#2}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
} %
{\huge \textbf{#3}}%
}

\refstepcounter{section}%
\IfValueTF{#2}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
}%
{\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
{\LARGE This is chapter 2 section 1}
\clearpage

%\phantomsection

{\LARGE This is chapter 3}
\clearpage

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

\makeatletter

\refstepcounter{chapter}%
\typeout{Chapter is \thechapter~#3}
\IfValueTF{#2}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
} %
{\huge \textbf{#3}}%
}

\refstepcounter{section}%
\IfValueTF{#2}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
}{%
\IfBooleanTF{#1}{%
% omit the page number
}{%
}%
}%
{\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
{\LARGE This is chapter 2 section 1}
\clearpage

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


• 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