我需要在 PDF 书签中写入章节编号,但仅限于章节(而不是子章节等)。期望结果:
现在我使用bookmark
包,这样我就可以用它为每个书签写数字编号选项。是否可以只显示顶级书签的数字?(其他级别的项目,如子部分和更深层次的项目必须显示在书签中,但不显示数字)
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[
open,
openlevel=2,
atend,
numbered
]{bookmark}
\makeatletter
\renewcommand{\Hy@numberline}[1]{#1. }
\makeatother
\begin{document}
\section{Bla bla}
...
\subsection{Lorem}
...
\subsection{ipsum}
..
\section{Bla bla bla}
\end{document}
答案1
addtohook
包选项bookmark
允许根据某些参数(例如书签级别)更改书签的设置。因此,以下方法可以解决问题:
\bookmarksetup{
numbered,
addtohook={%
\ifnum\bookmarkget{level}>1 %
\bookmarksetup{numbered=false}%
\fi
},
}
不幸的选择numbered
被评估前中的钩子被执行,参见包中\bookmark
的定义。因此,下面的示例改变了选项的效果:\Hy@writebookmark
bookmark
numbered
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[atend]{bookmark}
\bookmarksetup{
open,
openlevel=2,
numbered,
addtohook={%
\ifnum\bookmarkget{level}>1 %
\DisableBookmarkNumbering
\fi
},
}
\makeatletter
\newcommand*{\DisableBookmarkNumbering}{%
\let\numberline\@gobble
% The followings are not needed for subsections and below
% \let\booknumberline\@gobble
% \let\partnumberline\@gobble
% \let\chapternumberline\@gobble
}
\makeatother
\begin{document}
\section{Bla bla}
...
\subsection{Lorem}
...
\subsection{ipsum}
..
\section{Bla bla bla}
\end{document}
答案2
您可以使用以下开关:
\documentclass{article}
\usepackage[
open,
openlevel=2,
atend,
numbered
]{bookmark}
\makeatletter
\newcommand{\numbersections}{\renewcommand{\Hy@numberline}[1]{##1. }}
\newcommand{\nonumbersections}{\renewcommand{\Hy@numberline}[1]{}}
\makeatother
\begin{document}
\tableofcontents
\numbersections
\section{Bla bla}
...
\nonumbersections
\subsection{Lorem}
...
\subsection{ipsum}
..
\numbersections
\section{Bla bla bla}
\end{document}
\numbersections
打开书签中的编号,而\nonumbersections
关闭书签中的编号。
通过在序言中添加以下内容,可以自动执行上述过程:
\let\oldsection\section
\renewcommand{\section}{\numbersections\oldsection}
\let\oldsubsection\subsection
\renewcommand{\subsection}{\nonumbersections\oldsubsection}
\section
它在使用或之前插入所需的开关\subsection
。当然, 不需要它\subsubsection
,因为它们必然属于\subsection
(其中书签编号已通过 停用),而没有 的\nonumbersections
话似乎很奇怪。\subsubsection
\subsection
答案3
对内核宏进行了一些修改;我重新定义\@sect
为写\xnumberline{<level>}
而不是\numberline
,但定义\xnumberline
为忽略其第一个参数并执行\numberline
。
我还修补了\Hy@writebookmark
重新定义,\xnumberline
以便\xnumberline{<level>}
变成\stas@numberline@<level>
;然后定义这样的命令来执行我们喜欢的操作:\stas@numberline@section
打印数字,后跟句点和空格,而\stas@numberline@subsection
忽略数字。
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{etoolbox}
\usepackage[
open,
openlevel=2,
atend,
numbered
]{bookmark}
\makeatletter
\patchcmd{\H@old@sect} % \@sect becomes \H@old@sect
{\numberline}
{\xnumberline{#1}}
{}{}
\def\xnumberline#1{\numberline} % for the TOC
\patchcmd{\Hy@writebookmark}
{\let\numberline}
{\let\xnumberline\stas@numberline\let\numberline}
{}{}
\patchcmd{\Hy@writebookmark}
{\else\let\numberline}
{\else\let\xnumberline\@gobbletwo\let\numberline}
{}{}
\def\stas@numberline#1{\csname stas@numberline@#1\endcsname}
\def\stas@numberline@section#1{#1. }
\let\stas@numberline@subsection\@gobble
\let\stas@numberline@subsubsection\@gobble
\makeatother
\begin{document}
\section{Bla bla}
...
\subsection{Lorem}
...
\subsection{ipsum}
..
\section{Bla bla bla}
\end{document}