titlesec 弄乱了我的超链接目录

titlesec 弄乱了我的超链接目录

请考虑以下多页 MWE,其中包含页码重新启动;以便进行编译pdflatex test.tex

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{twoside,inner=50bp,outer=30bp,top=50bp,bottom=50bp}


% \usepackage{titlesec}
% \titleformat{\section}{\centering\normalsize}{\thesection}{1em}{}
% \titleformat{\subsection}{\centering\small}{\thesubsection}{1em}{}


\usepackage{layouts}
\usepackage{etoolbox}
\patchcmd{\drawpage}{\ifdrawparameters}{\iftrue}%
  {\typeout{^^J*******\string\drawpage fixed*******^^J}}%
  {\typeout{^^J*******\string\drawpage not fixed*******^^J}}

\usepackage{hyperref}
\usepackage{tikz,enumitem}
\usepackage{fix-cm}

\usepackage{lipsum}



\begin{document}


  \pagestyle{empty} % no page numbers on next few pages

% pg 1
    \begin{tikzpicture}[overlay,remember picture,yscale=-1, inner sep=0pt, outer sep=0pt,anchor=north west]

      \path (current page.center) node[anchor=center] (textX) {%

        Physical page 1 / Title

      };

    \end{tikzpicture}

  \clearpage

% pg 2
  Physical page 2 / Empty

  \clearpage

% pg 3
  Physical page 3 / TOC

  \tableofcontents

  \clearpage

% pg 4
  %Physical page 4 / Empty
  \ \\

  \clearpage

  \pagenumbering{arabic} % restart page numbering from 1
  \pagestyle{plain} % show page numbers

% pg 5
  Physical page 5

  \section*{Section One}
  \addcontentsline{toc}{section}{Section One}

  \subsection*{Subsection One}
  \addcontentsline{toc}{subsection}{Subsection One}

  \lipsum[1]

  \clearpage

% pg 6
  Physical page 6

  \subsection*{Subsection Two}
  \addcontentsline{toc}{subsection}{Subsection Two}

  \lipsum[2]

  \clearpage

% pg 7
  Physical page 7

  \section*{Section Two}
  \addcontentsline{toc}{section}{Section Two}

  \lipsum[3]

  \clearpage

% pg 8
  Physical page 8

  \subsection*{Subsection Two}
  \addcontentsline{toc}{subsection}{Subsection Two}

  \lipsum[4]

  \clearpage


\end{document}

当编译(两次)时,PDF 中绝对没有问题 - PDF 中的目录可点击链接按预期工作;请注意,对于这种正确的情况,我们有test.toc

\contentsline {section}{Section One}{1}{section*.2}
\contentsline {subsection}{Subsection One}{1}{section*.3}
\contentsline {subsection}{Subsection Two}{2}{section*.4}
\contentsline {section}{Section Two}{3}{section*.5}
\contentsline {subsection}{Subsection Two}{4}{section*.6}

test.out

\BOOKMARK [1][-]{section*.2}{Section One}{}% 1
\BOOKMARK [2][-]{section*.3}{Subsection One}{section*.2}% 2
\BOOKMARK [2][-]{section*.4}{Subsection Two}{section*.2}% 3
\BOOKMARK [1][-]{section*.5}{Section Two}{}% 4
\BOOKMARK [2][-]{section*.6}{Subsection Two}{section*.5}% 5

 

现在,取消注释 周围的三行\usepackage{titlesec},然后重新编译(*.{out,toc,aux}如果需要,可以删除之前的行)。最终你可以看到pdflatex吐出的内容:

Package hyperref Warning: The anchor of a bookmark and its parent's must not
(hyperref)                be the same. Added a new anchor on input line 72.

pdfTeX warning (ext4): destination with the same identifier (name{page.1}) has 
been already used, duplicate ignored

现在,目录中的第一个链接(指向“第一部分”)基本上指向物理页面 1(文档的开始) - 而不是物理页面 5(重新启动后逻辑页面 1),它应该在那里!在这种情况下,test.toc是:

\contentsline {section}{Section One}{1}{Doc-Start}
\contentsline {subsection}{Subsection One}{1}{section*.1}
\contentsline {subsection}{Subsection Two}{2}{section*.1}
\contentsline {section}{Section Two}{3}{section*.1}
\contentsline {subsection}{Subsection Two}{4}{section*.2}

... 并且test.out是:

\BOOKMARK [1][-]{Doc-Start}{Section One}{}% 1
\BOOKMARK [2][-]{section*.1}{Subsection One}{Doc-Start}% 2
\BOOKMARK [2][-]{section*.1}{Subsection Two}{Doc-Start}% 3
\BOOKMARK [1][-]{section*.1}{Section Two}{}% 4
\BOOKMARK [2][-]{section*.2}{Subsection Two}{section*.1}% 5

 

我的问题是 - 我是否可以以某种方式使用titlesec上述示例中的包,并且仍然可以获得 PDF 中目录的正确超链接?

非常感谢您的任何回答,
干杯!

答案1

编辑:对我有用的是避免 titlesec,并且更改章节标题旧方法 - 将建议包装\renewcommand\makeatletter序言\makeatother中,如下所示:

\makeatletter
\renewcommand{\section}{\@startsection
{section}%                   % the name
{1}%                         % the level
{\z@}%                       % the indent / 0mm
{-\baselineskip}%            % the before skip / -3.5ex \@plus -1ex \@minus -.2ex
{0.5\baselineskip}%          % the after skip / 2.3ex \@plus .2ex
{\centering\normalsize}} % the style

好吧,显然这是一件众所周知的事情:

http://www.tug.org/applications/hyperref/ftp/README

titlesec
--------
  "nameref" supports titlesec, but hyperref does not
  (unsolved is the anchor setting, missing with unnumbered
  section, perhaps problems with page breaks with numbered ones).

好吧,如果有人知道解决方法,那就太好了:)

干杯!

答案2

您的设置似乎表明您对对部门单位进行编号不感兴趣。因此,继续使用\section\subsection但添加

\setcounter{secnumdepth}{0}

到文档序言中。随后,使用titlesec尽管存在一些编译器警告,但仍提供了指向目录中各部分单元的正确超链接。

以下是你的序言:

\documentclass[12pt]{article}

\usepackage{geometry}
\geometry{twoside,inner=50bp,outer=30bp,top=50bp,bottom=50bp}

\setcounter{secnumdepth}{0}

\usepackage{titlesec}
\titleformat{\section}{\centering\normalsize}{\thesection}{1em}{}
\titleformat{\subsection}{\centering\small}{\thesubsection}{1em}{}

\usepackage{layouts}
\usepackage{etoolbox}
\patchcmd{\drawpage}{\ifdrawparameters}{\iftrue}%
  {\typeout{^^J*******\string\drawpage fixed*******^^J}}%
  {\typeout{^^J*******\string\drawpage not fixed*******^^J}}

\usepackage{hyperref}
\usepackage{tikz,enumitem}
\usepackage{fix-cm}
\usepackage{lipsum}

\begin{document}

答案3

我采用了不同的方法来解决它:我使用 titletoc 包来配置目录(方式与 latex 默认的方式非常相似),最终我能够一起使用 hyperref 和 titlesec。

下面是我的代码片段:

\documentclass{book}

\usepackage{titlesec,titletoc,hyperref}

% Section heading format/spacing    
\titleformat{\chapter}[block]%
    {\flushright\huge\bfseries}{\thechapter.}{2ex}{}[\titlerule]
\titlespacing{\chapter}{0pt}{.45\textheight}{3em}
\titleformat{\section}{\normalfont\bfseries}{\thesection.}{5ex}{}
\titleformat{\subsection}{\normalfont}{\thesubsection.}{3ex}{}
\titleformat{\subsubsection}{\normalfont}{\thesubsubsection.}{3ex}{}

% Table of contents line format
\titlecontents{part}[0pt]{\vspace{2em}}%
    {\bfseries\Large\contentslabel{-2.3pc}}{\bfseries\Large\hspace*{-2pc}}%
    {\bfseries\Large\titlerule*{ }\contentspage}
\titlecontents{chapter}[2em]{}%
    {\bfseries\contentslabel{2.3pc}}{\bfseries\hspace*{-2pc}}%
    {\bfseries\titlerule*{ }\contentspage}
\titlecontents{section}[2em]{}%
    {\contentslabel{2.3pc}}{\hspace*{-2pc}}%
    {\titlerule*{.}\contentspage}
\titlecontents{subsection}[2em]{}%
    {\contentslabel{2.3pc}}{\hspace*{-2pc}}%
    {\titlerule*{.}\contentspage}
\titlecontents{subsubsection}[2em]{}%
    {\contentslabel{2.3pc}}{\hspace*{-2pc}}%
    {\titlerule*{.}\contentspage}

\begin{document}

\tableofcontents

\part{Part One}
\chapter{Chapter One}
\section{Section One}
\section{Section Two}

\chapter{Chapter Two}

\end{document}

如果这篇文章停止发布,我很抱歉,但是我找到了一个更为舒适的替代解决方案,并认为我应该把它留在这里,以防其他人最终发现这里。

相关内容