1

Here's what I am trying to do:

I have a large scanned pdf file and I want to make an index page so that I can jump to a given page by clicking on a link. So far, I managed to do that, but I would like to print the page number as well, so that someone with a physical copy can locate a given page. Since there are many entries, I was thinking about somethig like this:

  1. Entry 1 (p. xx)
  2. Entry 2 (p. yy)
  3. Entry 3 (p. zz)

and so on.

I also want to add a red box around the entries so that they show on the pdf file but not on a printed copy. I don't need TOC for the pdf file. Is it possible to do that?

Main code:

\documentclass[12pt,a4paper]{article}
\input{packs.tex}
\title{\Large Fancy title}
\author{\Large Me}
\date{\Large \today}
\begin{document}
\maketitle
Here are some entries...
\begin{multicols}{2}
\begin{foo}[nosep]
\hyperlink{page.2}{Entry 1}
\hyperlink{page.4}{Entry 2}
\hyperlink{page.6}{Entry 3}
\end{foo}
\end{multicols}
\includepdf[pages=-,pagecommand={\thispagestyle{plain}}]{dummy.pdf}
\end{document}

packs.tex:

\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{pdfpages}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\makeatletter
\newcommand{\checkForEnd}{\@ifnextchar\end{}{\item}}
\makeatother
\newenvironment{foo}[1][]
 {\par\obeylines
  \begingroup\lccode`~=`\^^M
  \lowercase{\endgroup\let~}\checkForEnd
  \begin{enumerate}[#1]}
 {\end{enumerate}}
\makeatletter
\let\orig@Hy@EveryPageAnchor\Hy@EveryPageAnchor
\def\Hy@EveryPageAnchor{%
    \begingroup
    \hypersetup{pdfview=Fit}%
    \orig@Hy@EveryPageAnchor
    \endgroup
}
\makeatother 
\usepackage{hyperref}

And the code to generate a dummy pdf file:

\documentclass{report}
\usepackage{blindtext}
\pagenumbering{gobble}
\begin{document}
\Blinddocument 
\end{document}

1 Answer 1

2

You can define a new command with the entry text and the page number as argument, and then use the page number both in the \hyperlink command and again to print the number itself in the index.

For the red box that is not printed you can set some hyperref options:

  • colorlinks=false (true means that the link text itself is colored, which is also printed, false means that the border is shown, which is not printed)
  • linkbordercolor={1 0 0} (RGB between 0 and 1, so 1 0 0 is red)
  • pdfborder={0 0 1} (link border box of 1pt width)

These values are the default (so you don't need to set them explicitly) but the defaults may change in future versions of hyperref, see better default colors for hyperref links.

MWE:

\documentclass[12pt,a4paper]{article}
\input{packs.tex}

% explicitly set red link boxes
\hypersetup{colorlinks=false, linkbordercolor={1 0 0}, pdfborder={0 0 1}}

\newcommand{\myindex}[2]{%
\hyperlink{page.#2}{#1} (p. #2)%
}

\title{\Large Fancy title}
\author{\Large Me}
\date{\Large \today}
\begin{document}
\maketitle
Here are some entries...
\begin{multicols}{2}
\begin{foo}[nosep]
\myindex{Entry 1}{2}
\myindex{Entry 2}{4}
\myindex{Entry 3}{6}
\end{foo}
\end{multicols}
\includepdf[pages=-,pagecommand={\thispagestyle{plain}}]{dummy.pdf}
\end{document}

Result:

enter image description here

In case the page number you want to refer to is different from the linked page then you can use an additional optional argument for that. Since the default value of that argument is itself an argument (if #3 is not specified then you want to use #2) you need xparse (see \newcommand: Using one parameter as the default for the other). Of course when the third argument is always different then you can just use the regular \newcommand with three mandatory arguments, i.e., remove the \NewDocumentCommand part and just use \printmyindex directly.

\documentclass[12pt,a4paper]{article}
\input{extindexpre.tex}
\usepackage{xparse}

% explicitly set red link boxes
\hypersetup{colorlinks=false, linkbordercolor={1 0 0}, pdfborder={0 0 1}}

\NewDocumentCommand{\myindex}{m m o}{%
  \IfValueTF{#3}
    {\printmyindex{#2}{#1}{#3}}%
    {\printmyindex{#2}{#1}{#2}}%
}

\newcommand{\printmyindex}[3]{%
\hyperlink{page.#1}{#2} (p. #3)%
}

\title{\Large Fancy title}
\author{\Large Me}
\date{\Large \today}
\begin{document}
\maketitle
Here are some entries...
\begin{multicols}{2}
\begin{foo}[nosep]
\myindex{Entry 1}{2}
\myindex{Entry 2}{4}[5]
\myindex{Entry 3}{6}
\end{foo}
\end{multicols}
\includepdf[pages=-,pagecommand={\thispagestyle{plain}}]{dummy.pdf}
\end{document}

enter image description here

1
  • 1
    With a new enough xparse you can say \NewDocumentCommand{\myindex}{m m O{#2}} and avoid the \If... ;-) (see manual, section 1.4)
    – Rmano
    Commented Jul 9, 2020 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .