花哨标题中的超链接未指向正确的页面

花哨标题中的超链接未指向正确的页面

这是我的main.tex文件(将其作为整个文件添加到这里,因为我不知道可能会有什么冲突):

\documentclass[9pt,twoside]{extbook}
\usepackage{adforn} % ornaments glyphs adforn font
\usepackage{ifthen}
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{extramarks}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}%
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}% produces numofchapter.numsection name of section now
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}% produces numofchapter.numsection.numofsubsection name of subsection now

\fancyhf{}%
\fancyhead{}%
\fancyhead[L]{%
    \large{%
        \hyperref[Index]{Index}% Point to index, always present. Works correctly
        \ifthenelse{\not\equal{\nameref{\chaptername::\thechapter}}{}}{% Point to chapter now, only if chapter isn't empty
            \ifthenelse{\not\equal{\value{chapter}}{0}}{% Point to chapter now, only if it's number isn't 0
                \ \adforn{43}\ % Symbol >
                \hyperlink{\chaptername::\thechapter}{\thechapter\ \nameref{\chaptername::\thechapter}}% The hyperlink.
                \ifthenelse{\not\equal{\firstleftmark}{}}{% Point to section now, only if it isn't empty
                    \ifthenelse{\not\equal{\value{section}}{0}}{% Point to section now, only if it's number isn't 0
                        \ \adforn{43}\ % Symbol >
                        \hyperlink{section::\thesection}{\firstleftmark}% The hyperlink.
                        \ifthenelse{\not\equal{\value{subsection}}{0}}{% Point to subsection now, only if it's number isn't 0
                            \ifthenelse{\not\equal{\firstrightmark}{}}{% Point to section now, only if it's number isn't 0
                                \ifthenelse{\not\equal{\firstleftmark}{\firstrightmark}}{% Point to subsection now, only if section and subsection don't have the same name
                                    \ \adforn{43}\ % Symbol >
                                    \hyperlink{subsection::\thesubsection}{\firstrightmark}% The hyperlink.
                                }{}%
                            }{}%
                        }{}%
                    }{}%
                }{}%
            }{}%
        }{}%
    }%
}%
\fancyfoot{}%
\fancyfoot[C]{%
    \large{%
        \thepage%
    }%
}%

\begin{document}

\addcontentsline{toc}{chapter}{Index} \label{Index} \tableofcontents
\chapter{Introduction} \label{chapter:Introduction} \label{\chaptername::\thechapter} \lipsum[1-3]
\newpage \section{Overview} \label{section:Introduction/Overview} \lipsum[1-5]
\newpage \section{Problem statement} \label{section:Introduction/Problemstatement} \lipsum[1-4]
\subsection{Context} \label{subsection:Introduction/Problemstatement/Context} \lipsum[1-4]

\end{document}

章节和子章节的超链接没有指向正确的页面,而是指向文档的第一页(可能是某些功能无法正常工作时的默认行为)。一切都编译正确。

渲染为:

这就是它的渲染方式

答案1

通过使用Ulrike Fischer 的解决方案 并将其与我已经拥有的内容混合,我最终的解决方案(包含所有边缘情况处理)如下:

\NewDocumentCommand\myhyperlink{mm}{\hyperlink{#1}{#2}}% New line
\renewcommand{\sectionmark}[1]{\markboth{\myhyperlink{section.\theHsection}{\thesection\ #1}}{}}% Changed
\renewcommand{\subsectionmark}[1]{\markright{\myhyperlink{subsection.\theHsubsection}{\thesubsection\ #1}}}% Changed

\fancyhf{}%
\fancyhead{}%
\fancyhead[L]{%
    \large{%
        \hyperref[Index]{Index}%
        \ifthenelse{\not\equal{\nameref{\chaptername::\thechapter}}{}}{%
            \ifthenelse{\not\equal{\value{chapter}}{0}}{%
                \ \adforn{43}\ %
                \hyperref[\chaptername::\thechapter]{\thechapter\ \nameref{\chaptername::\thechapter}}%
                \ifthenelse{\not\equal{\firstleftmark}{}}{%
                    \ifthenelse{\not\equal{\value{section}}{0}}{%
                        \ \adforn{43}\ %
                        \firstleftmark% Changed
                        \ifthenelse{\not\equal{\value{subsection}}{0}}{%
                            \ifthenelse{\not\equal{\firstrightmark}{}}{%
                                    \ \adforn{43}\ %
                                    \firstrightmark% Changed
                                }{}%
                            }{}%
                        }{}%
                    }{}%
                }{}%
            }{}%
        }{}%
    }%
}%
\fancyfoot{}%
\fancyfoot[C]{%
    \large{%
        \thepage%
    }%
}%

答案2

我没有时间整理所有细节,但在核心中,如果你想获得正确的标题,你必须通过标记命令传递超链接。比较数字并不可靠。

\documentclass[9pt,twoside]{extbook}
\usepackage{adforn} % ornaments glyphs adforn font
\usepackage{ifthen}
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{extramarks}
\usepackage{lipsum}
\usepackage{fancyhdr}
\pagestyle{fancy}%
\NewDocumentCommand\myhyperlink{mm}{\ \adforn{43}\ \hyperlink{#1}{#2}}
\renewcommand{\sectionmark}[1]{\markboth{\myhyperlink{section.\theHsection}{\thesection\ #1}}{}}% 
\renewcommand{\subsectionmark}[1]{\markright{\myhyperlink{subsection.\theHsubsection}{\thesubsection\ #1}}}% 

\fancyhf{}%
\fancyhead{}%
\fancyhead[L]{%
    \large
        \hyperref[Index]{Index}% Point to index, always present. Works correctly
        \firstleftmark \lastrightmark
    }%
\fancyfoot{}%
\fancyfoot[C]{%
    \large{%
        \thepage%
    }%
}%

\begin{document}

\addcontentsline{toc}{chapter}{Index} \label{Index} \tableofcontents \markboth{}{}
\chapter{Introduction} \label{chapter:Introduction} \label{\chaptername::\thechapter} \lipsum[1-3]
\newpage \section{Overview} \label{section:Introduction/Overview} \lipsum[1-5]
\newpage \section{Problem statement} \label{section:Introduction/Problemstatement} \lipsum[1-4]
\subsection{Context} \label{subsection:Introduction/Problemstatement/Context} \lipsum[1-4]

\end{document}
~~~~

相关内容