目录标签未将超链接指向正确的页面

目录标签未将超链接指向正确的页面

作为一名 LaTeX 新手,我可能引入了一些自定义代码,更新部分格式并调整编号,这可能会影响超引用准确指向正确目标的能力。

简而言之:尽管每个层次结构对象上都有不同的标签,但重复的名称会在目录和所需部分之间的超链接中产生问题:EG

第1部分

A <-> 1 -> 转至第 1 部分的第 1 页

第2部分

A <-> 2 -> 转至第 1 部分的第 1 页

有问题的代码 - 简化为 MWE

\documentclass{report}

\makeatletter
    %=========================================================================================================================================
    % PACKAGES REQUIRED FOR CONTROLLING AND CONFIGURING TABLE OF CONTENTS AND OTHER LISTS
    %=========================================================================================================================================
    \usepackage[titles]{tocloft}
        \setcounter{secnumdepth}{6} 
        \setcounter{tocdepth}{6}
        \renewcommand{\cftchapfont}{\normalsize \scshape}
    \usepackage[titletoc,title]{appendix}
    \usepackage{etoolbox} % or xpatch

    \usepackage{chngcntr}           %http://tex.stackexchange.com/questions/28333/continuous-v-per-chapter-section-numbering-of-figures-tables-and-other-docume
        \counterwithin*{chapter}{part}  %http://tex.stackexchange.com/questions/54383/how-to-reset-chapter-and-section-counter-with-part
        \counterwithin*{section}{part}
        \counterwithin*{section}{chapter}

        \counterwithin*{figure}{part}
        \counterwithout{figure}{chapter}

        \counterwithin*{table}{part}
        \counterwithout{table}{chapter}

    %http://www.latex-community.org/forum/viewtopic.php?f=47&t=4293
    \renewcommand\part{%    
        \if@openright
        \cleardoublepage
        \else
        \clearpage
        \fi
        \thispagestyle{empty}%
        \if@twocolumn
        \onecolumn
        \@tempswatrue
        \else
        \@tempswafalse
        \fi
        \null\vfil
        \secdef\@part\@spart
    }

    %http://tex.stackexchange.com/questions/103926/part-title-in-list-of-figures-and-tables
    % initial definitions of the chapter info (name and number)
    \def\thischaptertitle{}\def\thischapternumber{}
    \newtoggle{noFigs}
    \newtoggle{noTabs}


    \apptocmd{\@part}%
    {\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}%
        \global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

    % the figure environment does the job: the first time it is used after a \chapter command, 
    % it writes the information of the chapter to the LoF
    \AtBeginDocument{%
        \AtBeginEnvironment{figure}{%
            \iftoggle{noFigs}{
                \addtocontents{lof}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{3.2em} }
                \global\togglefalse{noFigs}
            }{}
        }%
        \AtBeginEnvironment{table}{%
            \iftoggle{noTabs}{
                \addtocontents{lot}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{} }
                \global\togglefalse{noTabs}
            }{}
        }%
    }


    %/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    % setup environment to permit the removal of page numbers while still continuing sequence when resumed and keeping hyperref happy   // 
    %/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    %http://tex.stackexchange.com/questions/112782/gobble-page-numbers
    \newcounter{nopage}
    \newenvironment{nopage}
    {\clearpage\stepcounter{nopage}%
        \renewcommand{\thepage}{NP\arabic{nopage}}%
        \thispagestyle{empty}}
    {\clearpage\addtocounter{page}{-1}}

    %_________________________________________________________________________________________________________________________________________

    \newenvironment{paper}{%
        \begin{sloppypar}
            \global\let\oldthesection\thesection
            \renewcommand{\thesection}{\@arabic\c@section}%
        }{%
        \end{sloppypar}
        \global\let\thesection\oldthesection
    }


    %=========================================================================================================================================
    % PACKAGES REQUIRED FOR HYPERLINKS AND HYPER-REFERENCES
    %=========================================================================================================================================
    \usepackage{csquotes}
    \usepackage{url}
    \usepackage[nottoc]{tocbibind}
    \usepackage{bookmark}

    %http://tex.stackexchange.com/questions/42927/converting-from-latex-to-html-using-htlatex
    \usepackage{hyperref}   %\usepackage{hyperref}
    \hypersetup{
        %bookmarks=false,         % show bookmarks bar?
        unicode=true,          % non-Latin characters in Acrobat's bookmarks
        pdftoolbar=true,        % show Acrobat's toolbar?
        pdfmenubar=true,        % show Acrobat's menu?
        pdffitwindow=true,     % window fit to page when opened
        pdfstartview={FitV},    % fits the height of the page to the window:  or fits the width of the page to the window  FitH
        pdftitle={My title},    % title
        pdfauthor={Author},     % author
        pdfsubject={Subject},   % subject of the document
        pdfcreator={Creator},   % creator of the document
        pdfproducer={Producer}, % producer of the document
        pdfkeywords={keyword1} {key2} {key3}, % list of keywords
        pdfnewwindow=true,      % links in new window
        colorlinks=true,       % false: boxed links; true: colored links
        linkcolor=black,          % color of internal links (change box color with linkbordercolor)
        citecolor=black,        % color of links to bibliography
        filecolor=black,      % color of file links
        urlcolor=black          % color of external links
    }

    \bookmarksetup{
        numbered,
        open
    }

    \usepackage[noabbrev]{cleveref}

    %_________________________________________________________________________________________________________________________________________

\makeatother

\begin{document}
    \tableofcontents    \addcontentsline{toc}{chapter}{Table of Contents}
    \listoffigures      %\addcontentsline{toc}{chapter}{List of Figures}
    \listoftables       %\addcontentsline{toc}{chapter}{List of Tables}

    \begin{sloppypar}% must be after abstract and title page

        \part{Author Introduction}
            %\setcounter{chapter}{0}
            \chapter{First}\label{P1I}
            \chapter{Second}\label{P1II}
            \chapter{Third}\label{P1III}


        \part{Paper 1}\label{First Paper}%
        %\setcounter{chapter}{1}
        \begin{paper}
            \section{Introduction}\label{pp1I}
            \section{Materials and Methods}\label{pp1M}
            \section{Results}\label{pp1R}
            \section{Discussion}\label{pp1D}
            \section{Conclusion}\label{pp1C}
            \section{References}\label{pp1B}
        \end{paper}     



        \part{Paper 2}\label{Second Paper}%
        %\setcounter{chapter}{1}
        \begin{paper}
            \section{Introduction}\label{pp2I}
            \section{Materials and Methods}\label{pp2M}
            \section{Results}\label{pp2R}
            \section{Discussion}\label{pp2D}
            \section{Conclusion}\label{pp2C}
            \section{References}\label{pp2B}
        \end{paper}


        \part{Global Discussion}
            \chapter{First}\label{PLI}
            \chapter{Second}\label{PLII}
            \chapter{Third}\label{PLIII}

    \end{sloppypar} 

\end{document}

答案1

问题在于 生成链接目标名称的方式hyperref。如果使用计数器值,则会导致错误,因为第1章本文档中出现四次,每次章节计数器的值为1然后。

要更改的相关部分是hypertexnames=false(初始默认值是真的

摘自手册hyperref

通常,可以通过适当定义 \theH 来解决目标名称重复的问题。如果禁用选项 hypertexnames,则使用唯一的人工数字代替计数器值。对于页面锚点,使用绝对页面锚点。对于选项 plainpages,页面锚点使用阿拉伯语形式。在后两种情况下,索引链接的 \hyperpage 都会受到影响,可能无法正常工作。

例如,当章节计数器被\part(宏,例如部分计数器被refstepped)重置时,就会发生这种情况,就像 OP 示例中所做的那样。在“标准”设置中,计数器编号为不是重置,只是在每个方面都先进\part

因此,要么hypertexnames=false在 中设置\usepackage[...]{hyperref},要么稍后在 中设置\hypersetup。这还将忽略有关重复目标标签的任何警告。

\documentclass{report}

\makeatletter
    %=========================================================================================================================================
    % PACKAGES REQUIRED FOR CONTROLLING AND CONFIGURING TABLE OF CONTENTS AND OTHER LISTS
    %=========================================================================================================================================
    \usepackage[titles]{tocloft}
        \setcounter{secnumdepth}{6} 
        \setcounter{tocdepth}{6}
        \renewcommand{\cftchapfont}{\normalsize \scshape}
    \usepackage[titletoc,title]{appendix}
    \usepackage{etoolbox} % or xpatch

    \usepackage{chngcntr}           %http://tex.stackexchange.com/questions/28333/continuous-v-per-chapter-section-numbering-of-figures-tables-and-other-docume
        \counterwithin*{chapter}{part}  %http://tex.stackexchange.com/questions/54383/how-to-reset-chapter-and-section-counter-with-part
        \counterwithin*{section}{part}
        \counterwithin*{section}{chapter}

        \counterwithin*{figure}{part}
        \counterwithout{figure}{chapter}

        \counterwithin*{table}{part}
        \counterwithout{table}{chapter}

    %http://www.latex-community.org/forum/viewtopic.php?f=47&t=4293
    \renewcommand\part{%    
        \if@openright
        \cleardoublepage
        \else
        \clearpage
        \fi
        \thispagestyle{empty}%
        \if@twocolumn
        \onecolumn
        \@tempswatrue
        \else
        \@tempswafalse
        \fi
        \null\vfil
        \secdef\@part\@spart
    }

    %http://tex.stackexchange.com/questions/103926/part-title-in-list-of-figures-and-tables
    % initial definitions of the chapter info (name and number)
    \def\thischaptertitle{}\def\thischapternumber{}
    \newtoggle{noFigs}
    \newtoggle{noTabs}


    \apptocmd{\@part}%
    {\gdef\thisparttitle{#1}\gdef\thispartnumber{\thepart}%
        \global\toggletrue{noFigs}\global\toggletrue{noTabs}}{}{}

    % the figure environment does the job: the first time it is used after a \chapter command, 
    % it writes the information of the chapter to the LoF
    \AtBeginDocument{%
        \AtBeginEnvironment{figure}{%
            \iftoggle{noFigs}{
                \addtocontents{lof}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{3.2em} }
                \global\togglefalse{noFigs}
            }{}
        }%
        \AtBeginEnvironment{table}{%
            \iftoggle{noTabs}{
                \addtocontents{lot}{\protect\contentsline{part}%
                    {\protect\numberline{\thisparttitle}}{}{} }
                \global\togglefalse{noTabs}
            }{}
        }%
    }


    %/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    % setup environment to permit the removal of page numbers while still continuing sequence when resumed and keeping hyperref happy   // 
    %/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    %http://tex.stackexchange.com/questions/112782/gobble-page-numbers
    \newcounter{nopage}
    \newenvironment{nopage}
    {\clearpage\stepcounter{nopage}%
        \renewcommand{\thepage}{NP\arabic{nopage}}%
        \thispagestyle{empty}}
    {\clearpage\addtocounter{page}{-1}}

    %_________________________________________________________________________________________________________________________________________

    \newenvironment{paper}{%
        \begin{sloppypar}
            \global\let\oldthesection\thesection
            \renewcommand{\thesection}{\@arabic\c@section}%
        }{%
        \end{sloppypar}
        \global\let\thesection\oldthesection
    }


    %=========================================================================================================================================
    % PACKAGES REQUIRED FOR HYPERLINKS AND HYPER-REFERENCES
    %=========================================================================================================================================
    \usepackage{csquotes}
    \usepackage{url}
    \usepackage[nottoc]{tocbibind}
    \usepackage{bookmark}

    %http://tex.stackexchange.com/questions/42927/converting-from-latex-to-html-using-htlatex
    \usepackage{hyperref}   %\usepackage{hyperref}
    \hypersetup{
        %bookmarks=false,         % show bookmarks bar?
        unicode=true,          % non-Latin characters in Acrobat's bookmarks
        pdftoolbar=true,        % show Acrobat's toolbar?
        pdfmenubar=true,        % show Acrobat's menu?
        pdffitwindow=true,     % window fit to page when opened
        pdfstartview={FitV},    % fits the height of the page to the window:  or fits the width of the page to the window  FitH
        pdftitle={My title},    % title
        pdfauthor={Author},     % author
        pdfsubject={Subject},   % subject of the document
        pdfcreator={Creator},   % creator of the document
        pdfproducer={Producer}, % producer of the document
        pdfkeywords={keyword1} {key2} {key3}, % list of keywords
        pdfnewwindow=true,      % links in new window
        colorlinks=true,       % false: boxed links; true: colored links
        linkcolor=black,          % color of internal links (change box color with linkbordercolor)
        citecolor=black,        % color of links to bibliography
        filecolor=black,      % color of file links
        urlcolor=black,          % color of external links
        hypertexnames=false
    }

    \bookmarksetup{
        numbered,
        open
    }

    \usepackage[noabbrev]{cleveref}

    %_________________________________________________________________________________________________________________________________________

\makeatother

\begin{document}
    \tableofcontents    \addcontentsline{toc}{chapter}{Table of Contents}
    \listoffigures      %\addcontentsline{toc}{chapter}{List of Figures}
    \listoftables       %\addcontentsline{toc}{chapter}{List of Tables}

    \begin{sloppypar}% must be after abstract and title page

        \part{Author Introduction}
            %\setcounter{chapter}{0}
            \chapter{First}\label{P1I}
            \chapter{Second}\label{P1II}
            \chapter{Third}\label{P1III}


        \part{Paper 1}\label{First Paper}%
        %\setcounter{chapter}{1}
        \begin{paper}
            \section{Introduction}\label{pp1I}
            \section{Materials and Methods}\label{pp1M}
            \section{Results}\label{pp1R}
            \section{Discussion}\label{pp1D}
            \section{Conclusion}\label{pp1C}
            \section{References}\label{pp1B}
        \end{paper}     



        \part{Paper 2}\label{Second Paper}%
        %\setcounter{chapter}{1}
        \begin{paper}
            \section{Introduction}\label{pp2I}
            \section{Materials and Methods}\label{pp2M}
            \section{Results}\label{pp2R}
            \section{Discussion}\label{pp2D}
            \section{Conclusion}\label{pp2C}
            \section{References}\label{pp2B}
        \end{paper}


        \part{Global Discussion}
            \chapter{First}\label{PLI}
            \chapter{Second}\label{PLII}
            \chapter{Third}\label{PLIII}

    \end{sloppypar} 

\end{document}

相关内容