4

Is it possible to display the page numbers of included pdf file? Currently I have the following footer/header definition using fancyhdr

%-------------------------------------------------
% Header & Footer
%-------------------------------------------------
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{{\scriptsize}}
\fancyfoot[R]{{\scriptsize\thepage{}/\pageref{LastPage}}}
\fancyheadoffset{0mm}
\fancyfootoffset{0mm}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

and the following include command using pdfpages

\newcommand{\InsertPdfPages}[1]{%
\includepdf[pages={#1},pagecommand={\pagestyle{fancy}}]{./NP2-18.pdf}%
}

But I want to have footer looking something like this for pdf included pages

\fancyfoot[R]{{\scriptsize\thepage{}(\thepdfincludedpage)/\pageref{LastPage}}}

That in the result the footer of included pages looks like this

169(23)/423 % <current page>(<number of pdf included page>)/<total number of pages>

1 Answer 1

3

An example to start:

\documentclass{article}
\usepackage{lastpage}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[R]{{\scriptsize\thepage/\pageref{LastPage}}}
\fancyheadoffset{0mm}
\fancyfootoffset{0mm}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{fancyinsert}{%
  \fancyfoot[R]{{\scriptsize\thepage(\theinsertpage)/\pageref{LastPage}}}%
}

\newcounter{insertpage}
\newcommand{\InsertPdfPages}[1]{%
  \setcounter{insertpage}{0}%
  \includepdf[
    pages={#1},
    pagecommand={%
      \stepcounter{insertpage}%
      \thispagestyle{fancyinsert}%
    },
  ]{NP2-18.pdf}%
}

\begin{document}
  \InsertPdfPages{1-2}
  Last page.
\end{document}

It is not very clear, which kind of page number is the "inserted page":

  • absolute page number of the inserted PDF document,
  • "\thepage" number of the inserted PDF document,
  • absolute page number of the inserted page range.

The example implements the latter interpretation.


The next example provides an example for \thepage and more complicate page ranges:

\documentclass{article}
\usepackage{lastpage}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[R]{{\scriptsize\thepage/\pageref{LastPage}}}
\fancyheadoffset{0mm}
\fancyfootoffset{0mm}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{fancyinsert}{%
  \fancyfoot[R]{{\scriptsize\thepage(\theinsertpage)/\pageref{LastPage}}}%
}

\newcounter{insertpage}
\newcommand{\InsertPdfPages}[1]{%
  \setcounter{insertpage}{0}%
  \includepdf[
    pages={#1},
    pagecommand={%
      \stepcounter{insertpage}%
      \thispagestyle{fancyinsert}%
    },
  ]{NP2-18.pdf}%
}

\begin{document}
  \renewcommand*{\theinsertpage}{%
    \ifcase\value{insertpage}%
      20\or
      21\or
      22\or
      23\or
      24\or
      41\or
      43\or
      44\or
      50\or
      51\or
      52%
    \fi
  }%
  \InsertPdfPages{20-24,41,43,44,50-52}
  Last page.
\end{document}

Or a variant that parses the page list:

\documentclass{article}
\usepackage{lastpage}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[R]{{\scriptsize\thepage/\pageref{LastPage}}}
\fancyheadoffset{0mm}
\fancyfootoffset{0mm}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{fancyinsert}{%
  \fancyfoot[R]{{\scriptsize\thepage(\theinsertpage)/\pageref{LastPage}}}%
}

\newcounter{insertpage}

\usepackage{kvsetkeys}
\makeatletter
\newcommand{\InsertPdfPages}[1]{%
  \comma@parse{#1}{\@InsertPdfPages}%
}
\newcommand*{\@InsertPdfPages}[1]{%
  \@@InsertPdfPages#1-#1-\@nil
}
\def\@@InsertPdfPages#1-#2-#3\@nil{%
  \InsertPdfPageRange{#1}{#2}%
}
\makeatother

\newcommand*{\InsertPdfPageRange}[2]{%
  \setcounter{insertpage}{#1}%
  \addtocounter{insertpage}{-1}%
  \includepdf[
    pages={#1-#2},
    pagecommand={%
      \stepcounter{insertpage}%
      \thispagestyle{fancyinsert}%
    },
  ]{NP2-18.pdf}%
}

\begin{document}
  \InsertPdfPages{20-24,41,43,44,50-52}
  Last page.
\end{document}
6
  • Yes I think I did not clarify that. I want to use \thepage number of the inserted PDF document.
    – itun
    Sep 16, 2014 at 17:58
  • Then you can define \theinsertpage accordingly and set the correct start value in \setcounter{insertpage}{...}. Sep 16, 2014 at 18:12
  • I dont think so. What about a range of pages, something like this page{20,21-24,41,43,44,50-52}.
    – itun
    Sep 16, 2014 at 20:38
  • Perfect, thank you very much, if it is not difficult give me last one tip, how to disable the (\theinsertpage) part for not inserted pages?
    – itun
    Sep 16, 2014 at 23:47
  • @itun Examples fixed by defining a new page style and using \thispagestyle. Sep 16, 2014 at 23:58

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.