交叉引用目录中的超链接和分页符

交叉引用目录中的超链接和分页符

TOC 参赛作品要求如下:

  1. (2)和(3)定义的目录分页符应该是自动的(无需手动添加,\addtocontents{toc}{\protect\newpage}将目录拆分为两页
  2. 级别与子级别之间应禁止中断。
  3. 只要满足(2),某个级别就应该具有最少的中断次数,并且更深的级别具有最高的优先级,即:
    • (3.1) 只要满足 (2),如果某个级别无法在少于 (N+1) 个页面且不存在 Overfull \vbox,则该级别就会出现 N 个中断。
    • (3.2)(最大深度)水平突破无论如何都是没有意义的。
    • (3.3)对于X范围从(最大深度-1)到(最小深度),X深度级别不可避免的中断是:
      • (3.3.1)与(2)不冲突。
      • (3.3.2)在不会向更深层次添加任何断点的行上。
      • (3.3.3)位于最低行,以防止 \vbox 过满。
    • (3.4)“内容”与下列项目之间没有间隔,因为这与(2)相冲突。
  4. 级别标题和目录之间的交叉引用应该准确。

问题是 :

  • 条件 (3.1) 和 (3.3.2) 不起作用,因为它应该在 (第 1.32 节) 和 (第 2 部分) 之前破坏目录页面。
  • 条件 (4) 从章节标题到目录条目不起作用,因为您始终会被引导至第一个子级目录条目。
  • 如果未手动添加,则条件 (4) 不适用于从其他级别标题到目录条目(请参阅小节 1.32.2)\addtocontents{toc}{\protect\newpage}

\titleformat{\chapter}命令放在\begin{document}“内容”之后是为了不让其成为死链接。

事实上,这个链接在这里根本就不起作用,因为 (4) 对章节标题不起作用。

无论如何,放置\titleformat{\chapter}在序言中都无法解决问题。

我已经尝试\Hy@raisedlink从超链接导航到超链接超目标后的垂直位置似乎太低

结果可能是由于连字符引起的错误,如下所述`\Hy@raisedlink` 阻止连字并干扰换行

对于章节,问题似乎是由于.toc文件的写入方式造成的,因为\hypertarget {\the part.\thechapter}{}应该在相应的之前写入\contentsline.toc下面给出代码)。

在此先感谢您的帮助。

PS:不确定这个工作示例是否真的很简单,因为我还加载了可能会干扰标题的额外包,即:

这是 .tex 文件:

\documentclass[12pt,a4paper]{book}
\raggedbottom
\setlength{\headheight}{14.49998pt}
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\usepackage{etoc}
\etocsetstyle{part}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[2em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etocsetstyle{chapter}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[4em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etocsetstyle{section}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[6em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etocsetstyle{subsection}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[8em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etoctocstyle{1}{Contents}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyhf[LO,RE]{Author}
\fancypagestyle{plain}{\pagestyle{fancy}}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{\headrulewidth}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[clearempty,explicit]{titlesec}
\renewcommand{\partmark}[1]{\markboth{\thepart. #1}{}}
\renewcommand{\chaptermark}[1]{\markright{#1}{}}
\renewcommand{\sectionmark}[1]{}
\titleformat{\part}{\Huge\bfseries}{\thepart}{2\wordsep}{\hyperlink{\thepart}{#1}\addtocontents{toc}{\protect\hypertarget{\thepart}{}}}
\titleformat{\section}{\Large\bfseries}{\thesection}{2\wordsep}{\hyperlink{\theHsection}{#1}\addtocontents{toc}{\protect\hypertarget{\theHsection}{}}}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{2\wordsep}{\hyperlink{\theHsubsection}{#1}\addtocontents{toc}{\protect\hypertarget{\theHsubsection}{}}}
\titlespacing{\chapter}{0pt}{0pt}{\baselineskip}
\titlespacing{\section}{0pt}{\baselineskip}{\baselineskip}
\titlespacing{\subsection}{0pt}{\baselineskip}{\baselineskip}
\usepackage[hidelinks,linktoc=all]{hyperref}
\renewcommand{\theHchapter}{\thepart.\thechapter}
\renewcommand{\theHsection}{\thepart.\thesection}
\renewcommand{\theHsubsection}{\thepart.\thesubsection}
\begin{document}
\titleformat{\chapter}{\huge\bfseries}{}{0pt}{Contents\markright{#1}{}}{}
\fancyhead[LE,RO]{\MakeUppercase\rightmark}
\tableofcontents
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{2\wordsep}{\hyperlink{\theHchapter}{#1}\addtocontents{toc}{\protect\hypertarget{\theHchapter}{}}}

\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The First Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\section{A section}

\subsection{A subsection}

\subsection{A subsection}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The Second Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\chapter{A chapter}

\section{A section}

\end{document}

这是 .toc 文件:

\@ifundefined {etoctocstyle}{\let \etoc@startlocaltoc \@gobble \let \etoc@settocdepth \@gobble \let \etoc@depthtag \@gobble }{}
\hypertarget {I}{}
\contentsline {part}{I\hspace {1em}The First Part}{5}{part.1}
\contentsline {chapter}{\numberline {1}A chapter}{7}{chapter.I.1}
\hypertarget {I.1}{}
\hypertarget {I.1.1}{}
\contentsline {section}{\numberline {1.1}A section}{7}{section.I.1.1}
\hypertarget {I.1.2}{}
\contentsline {section}{\numberline {1.2}A section}{7}{section.I.1.2}
\hypertarget {I.1.3}{}
\contentsline {section}{\numberline {1.3}A section}{7}{section.I.1.3}
\hypertarget {I.1.4}{}
\contentsline {section}{\numberline {1.4}A section}{7}{section.I.1.4}
\hypertarget {I.1.5}{}
\contentsline {section}{\numberline {1.5}A section}{7}{section.I.1.5}
\hypertarget {I.1.6}{}
\contentsline {section}{\numberline {1.6}A section}{7}{section.I.1.6}
\hypertarget {I.1.7}{}
\contentsline {section}{\numberline {1.7}A section}{7}{section.I.1.7}
\hypertarget {I.1.8}{}
\contentsline {section}{\numberline {1.8}A section}{7}{section.I.1.8}
\hypertarget {I.1.9}{}
\contentsline {section}{\numberline {1.9}A section}{7}{section.I.1.9}
\hypertarget {I.1.10}{}
\contentsline {section}{\numberline {1.10}A section}{7}{section.I.1.10}
\hypertarget {I.1.11}{}
\contentsline {section}{\numberline {1.11}A section}{7}{section.I.1.11}
\hypertarget {I.1.12}{}
\contentsline {section}{\numberline {1.12}A section}{7}{section.I.1.12}
\hypertarget {I.1.13}{}
\contentsline {section}{\numberline {1.13}A section}{7}{section.I.1.13}
\hypertarget {I.1.14}{}
\contentsline {section}{\numberline {1.14}A section}{7}{section.I.1.14}
\hypertarget {I.1.15}{}
\contentsline {section}{\numberline {1.15}A section}{8}{section.I.1.15}
\hypertarget {I.1.16}{}
\contentsline {section}{\numberline {1.16}A section}{8}{section.I.1.16}
\hypertarget {I.1.17}{}
\contentsline {section}{\numberline {1.17}A section}{8}{section.I.1.17}
\hypertarget {I.1.18}{}
\contentsline {section}{\numberline {1.18}A section}{8}{section.I.1.18}
\hypertarget {I.1.19}{}
\contentsline {section}{\numberline {1.19}A section}{8}{section.I.1.19}
\hypertarget {I.1.20}{}
\contentsline {section}{\numberline {1.20}A section}{8}{section.I.1.20}
\hypertarget {I.1.21}{}
\contentsline {section}{\numberline {1.21}A section}{8}{section.I.1.21}
\hypertarget {I.1.22}{}
\contentsline {section}{\numberline {1.22}A section}{8}{section.I.1.22}
\hypertarget {I.1.23}{}
\contentsline {section}{\numberline {1.23}A section}{8}{section.I.1.23}
\hypertarget {I.1.24}{}
\contentsline {section}{\numberline {1.24}A section}{8}{section.I.1.24}
\hypertarget {I.1.25}{}
\contentsline {section}{\numberline {1.25}A section}{8}{section.I.1.25}
\hypertarget {I.1.26}{}
\contentsline {section}{\numberline {1.26}A section}{8}{section.I.1.26}
\hypertarget {I.1.27}{}
\contentsline {section}{\numberline {1.27}A section}{8}{section.I.1.27}
\hypertarget {I.1.28}{}
\contentsline {section}{\numberline {1.28}A section}{8}{section.I.1.28}
\hypertarget {I.1.29}{}
\contentsline {section}{\numberline {1.29}A section}{8}{section.I.1.29}
\hypertarget {I.1.30}{}
\contentsline {section}{\numberline {1.30}A section}{9}{section.I.1.30}
\hypertarget {I.1.31}{}
\contentsline {section}{\numberline {1.31}A section}{9}{section.I.1.31}
\hypertarget {I.1.32}{}
\contentsline {section}{\numberline {1.32}A section}{9}{section.I.1.32}
\hypertarget {I.1.32.1}{}
\contentsline {subsection}{\numberline {1.32.1}A subsection}{9}{subsection.I.1.32.1}
\hypertarget {I.1.32.2}{}
\contentsline {subsection}{\numberline {1.32.2}A subsection}{9}{subsection.I.1.32.2}
\hypertarget {I.1.33}{}
\contentsline {section}{\numberline {1.33}A section}{9}{section.I.1.33}
\hypertarget {I.1.34}{}
\contentsline {section}{\numberline {1.34}A section}{9}{section.I.1.34}
\hypertarget {I.1.35}{}
\contentsline {section}{\numberline {1.35}A section}{9}{section.I.1.35}
\hypertarget {I.1.36}{}
\contentsline {section}{\numberline {1.36}A section}{9}{section.I.1.36}
\hypertarget {I.1.37}{}
\contentsline {section}{\numberline {1.37}A section}{9}{section.I.1.37}
\hypertarget {I.1.38}{}
\contentsline {section}{\numberline {1.38}A section}{9}{section.I.1.38}
\hypertarget {I.1.39}{}
\contentsline {section}{\numberline {1.39}A section}{9}{section.I.1.39}
\hypertarget {I.1.40}{}
\contentsline {section}{\numberline {1.40}A section}{9}{section.I.1.40}
\hypertarget {I.1.41}{}
\contentsline {section}{\numberline {1.41}A section}{9}{section.I.1.41}
\hypertarget {I.1.42}{}
\contentsline {section}{\numberline {1.42}A section}{9}{section.I.1.42}
\contentsline {chapter}{\numberline {2}A chapter}{11}{chapter.I.2}
\hypertarget {I.2}{}
\hypertarget {I.2.1}{}
\contentsline {section}{\numberline {2.1}A section}{11}{section.I.2.1}
\hypertarget {I.2.2}{}
\contentsline {section}{\numberline {2.2}A section}{11}{section.I.2.2}
\hypertarget {I.2.3}{}
\contentsline {section}{\numberline {2.3}A section}{11}{section.I.2.3}
\hypertarget {I.2.4}{}
\contentsline {section}{\numberline {2.4}A section}{11}{section.I.2.4}
\hypertarget {I.2.5}{}
\contentsline {section}{\numberline {2.5}A section}{11}{section.I.2.5}
\hypertarget {I.2.6}{}
\contentsline {section}{\numberline {2.6}A section}{11}{section.I.2.6}
\hypertarget {I.2.7}{}
\contentsline {section}{\numberline {2.7}A section}{11}{section.I.2.7}
\hypertarget {I.2.8}{}
\contentsline {section}{\numberline {2.8}A section}{11}{section.I.2.8}
\hypertarget {I.2.9}{}
\contentsline {section}{\numberline {2.9}A section}{11}{section.I.2.9}
\hypertarget {I.2.10}{}
\contentsline {section}{\numberline {2.10}A section}{11}{section.I.2.10}
\hypertarget {I.2.11}{}
\contentsline {section}{\numberline {2.11}A section}{11}{section.I.2.11}
\hypertarget {I.2.12}{}
\contentsline {section}{\numberline {2.12}A section}{11}{section.I.2.12}
\hypertarget {I.2.13}{}
\contentsline {section}{\numberline {2.13}A section}{11}{section.I.2.13}
\hypertarget {I.2.14}{}
\contentsline {section}{\numberline {2.14}A section}{11}{section.I.2.14}
\hypertarget {I.2.15}{}
\contentsline {section}{\numberline {2.15}A section}{12}{section.I.2.15}
\hypertarget {I.2.16}{}
\contentsline {section}{\numberline {2.16}A section}{12}{section.I.2.16}
\hypertarget {I.2.17}{}
\contentsline {section}{\numberline {2.17}A section}{12}{section.I.2.17}
\hypertarget {I.2.18}{}
\contentsline {section}{\numberline {2.18}A section}{12}{section.I.2.18}
\hypertarget {I.2.19}{}
\contentsline {section}{\numberline {2.19}A section}{12}{section.I.2.19}
\hypertarget {I.2.20}{}
\contentsline {section}{\numberline {2.20}A section}{12}{section.I.2.20}
\hypertarget {I.2.21}{}
\contentsline {section}{\numberline {2.21}A section}{12}{section.I.2.21}
\hypertarget {I.2.22}{}
\contentsline {section}{\numberline {2.22}A section}{12}{section.I.2.22}
\hypertarget {I.2.23}{}
\contentsline {section}{\numberline {2.23}A section}{12}{section.I.2.23}
\hypertarget {II}{}
\contentsline {part}{II\hspace {1em}The Second Part}{13}{part.2}
\contentsline {chapter}{\numberline {1}A chapter}{15}{chapter.II.1}
\hypertarget {II.1}{}
\hypertarget {II.1.1}{}
\contentsline {section}{\numberline {1.1}A section}{15}{section.II.1.1}
\hypertarget {II.1.2}{}
\contentsline {section}{\numberline {1.2}A section}{15}{section.II.1.2}
\hypertarget {II.1.3}{}
\contentsline {section}{\numberline {1.3}A section}{15}{section.II.1.3}
\hypertarget {II.1.4}{}
\contentsline {section}{\numberline {1.4}A section}{15}{section.II.1.4}
\contentsline {chapter}{\numberline {2}A chapter}{17}{chapter.II.2}
\hypertarget {II.2}{}
\hypertarget {II.2.1}{}
\contentsline {section}{\numberline {2.1}A section}{17}{section.II.2.1}

答案1

关键是要修改以在同一行上\contentsline添加。在某些环境(如表格)中,瞄准太低一行是一个已知问题,可以使用或修复。请参阅\hypertarget\hypertarget\raisebox\vadjust pre{...}这个问题

注意:hyperref 修改\addtocontents\contentsline包含超目标名称。我只是将.cross这个名称添加到交叉引用中。

\documentclass[12pt,a4paper]{book}
\raggedbottom
\setlength{\headheight}{14.49998pt}
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\usepackage{etoc}
\etocsetstyle{part}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[2em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etocsetstyle{chapter}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[4em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etocsetstyle{section}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[6em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etocsetstyle{subsection}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[8em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
\etoctocstyle{1}{Contents}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyhf[LO,RE]{Author}
\fancypagestyle{plain}{\pagestyle{fancy}}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{\headrulewidth}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[clearempty,explicit]{titlesec}
\renewcommand{\partmark}[1]{\markboth{\thepart. #1}{}}
\renewcommand{\chaptermark}[1]{\markright{#1}{}}
\renewcommand{\sectionmark}[1]{}
\titleformat{\part}{\Huge\bfseries}{\thepart}{2\wordsep}{\hyperlink{part.\theHpart.cross}{#1}}
\titleformat{\section}{\Large\bfseries}{\thesection}{2\wordsep}{\hyperlink{section.\theHsection.cross}{#1}}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{2\wordsep}{\hyperlink{subsection.\theHsubsection.cross}{#1}}
\titlespacing{\chapter}{0pt}{0pt}{\baselineskip}
\titlespacing{\section}{0pt}{\baselineskip}{\baselineskip}
\titlespacing{\subsection}{0pt}{\baselineskip}{\baselineskip}
\usepackage[hidelinks,linktoc=all]{hyperref}
\renewcommand{\theHchapter}{\thepart.\thechapter}
\renewcommand{\theHsection}{\thepart.\thesection}
\renewcommand{\theHsubsection}{\thepart.\thesubsection}

\let\oldcontentsline=\contentsline
\def\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{\raisebox{\baselineskip}{\hypertarget{#4.cross}{\relax}}#3}{#4}}

\begin{document}
\fancyhead[LE,RO]{\MakeUppercase\rightmark}
\titleformat{\chapter}{\huge\bfseries}{}{0pt}{Contents\markright{#1}}
\tableofcontents
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{2\wordsep}{\hyperlink{chapter.\theHchapter.cross}{#1}}

\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The First Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\section{A section}

\subsection{A subsection}

\subsection{A subsection}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The Second Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\chapter{A chapter}

\section{A section}

\end{document}

相关内容