0

This is an extention of How to jump into any page in an article

I want to make an anchor on each page number with which clicking the page number will jump to the related page.

But the following MWE show that all anchors are in wrong positions(please see the attached figure).

Anyone have proposal or solution of this issue?

MWE:

\documentclass{article}
\usepackage{calc,ifthen,eso-pic,picture,xparse,lastpage,refcount,tikz,hyperref}
\AtBeginDocument{
 \newcounter{totalpage}
 \setcounter{totalpage}{\getpagerefnumber{LastPage}}
}
\begin{document}
\AddToShipoutPictureBG{\AtPageCenter{%
   \put(0.5\paperwidth-0.5\marginparwidth,0.5\textheight) {%
      \scalebox{0.7} {%
        \begin{tikzpicture}[baseline=(current bounding box.north)]%
          \foreach \i in {1,...,\thetotalpage}{%
            \ifnum\value{page}=\i\relax%
               \tikzset{pn/.style={fill=red,font=\bfseries}}
            \else%
               \tikzset{pn/.style={fill=gray,opacity=0.5}}%
            \fi%
            \hyperlink{page.\i}{%   
            \path[radius=0.5cm,scale=1,text=white,pn](0,-\i) circle node[scale=1.5]{\i};
          }%\hyperlink  
          }%\foreach
        \end{tikzpicture}%
      }%\scalebox
    }%put
}}
first page
\clearpage
second page
\clearpage
third page    
\end{document} 

enter image description here

lyl
  • 2,727
  • 1
    I had a similar issue the other day and the answers to this question here helped me out: https://tex.stackexchange.com/questions/36109/making-tikz-nodes-hyperlinkable – Markus G. Jun 05 '21 at 10:17
  • 1
    Essentially, put the hyperlink inside the node, instead of the node within the hyperlink. – Torbjørn T. Jun 05 '21 at 10:18
  • I tried this: node[scale=1.5]{\hyperlink{page.\i}{\i}} in my code. It does work, but only on the number. How to make the anchor on all the circle area? Would you please show me a MWE – lyl Jun 05 '21 at 10:28
  • https://tex.stackexchange.com/a/600053/113546 – Pieter van Oostrum Jun 05 '21 at 21:15

1 Answers1

2

Try this code inspired from https://tex.stackexchange.com/a/36111/161015

There's a new TikZ style called hyperlink node=<target> that takes a hypertarget reference. It works by measuring the node it is supplied to, and then placing a new invisible node on top of that. The new node has the content \hyperlink{<target>}{\phantom{\rule{<width of node>}{<height of node>}}, so it has the same size as the original node, but the whole area is clickable.

w

\documentclass{article}

\usepackage[hidelinks]{hyperref} \usepackage{tikz} \usetikzlibrary{calc} \usepackage{eso-pic} \usepackage{lastpage}

\AtBeginDocument{% \newcounter{totalpage} \setcounter{totalpage}{\getpagerefnumber{LastPage}} }

\begin{document} \tikzset{%https://tex.stackexchange.com/a/36111/161015 hyperlink node/.style={% alias=sourcenode, append after command={% let \p1 = (sourcenode.north west), \p2=(sourcenode.south east), \n1={\x2-\x1}, \n2={\y1-\y2} in node [inner sep=0pt, outer sep=0pt,anchor=north west,at=(\p1)] {\hyperlink{#1}{\phantom{\rule{\n1}{\n2}}}}
} }, NOlink/.style={circle, draw, fill=gray!30, very thick, minimum size=7mm}, LINKED/.style={circle, draw, fill=green!40, very thick, minimum size=7mm,font=\bfseries, hyperlink node=\i}, }

\AddToShipoutPictureBG{\AtPageCenter{%
        \put(0.5\paperwidth-1.5\marginparwidth,0.5\textheight){%
                \scalebox{0.7} {%
                    \parbox{7mm}{%
                    \foreach \i in {1,...,\thetotalpage}{%
                        \ifnum\value{page}=\i%
                    \tikz{\node [NOlink] {\i};\node[] () [below =8pt] {};}  %plus node spacer               
                        \else%
                    \tikz{\node [LINKED] {\i};\node[] () [below =8pt] {};}
                        \fi%            
                    }%\foreach
            }       
            }%\scalebox
        }%put
    \AtPageCenter{\hypertarget{\thepage}{}}
}

}

first page
\clearpage
second page \clearpage
third page
\end{document}

Simon Dispa
  • 39,141