如何在页码上创建锚点

如何在页码上创建锚点

这是如何跳转到文章中的任意页面

我想在每个页码上创建一个锚点,单击页码就会跳转到相关页面。

但以下 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} 

在此处输入图片描述

答案1

尝试一下这个代码,灵感来自https://tex.stackexchange.com/a/36111/161015

有一种新的 TikZ 样式称为 ,hyperlink node=<target>它采用超目标引用。它的工作原理是测量它所提供给的节点,然后在其上放置一个新的不可见节点。新节点具有内容\hyperlink{<target>}{\phantom{\rule{<width of node>}{<height of node>}},因此它的大小与原始节点相同,但整个区域都是可点击的。

瓦

\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} 

相关内容