0

I am trying to include a footer on each page that would link to the table of contents but this footer ("Contents") does not show up on pages where pdf is inserted. It shows up on pages with section though (where pdf is not empty). Does anyone know how to solve this?

\documentclass[a4paper]{book} %11pt b4
%\usepackage{preamble} 
\usepackage{pdfpages}    
\usepackage{fancyhdr}
\usepackage{hyperref}

\fancypagestyle{mainmatter-pages}{% \fancyhf{}% Clear header/footer \renewcommand{\headrulewidth}{0pt}% Remove header rule \renewcommand{\footrulewidth}{0pt}% Remove footer rule \fancyfoot[L]{\hyperref[ToC-first-page]{Contents}} \fancyfoot[C]{\thepage} } \let\oldtableofcontents\tableofcontents \renewcommand{\tableofcontents}{% \cleardoublepage \phantomsection% Place hyperlink marker \label{ToC-first-page}% Set \label for hyperlink \oldtableofcontents } \let\oldmainmatter\mainmatter \renewcommand{\mainmatter}{% \cleardoublepage \oldmainmatter \pagestyle{mainmatter-pages}% }

\begin{document}

% contents % CONTENTS \newpage \frontmatter

\pagenumbering{gobble}
\hypertarget{contents}{}
\tableofcontents

\newpage
\pagenumbering{arabic}
\setcounter{page}{1}

\mainmatter



\section{Section 1} \includepdf[pages=-]{filename1.pdf}

\section{Section 2} \includepdf[pages=-]{filename2.pdf}

\end{document}

This code is based on this question: Reference to the Table of Contents page (link in the bottom of the page).

Bernard
  • 271,350
Clone
  • 533

1 Answers1

1

With the option pagecommand={} you can get the the link in the desired position.

If you want to clear the footer (in this case the page number) you have to define a new page style:

\fancypagestyle{pdfpages-pages}{%
 \fancyhf{}% Clear header/footer
 \renewcommand{\headrulewidth}{0pt}% Remove header rule
 \fancyfoot[L]{\hyperref[ToC-first-page]{Contents}}
}

and call it inside pagecommand:

pagecommand={\thispagestyle{pdfpages-pages}}

MWE

\documentclass[a4paper]{article}
\usepackage{pdfpages}    
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{hyperref}

\fancypagestyle{mainmatter-pages}{% \fancyhf{}% Clear header/footer \renewcommand{\headrulewidth}{0pt}% Remove header rule \fancyfoot[L]{\hyperref[ToC-first-page]{Contents}} \fancyfoot[C]{\thepage} }

\fancypagestyle{pdfpages-pages}{% \fancyhf{}% Clear header/footer \renewcommand{\headrulewidth}{0pt}% Remove header rule \fancyfoot[L]{\hyperref[ToC-first-page]{Contents}} }

\usepackage{etoolbox} \pretocmd{\tableofcontents}{\label{ToC-first-page}}{}{}

\begin{document}

\pagestyle{mainmatter-pages}

\tableofcontents

\section{Section 1} \lipsum[1-4] \includepdf[pages=-,pagecommand={\thispagestyle{pdfpages-pages}}]{example-image-a.pdf}

\section{Section 1} \lipsum[1-4] \includepdf[pages=-,pagecommand={\thispagestyle{pdfpages-pages}}]{example-image-a.pdf}

\end{document}

Output

enter image description here

Ivan
  • 4,368
  • Perfect, thanks! Do oyu happen to know how to make the section also be on the same page so that it does not create an empty page and then insert the pdf on the new page? – Clone Apr 06 '21 at 21:37
  • 1
    Use \includegraphics in place of \includepdf – Ivan Apr 06 '21 at 21:40