# How to jump into any page in an article

I want to make a macro by which I can jump into any page I want when clicking a designated object, like this:

\gotopage{3}{object selected} %then goto the third page

The object may be text, graphic etc.

And I don't like putting a label at each page especially when there are many pages in an article.

How can I do this?

Here is an MWE and related figure to show my idea:

\documentclass{article}
\usepackage{calc,ifthen,eso-pic,picture,xparse,lastpage,refcount,tikz}
\AtBeginDocument{
\newcounter{totalpage}
\setcounter{totalpage}{\getpagerefnumber{LastPage}}
}
\begin{document}
\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%
}%\foreach
\end{tikzpicture}%
}%\scalebox
}%put
}}
first page
\clearpage
second page
\clearpage
third page
\end{document}


UPDATED MWE:

\documentclass{article}
\usepackage{calc,ifthen,eso-pic,picture,xparse,lastpage,refcount,tikz,hyperref}
\AtBeginDocument{
\newcounter{totalpage}
\setcounter{totalpage}{\getpagerefnumber{LastPage}}
}
\begin{document}
\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%
}%\foreach
\end{tikzpicture}%
}%\scalebox
}%put
}}
first page
\clearpage
second page
\clearpage
third page
\end{document}


• While I was attempting to solve your problem, got stuck at the application level. Can you please create a complete document showing some use cases of the \goto macro? You leave \goto undefined. – Masroor Jun 5 at 3:20
• How do you know where you're trying to go to if you don't have some way of identifying the target page? – Don Hosek Jun 5 at 4:03
• @Masroor @ Don Hosek The initial idea is for an article with not many pages. Thus all page numbers can be list on every page. Then you can click one of the listed page number to directly go there. I updated my question to give more explanation. – lyl Jun 5 at 4:32

hyperref creates by default destinations (anchors) with the name page.<number> for every page:

\documentclass{article}

\usepackage{hyperref}
\begin{document}
page 1
\newpage
page 2
\newpage

page 3

\newpage \pagenumbering{roman}

roman i

\end{document}


• Many thanks for this perfect solution!! And, I updated my MWE with your instruction, then anchors were created, but not on the page numbers(please see the attached figur2). Would you have any proposal on this? – lyl Jun 5 at 9:36
• that is a new question, so better ask a new question. It only gets confusing if questions change the focus. – Ulrike Fischer Jun 5 at 9:41
• In this way, the format of \thepage has to be known in advance. That is to say, we must know whether the page number of a certain page is arabic or roman. How can I know the format of the page number of a page in a large article with many pages? – lyl Jul 8 at 13:18

My solution puts the link on an empty box stacked on top of the circles numbers.

\documentclass{article}
\usepackage{calc,ifthen,eso-pic,picture,xparse,lastpage,refcount,tikz}
\usepackage{hyperref}

\AtBeginDocument{
\newcounter{totalpage}
\setcounter{totalpage}{\getpagerefnumber{LastPage}}
}
\begin{document}
\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%
circle node[scale=1.5]{\refbox{\i}\i};
}%\foreach
\end{tikzpicture}%
}%\scalebox
}%put
}}
first page
\clearpage
second page
\clearpage
third page
\end{document}


Working fine except that the numbers at right are not stacked.

\documentclass{article}
\usepackage{calc,ifthen,eso-pic,picture,xparse,lastpage,refcount,tikz}
\AtBeginDocument{
\newcounter{totalpage}
\setcounter{totalpage}{\getpagerefnumber{LastPage}}
}

% For cross referencing
\usepackage{hyperref}

% Arrange labeling every page
% \usepackage{everypage}
% Comment out the followind and uncomment the above two lines if you
% get, ERROR: Undefined control sequence.  --- TeX said --- l.12

% Dummy text
\usepackage{blindtext}

\begin{document}

\put(0.5\paperwidth-0.5\marginparwidth,0.5\textheight) {%
\scalebox{0.7} {%
\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%
\hyperref[pagelabel\i]{\begin{tikzpicture}[baseline=(current bounding box.north)]%
\end{tikzpicture}}\\}%
}%\foreach
}%\scalebox
}%put
}

first page
\clearpage

second page

\clearpage
third page

\Blindtext
\Blindtext

\end{document}

• Than you! I tried your code and get this: ERROR: Undefined control sequence. --- TeX said --- l.12 \AddToHook {shipout/background}{\phantomsection\label{pagelabel\thepage}} . Why? – lyl Jun 5 at 10:15
• @lyl, perhaps your LaTeX is not updated. Please use the everypage package. See around line 13 of my code on how to do this. – Masroor Jun 5 at 11:39