定义引导标题 [大写] 会破坏自动 hyperref 的 pdf 书签

定义引导标题 [大写] 会破坏自动 hyperref 的 pdf 书签

我正在尝试使用一些引导样式的标题文本和 pdf 书签来定义我自己的文档类。

这是我的课程文件myclass.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}

\LoadClass[12pt,oneside,a4paper]{article}

\makeatletter

% disable section numbering
\setcounter{secnumdepth}{-2}    

% define lead-in section
\renewcommand\section{  
\@startsection{section}{1}{\z@}{-1ex}{-1.5ex}{\bfseries\MakeUppercase}%
}

\RequirePackage{hyperref}

\hypersetup{bookmarksdepth=4}

\makeatother

这是我的文档来源mydoc.tex

\documentclass{myclass}

\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\section{First Section}

\blindtext[5]


\section{Second Section}

\blindtext[5]


\section{Third Section}

\blindtext[5]


\end{document}

如果我使用 pdflatex 编译它,我会收到以下警告:

pdfTeX warning (dest): name{section*.
3} has been referenced but does not exist, replaced by a fixed one

pdfTeX warning (dest): name{section*.2} has been referenced but does not exist,
 replaced by a fixed one

pdfTeX warning (dest): name{section*.1} has been referenced but does not exist,
 replaced by a fixed one

并且,当在 pdfviewer 中点击时,pdf 书签不会转到其各自的标题。

如果删除了注释下方的 renewcommand % define lead-in section,书签就可以正常工作(但这样我就得不到我想要的标题样式了。如果可能的话,我强烈希望保留自动部分标签/书签。关于如何实现这一点,有什么想法吗?

答案1

也许你可以使用\MakeUppercase-variant 的文本大小写-package 并修补其底层\@uclcnotmath-command 以防止使用 进行大小写更改\Hy@SectionAnchorHref,当加载 hyperref 时,会创建部分的锚点/命名目标:

\documentclass[12pt,oneside,a4paper]{article}

\usepackage[overload]{textcase}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@uclcnotmath}%
         {\@nonchangecase\ensuremath}%
         {\@nonchangecase\ensuremath\@nonchangecase\Hy@SectionAnchorHref}%
         {\message{!!! Patching successful !!!}}%
         {\message{!!! Patching failed !!!}}%
\makeatother


\setcounter{secnumdepth}{-2}

\makeatletter
\renewcommand\section{%%%%
   \@startsection{section}{1}{\z@}{-1ex}{-1.5ex}{\bfseries\MakeUppercase}%
}
\makeatother

\RequirePackage{hyperref}

\hypersetup{bookmarksdepth=4}

\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

\section{First Section}

\blindtext[5]


\section{Second Section}

\blindtext[5]


\section{Third Section}

\blindtext[5]


\end{document}

编写/测试上述代码时使用的内容:

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex 2021.1.17)  30 JAN 2021 21:33
entering extended mode
[...]
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-01-09> xparse <2020-03-03>
[...]
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
[...]
Package: textcase 2019/09/14 v1.00 Text only upper/lower case changing (DPC)
[...]
Package: etoolbox 2020/10/05 v2.5k e-TeX tools for LaTeX (JAW)
[...]
Package: hyperref 2020-05-15 v7.00e Hypertext links for LaTeX
[...]
Package: blindtext 2012/01/06 V2.0 blindtext-Package
[...]
Package: babel 2020/12/16 3.52 The Babel package
[...]
File: babel.def 2020/12/16 3.52 Babel common definitions
[...]
Language: english 2017/06/06 v3.3r English support from the babel system
[...]

相关内容