0

I was able to build a table of contents that directs me to each chapter, as in this example: toc with hyperlinks. My code:

\documentclass{book}
\usepackage[left=1.5cm,right=1.5cm]{geometry}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}    
\usepackage[table]{xcolor}
\usepackage{booktabs,newtxtext,newtxmath,multirow}
\usepackage{ltablex}
\usepackage{sectsty}
\chapterfont{\color{darkblue2}}
\renewcommand{\thepage}{\textcolor{darkblue2}{\arabic{page}}}
\usepackage{hyperref}
\hypersetup{colorlinks=,linkcolor=darkblue2}
\pagecolor{white}
\usepackage{pdfpages,graphics}
\definecolor{lilita}    {RGB}{215,205,247}
\definecolor{darkblue2} {rgb}{0,0,0.7}
\pagestyle{plain}
\begin{document}
\includepdf[pages=2]{portadas}
\pagecolor{lilita}
{\makeatletter
 \let\@oldstarttoc\@starttoc
 \renewcommand{\@starttoc}{%
 \addcontentsline{toc}{chapter}{\protect\numberline{}\contentsname}
 \@oldstarttoc}
\tableofcontents
\makeatother}
\includepdf[pages=4]{portadas}
\include{i}
\includepdf[pages=5]{portadas}
\include{u}
\end{document}

in file 'i.tex' and 'u.tex' I put

\phantomsection 
\addcontentsline{toc}{chapter}{I}

to generate the "entrance" in toc. The hyperlink takes me, correctly, to the page where each chapter begins. Each chapter has as first page an image which I generated elsewhere and is included in the file: portadas. What I would like to obtain is that the hyperlink takes me to this image instead of "page 2" of each chapter. Is it possible?

  • \clearpage \phantomsection \addcontentsline{toc}{chapter}{I} \includepdf[pages=1]{portadas} should give you a toc entry that points to the top of the included pdf. – leandriis Jun 16 '18 at 7:21
  • While you're waiting for an answer on this question, you might also want to go back to your old questions an since you have some responses there that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – leandriis Jun 16 '18 at 7:26
  • Not working. The hyperlink keeps directing me to "page 2". Also, I marked the previous posts. Thanks – Antonio Ortiz Jun 16 '18 at 12:21
2

The following MWE demonstrates the different output that can be obtained by a different position of the \addcontentsline command in the code.

In the first example, the command is placed before \includepdf and the hyperlink in the table of contents correctly leads to the page with the included pdf.

In the second example, the \addcontentsline command is placed after the \includepdf command and thus the hyperlink in the toc leads to the page after the included pdf.

\documentclass{book}
\usepackage[left=1.5cm,right=1.5cm]{geometry}
\usepackage[spanish]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}    

\usepackage{pdfpages}
\usepackage{hyperref}

\begin{document}

\tableofcontents

\clearpage 
\phantomsection 
\addcontentsline{toc}{chapter}{This takes me to the included pdf}
\includepdf[pages=4]{portadas}

%%%%%%%%%%% The following could also be in 'i.tex'%%%%%
Some text that represents the contents of i.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



\includepdf[pages=5]{portadas}

%%%%%%%%%%% The following could also be in 'u.tex'%%%%%
\phantomsection 
\addcontentsline{toc}{chapter}{This takes me to the page after the inserted pdf image.}
Some text that represents the contents of u.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
  • Yes, it was a matter of order. I tested the code with the actual book and is working perfectly! Thank you! :-) – Antonio Ortiz Jun 17 '18 at 17:47

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.