书签问题

书签问题

我在网上找到了这段代码,它允许使用\level{n}{title}命令使用无限的部分级别。

\makeatletter
\newcommand\level[1]{%
    \ifcase#1\relax\expandafter\chapter\or
        \expandafter\section\or
        \expandafter\subsection\or
        \expandafter\subsubsection\else
        \def\next{\@level{#1}}\expandafter\next
    \fi}
\newcommand{\@level}[1]{%
    \@startsection{level#1}%
        {#1}%
        {\z@}%
        {-3.25ex\@plus -1ex \@minus -.2ex}%
        {1.5ex \@plus .2ex}%
        {\normalfont\normalsize\bfseries}}

\newdimen\@numsdim
\newdimen\@dotsdim
{\normalfont\normalsize
    \sbox\z@{0}\global\@numsdim=\wd\z@
    \sbox\z@{.}\global\@dotsdim=\wd\z@
}

\newdimen\@numindent
\newdimen\@textindent
\setlength{\@numindent}{15pt}
\setlength{\@textindent}{15pt}


\newcounter{level4}[subsubsection]
\@namedef{thelevel4}{\thesubsubsection.\arabic{level4}}
\@namedef{level4mark}#1{}
%\def\l@section{\@dottedtocline{1}{\dimexpr\@numindent*0\relax}{\dimexpr\@dotsdim*0+\@numsdim*1+\@textindent\relax}}
\def\l@subsection{\@dottedtocline{2}{\dimexpr\@numindent*1\relax}{\dimexpr\@dotsdim*1+\@numsdim*2+\@textindent\relax}}
\def\l@subsubsection{\@dottedtocline{3}{\dimexpr\@numindent*2\relax}{\dimexpr\@dotsdim*2+\@numsdim*3+\@textindent\relax}}
\@namedef{l@level4}{\@dottedtocline{4}{\dimexpr\@numindent*3\relax}{\dimexpr\@dotsdim*3+\@numsdim*4+\@textindent\relax}}

\count@=4
\def\@ncp#1{\number\numexpr\count@+#1\relax}
\loop\ifnum\count@<100
    \begingroup\edef\x{\endgroup
        \noexpand\newcounter{level\@ncp{1}}[level\number\count@]
        \noexpand\@namedef{thelevel\@ncp{1}}{%
            \noexpand\@nameuse{thelevel\@ncp{0}}.\noexpand\arabic{level\@ncp{1}}}
        \noexpand\@namedef{level\@ncp{1}mark}####1{}%
        \noexpand\@namedef{l@level\@ncp{1}}%
            {\noexpand\@dottedtocline%
                {\@ncp{1}}%
                {\dimexpr\@numindent*\@ncp{0}\relax}%
                {\the\dimexpr\@dotsdim*\@ncp{0}+\@numsdim*\@ncp{1}+\@textindent\relax}}}%
    \x
    \advance\count@\@ne
\repeat
\makeatother

它工作得很好,但超过 3 级的书签被视为部分。以下是示例: 错误书签示例

绿色的是正确的 ( \level{1}{Preventivo}),因为它被视为一个部分。红色的则是错误的 ( level{4}{Suddivisione lavoro}),因为它也被视为一个部分。

关于如何解决这个问题您有什么想法吗?

答案1

您应该会收到一个警告:

Package hyperref Warning: bookmark level for unknown level4 defaults to 0.

这意味着未知的部分级别(level4,,level5...)将被视为值 0(chapter)。

附加书签级别可以按以下方式定义:

\@namedef{toclevel@level4}{4}
\@namedef{toclevel@level5}{5}
...

或者在你的循环内:

\expandafter\edef\csname toclevel@level\the\count@\endcsname{\the\count@}%

其他值得关注的:

  • hyperrefbookmarksdepth增加书签层次深度的选项。
  • 4 级和 5 级通常是\paragraph\subparagraph
  • bookmark建议使用软件包。更多选项,更少 LaTeX 运行,...

测试文件:

\documentclass{report}

\makeatletter
\newcommand\level[1]{%
    \ifcase#1\relax\expandafter\chapter\or
        \expandafter\section\or
        \expandafter\subsection\or
        \expandafter\subsubsection\else
        \def\next{\@level{#1}}\expandafter\next
    \fi}
\newcommand{\@level}[1]{%
    \@startsection{level#1}%
        {#1}%
        {\z@}%
        {-3.25ex\@plus -1ex \@minus -.2ex}%
        {1.5ex \@plus .2ex}%
        {\normalfont\normalsize\bfseries}}

\newdimen\@numsdim
\newdimen\@dotsdim
{\normalfont\normalsize
    \sbox\z@{0}\global\@numsdim=\wd\z@
    \sbox\z@{.}\global\@dotsdim=\wd\z@
}

\newdimen\@numindent
\newdimen\@textindent
\setlength{\@numindent}{15pt}
\setlength{\@textindent}{15pt}


\newcounter{level4}[subsubsection]
\@namedef{thelevel4}{\thesubsubsection.\arabic{level4}}
\@namedef{level4mark}#1{}
%\def\l@section{\@dottedtocline{1}{\dimexpr\@numindent*0\relax}{\dimexpr\@dotsdim*0+\@numsdim*1+\@textindent\relax}}
\def\l@subsection{\@dottedtocline{2}{\dimexpr\@numindent*1\relax}{\dimexpr\@dotsdim*1+\@numsdim*2+\@textindent\relax}}
\def\l@subsubsection{\@dottedtocline{3}{\dimexpr\@numindent*2\relax}{\dimexpr\@dotsdim*2+\@numsdim*3+\@textindent\relax}}
\@namedef{l@level4}{\@dottedtocline{4}{\dimexpr\@numindent*3\relax}{\dimexpr\@dotsdim*3+\@numsdim*4+\@textindent\relax}}

\count@=4
\def\@ncp#1{\number\numexpr\count@+#1\relax}
\loop\ifnum\count@<100
    \begingroup\edef\x{\endgroup
        \noexpand\newcounter{level\@ncp{1}}[level\number\count@]
        \noexpand\@namedef{thelevel\@ncp{1}}{%
        \noexpand\@nameuse{thelevel\@ncp{0}}.\noexpand\arabic{level\@ncp{1}}}
        \noexpand\@namedef{level\@ncp{1}mark}####1{}%
        \noexpand\@namedef{l@level\@ncp{1}}%
            {\noexpand\@dottedtocline%
                {\@ncp{1}}%
                {\dimexpr\@numindent*\@ncp{0}\relax}%
                {\the\dimexpr\@dotsdim*\@ncp{0}+\@numsdim*\@ncp{1}+\@textindent\relax}}}%
    \x
    \expandafter\edef\csname toclevel@level\the\count@\endcsname{\the\count@}%
    \advance\count@\@ne
\repeat
\makeatother

\usepackage{hyperref}
\usepackage{bookmark}
\bookmarksetup{
  open,
  depth=8,
  numbered,
}
\setcounter{secnumdepth}{8}
\setcounter{tocdepth}{8}

\begin{document}
\tableofcontents
\level0{Chapter A}
\level1{Section B}
\level1{Section C}
\level2{Subsection D}
\level2{Subsection E}
\level3{Subsubsection F}
\level3{Subsubsection G}
\level4{Level 4 H}
\level4{Level 4 I}
\level5{Level 5 J}
\level5{Level 5 K}
\end{document}

结果

相关内容