4

here is my problem: by default I use oldstyle numbers in my document, but for the numbers of the headings (in this case the section headings), I would like to use lining numbers.

To do this, I have modified two commands:

\@seccntformat, which I define as:

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

and \@sect, which I define as :

\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}%
}

where only two lines of code have been changed to add a \liningNumbers command within a \addcontentsline command.

The display is correct and the section number is displayed correctly. I don't redefine the \thesection command to include a \liningNumbers command because when I refer to a section number in the text, it is preferable that the number be written with oldstyle numbers.

The problem occurs when I use the hyperref package. Using \liningNumbers inside \addcontentsline changes the way hyperref names sections in the PDF document. Like this :

enter image description here

And I have this warning :

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

Is there a way around this?

Here is a 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}
2
  • 1
    \newcommand{\liningNumbers}{\texorpdfstring{\addfontfeatures{Numbers={Lining}}}{}} Aug 19, 2021 at 10:53
  • @UlrikeFischer thank you very much, could you put this as an answer, so I can accept it?
    – B Legrand
    Aug 19, 2021 at 11:05

1 Answer 1

5

You can define your command so that it works in bookmarks and in text with the help of \texorpdfstring

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

If you are unsure if hyperref is loaded or not you can make sure that \texorpdfstring is defined with

\providecommand\texorpdfstring[2]{#1}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .