5

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}
\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%
            \path[radius=0.5cm,scale=1,text=white,pn](0,-\i) circle node[scale=1.5]{\i};
          }%\foreach
        \end{tikzpicture}%
      }%\scalebox
    }%put
}}
first page
\clearpage
second page
\clearpage
third page    
\end{document} 

enter image description here

UPDATED 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
  • 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 05 '21 at 03: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 05 '21 at 04: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 05 '21 at 04:32

3 Answers3

8

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

\documentclass{article}

\usepackage{hyperref} \begin{document} \hyperlink{page.1}{page 1} \hyperlink{page.2}{page 2} \hyperlink{page.3}{page 3} \hyperlink{page.i}{page i} page 1 \newpage page 2 \newpage

page 3

\newpage \pagenumbering{roman}

roman i

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • 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 05 '21 at 09:36
  • that is a new question, so better ask a new question. It only gets confusing if questions change the focus. – Ulrike Fischer Jun 05 '21 at 09: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 08 '21 at 13:18
3

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}
\newcommand\refbox[1]{\smash{\rlap{\hspace{-3mm}%
  \hyperlink{page.#1}{\rule[-3mm]{0pt}{9mm}\hspace{9mm}}}}}

\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% \pathradius=0.5cm,scale=1,text=white,pn circle node[scale=1.5]{\refbox{\i}\i}; }%\foreach \end{tikzpicture}% }%\scalebox }%put }} first page \clearpage second page \clearpage third page
\end{document}

image of output

2

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} % \AddEverypageHook{\phantomsection\label{pagelabel\thepage}} % Comment out the followind and uncomment the above two lines if you % get, ERROR: Undefined control sequence. --- TeX said --- l.12 \AddToHook{shipout/background}{\phantomsection\label{pagelabel\thepage}}

% Dummy text \usepackage{blindtext}

\begin{document}

\AddToShipoutPictureBG{\AtPageCenter{% \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)]% \pathradius=0.5cm,scale=1,text=white,pn circle node[scale=1.5]{\i}; \end{tikzpicture}}\}% }%\foreach }%\scalebox }%put }

first page \clearpage

second page

\clearpage third page

\Blindtext \Blindtext

\end{document}

Masroor
  • 17,842
  • 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 05 '21 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 05 '21 at 11:39