在 \addcontentsline breaks hyperref 中添加命令

在 \addcontentsline breaks hyperref 中添加命令

这是我的问题:默认情况下,我在文档中使用旧式数字,但对于标题的数字(在本例中为章节标题),我想使用内衬数字。

为此,我修改了两个命令:

\@seccntformat,我将其定义为:

\def\@seccntformat#1{{\liningNumbers\csname the#1\endcsname}\ }

我将\@sect其定义为:

\def\@sect#1#2#3#4#5#6[#7]#8{%
    \ifnum #2>\c@secnumdepth%
        \let\@svsec\@empty%
    \else%
        \refstepcounter{#1}%
        \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \fi%
    \@tempskipa #5\relax%
    \ifdim \@tempskipa>\z@%
        \begingroup%
            #6{%
                \@hangfrom{\hskip #3\relax\@svsec}%
                \interlinepenalty \@M #8\@@par%
            }%
        \endgroup%
        \csname #1mark\endcsname{#7}%
        \addcontentsline{toc}{#1}{%
            \ifnum #2>\c@secnumdepth%
            \else%
                \protect{{\protect\liningNumbers\protect\numberline{\csname the#1\endcsname}}}% Changed line: added "\liningNumbers" to change section numbers
            \fi%
            #7%
        }%
    \else%
    \def\@svsechd{%
        #6{%
            \hskip #3\relax%
            \@svsec #8%
        }%
        \csname #1mark\endcsname{#7}%
        \addcontentsline{toc}{#1}{%
            \ifnum #2>\c@secnumdepth \else%
                \protect{{\protect\liningNumbers\protect\numberline{\csname the#1\endcsname}}}% Changed line: added "\liningNumbers" to change section numbers
            \fi%
            #7%
        }%
    }%
    \fi%
    \@xsect{#5}%
}

其中仅更改了两行代码以在命令\liningNumbers内添加命令\addcontentsline

显示正确,章节号也显示正确。我没有重新定义命令\thesection来包含\liningNumbers命令,因为当我在文本中引用章节号时,最好使用旧式数字来书写。

当我使用该包时,就会出现问题hyperref。使用\liningNumbers内部会改变PDF 文档中各部分的名称\addcontentsline方式。如下所示:hyperref

在此处输入图片描述

我有这个警告:

Package hyperref Warning: Token not allowed in a PDF string (Unicode): removing `\addfontfeatures' on input line XX.

有没有解决的办法?

以下是 MWE:

\documentclass{book}
\usepackage{fontspec, lipsum, hyperref}
\setmainfont{LibertinusSerif}[Numbers={Proportional, OldStyle}]

\newcommand{\liningNumbers}{\addfontfeatures{Numbers={Lining}}}

\makeatletter
\renewcommand\thechapter{\@Roman\c@chapter}
\renewcommand\thesection{\@arabic\c@section}
\renewcommand\thesubsection{\@alph\c@subsection}
\def\@seccntformat#1{{\liningNumbers\csname the#1\endcsname}\ }% Formatting the section number, added : "\liningNumbers"

\def\@sect#1#2#3#4#5#6[#7]#8{%
    \ifnum #2>\c@secnumdepth%
        \let\@svsec\@empty%
    \else%
        \refstepcounter{#1}%
        \protected@edef\@svsec{\@seccntformat{#1}\relax}%
    \fi%
    \@tempskipa #5\relax%
    \ifdim \@tempskipa>\z@%
        \begingroup%
            #6{%
                \@hangfrom{\hskip #3\relax\@svsec}%
                \interlinepenalty \@M #8\@@par%
            }%
        \endgroup%
        \csname #1mark\endcsname{#7}%
        \addcontentsline{toc}{#1}{%
            \ifnum #2>\c@secnumdepth%
            \else%
                \protect{{\protect\liningNumbers\protect\numberline{\csname the#1\endcsname}}}% Changed line: added "\liningNumbers" to change section numbers
            \fi%
            #7%
        }%
    \else%
    \def\@svsechd{%
        #6{%
            \hskip #3\relax%
            \@svsec #8%
        }%
        \csname #1mark\endcsname{#7}%
        \addcontentsline{toc}{#1}{%
            \ifnum #2>\c@secnumdepth \else%
                \protect{{\protect\liningNumbers\protect\numberline{\csname the#1\endcsname}}}% Changed line: added "\liningNumbers" to change section numbers
            \fi%
            #7%
        }%
    }%
    \fi%
    \@xsect{#5}%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{Chapter one}
    \section{Section I.1}
        \subsection{Subsection I.1.a}
            \lipsum[1-2]
        \subsection{Subsection I.1.b}
            \lipsum[1-2]
    \section{Section I.2}
        \subsection{Subsection I.2.a}
            \lipsum[1-2]
        \subsection{Subsection I.2.b}
            \lipsum[1-2]

\end{document}

答案1

您可以定义命令,以便它在书签和文本中工作,借助\texorpdfstring

\newcommand{\liningNumbers}{\texorpdfstring{\addfontfeatures{Numbers={Lining}}}{}}

如果你不确定 hyperref 是否已加载,你可以确保\texorpdfstring已使用以下方式定义:

\providecommand\texorpdfstring[2]{#1}

相关内容