目录 链接至错误章节

目录 链接至错误章节

我目前正在使用自定义章节编号编写文档,但它已经填满了内容超链接。

我的编号方式是,前几章使用从 I 开始的罗马数字进行编号,然后另一组章节重新开始编号,这次使用阿拉伯数字。

但是,当我点击第一个阿拉伯数字章节的链接时,它会带我到第一个罗马数字章节,其他所有数字的章节也是如此(即 2-->II)。页码链接也有同样的问题。

如果我手动将章节链接添加到目录中,那么它不会在页边距的标题前添加数字,这是我想保留的。

我的代码本质上的一个例子是:

\documentclass[a4paper, english, 12pt]{book}
\setlength{\parindent}{0em}

\usepackage{fancyhdr}

\usepackage{fix-cm}

\usepackage[justification=centering]{caption}
\usepackage{babel}
\usepackage{subcaption}
\usepackage{placeins}

\usepackage{amsmath}
    \numberwithin{equation}{chapter}
    \setlength{\jot}{2em}
\usepackage{nccmath}

\usepackage{amssymb}
\usepackage{cancel}
\usepackage{siunitx}
\usepackage{esint}
\usepackage{accents}
\usepackage{pgfplots}
\usepackage{tikzscale}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,patterns,angles,quotes}
\usepackage{pgf}
\usepackage{calc}
\usepackage{lastpage}
\usepackage{graphicx}
\usepackage{bigints}
\usepackage{array}
\usepackage[none]{hyphenat}
\usepackage[a4paper, portrait, right=20mm, left=20mm, top=25mm, bottom=20mm]{geometry}
\usepackage{url}
\usepackage{adjustbox}

\usepackage{subfiles}
%See https://en.wikibooks.org/wiki/LaTeX/Modular_Documents

\usepackage[hidelinks, linktoc=all]{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=black,
    filecolor=magenta,      
    urlcolor=blue,
    pdfpagemode=FullScreen,
}

\usepackage{xpatch}
\newlength{\beforechapskip}
\newlength{\chaptertopskip}
\newlength{\chapterbottomskip}
\setlength{\beforechapskip}{0mm}
\setlength{\chaptertopskip}{0mm}
\setlength{\chapterbottomskip}{10mm}



\renewcommand{\chaptermark}[1]{\markboth{\Large #1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\begin{document}

    \frontmatter
    \phantomsection 
    \hypertarget{ToC}{}  % Make an anchor to the toc
    \tableofcontents
    \thispagestyle{empty}
    \newpage

    \mainmatter
    %Defines numbering for pre-modules as roman
    \renewcommand{\thechapter}{\Roman{chapter}}
    \makeatletter
    \def\@makechapterhead#1{%
    \vspace*{\chaptertopskip}%
    {\parindent \z@ \raggedright \normalfont
        \ifnum \c@secnumdepth >\m@ne
        \if@mainmatter
            \huge\bfseries Pre-Module\space \thechapter:\
    %        \par\nobreak
    %        \vskip 20\p@

        \fi
        \fi
        \interlinepenalty\@M
        \Huge \bfseries #1\par\nobreak
        \vskip \chapterbottomskip
    }}
    \makeatother
    % See https://tex.stackexchange.com/questions/309920/how-to-move-the-chapter-title-upwards-on-page

    %-------------------------------------------------------------------------------------------------------------
    %Defines numbering for pre-modules as roman

    \chapter{Example I}

    \chapter{Example II}


    \renewcommand{\thechapter}{\arabic{chapter}}
    %Defines numbering for modules as arabic numbering (normal)

     \makeatletter
     \def\@makechapterhead#1{%
     \vspace*{\chaptertopskip}%
     {\parindent \z@ \raggedright \normalfont
         \ifnum \c@secnumdepth >\m@ne
         \if@mainmatter
             \huge\bfseries Module\space \thechapter:\
     %        \par\nobreak
     %        \vskip 20\p@
         \fi
         \fi
         \interlinepenalty\@M
         \Huge \bfseries #1\par\nobreak
         \vskip \chapterbottomskip
     }}
     \makeatother
     % See https://tex.stackexchange.com/questions/309920/how-to-move-the-chapter-title-upwards-on-page
     %The guy from the internet basically had this word for word, though some changes have been made to make it say module and what not.
 
     \setcounter{chapter}{0}%makes the module numbering start at x+1, where x is the number here
 
 
     %-----------------------------------------------------------------------------------------------------------------------------------------------



     %Defines numbering for modules as arabic
     \chapter{Example 1}
     \chapter{Example 2}

\end{document}

答案1

重新定义 hyperref 的计数器表示

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\theHchapter}{\Roman{chapter}}

 
\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\theHchapter}{\arabic{chapter}}

然后 hyperref 就有机会创建独特的目的地。

相关内容