General(pdfpages) How to write code that only affects some select pages of a PDF document

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
cypherpunks
Posts: 15
Joined: Sat Dec 10, 2016 12:26 pm

(pdfpages) How to write code that only affects some select pages of a PDF document

Post by cypherpunks »

Suppose you feed a multi-page PDF into `\includepdf`. If you only want `pagecommand` or `picturecommand` to take effect on *some* pages, you normally must split the construct up into multiple invocations. E.g.

Code: Select all

\includepdf[pages=1], pagecommand={\doStuffOnPageOne}]{file.pdf}
\includepdf[pages=2-], pagecommand={\doStuffOnPagesAfterOne}]{file.pdf}
It gets ugly fast when there are some commands you want performed on *every* page, and some on select pages, because then you must write and maintain redundant code.

Fuck that. So here’s a demonstration of how to write code inside `pagecommand` and `picturecommand` that is page-specific:

Code: Select all

\documentclass{article}

% Demonstrates use of the pdfpages package with page-by-page actions that are specific to select pages.

\usepackage{mwe}        % furnishes example-image-a4-numbered.pdf
\usepackage{pdfpages}
\usepackage{pdfcomment}
\usepackage{ocgx2}      % furnishes the ocg environment (PDF layers)
\usepackage{fancybox}   % furnishes oval box
\usepackage{fontawesome} % furnishes \faWarning

\begin{document}

\makeatletter
\includepdf[pages=1-,pagecommand={%
  % \makeatletter ← ⚠ does not work in this scope; must wrap the whole includepdf construct
  \pdfcomment[icon=Note, hoffset=0.5\textwidth, voffset=5em]{%
    (inside pagecommand, executing on every page)\textLF\textLF
    \texttt{\textbackslash AM@page} variable: \AM@page\textLF\textLF
    Side-note: the voffset option has no effect when the value is positive (5em in this case)% 
  }%
  \ifthenelse{\AM@page = 1 \OR \AM@page = 2 \OR \AM@page = 12}{%
    \pdfcomment[icon=Insert, hoffset=0.5\textwidth, voffset=-6em]{%
      (inside pagecommand, affecting only on pages 1, 2, and 12)\textLF\textLF
      \texttt{\textbackslash AM@page} variable: \AM@page\textLF\textLF
      Strangely, the voffset option only works if it is negative.% 
    }%
  }{}
  % \makeatother
}, picturecommand={%
  \put(50,715){Inside the picture environment:}
  \put(50,700){%
    \begin{tabular}[t]{llp{0.6\textwidth}}
      internal \texttt{\textbackslash @tempcnta}    variable (useless):     &\the\@tempcnta&\\
      internal \texttt{\textbackslash @tempcntb}    variable (useless):     &\the\@tempcntb&\\
      internal \texttt{\textbackslash AM@pagecnt}   variable (useless):     &\the\AM@pagecnt&\\
      internal \texttt{\textbackslash AM@abs@page}  variable (useless):     &\AM@abs@page&\\
      internal \texttt{\textbackslash AM@page}      variable (interesting): &\AM@page & \faWarning Inside picturecommand, this number is 1 higher than the actual page number! But it’s correct inside pagecommand (see the annotation note to check).\\
      internal \texttt{\textbackslash AM@pagecount} variable (interesting): &\AM@pagecount&\\%
    \end{tabular}
    % lastpage: \AM@lastpage% broken
    \ifAM@firstpage
    We might expect this to trigger on the 1st page, but it never does.
    \fi
  }
  \put(500,770){%
    \begin{ocg}{section labels}{sl1}{on}\color{blue}
      \Large\rotatebox{-45}{\setlength{\fboxsep}{6pt}\Ovalbox{Section~A}}
    \end{ocg}}}]%
{example-image-a4-numbered.pdf}
\makeatother
\end{document}
It’s interesting to note that in the investigation of how to do this I discovered the “voffset” option to pdfcomment fails to have effect when given a positive dimension. Apparently this is a bug. It’s possibly overcomable by using \raisebox or \vspace... I did not try. Just mentioning it here so the bug is documented somewhere.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply