# pdfbookmark is not working if i used secnumdepth and \MakeUppercase

I would like to control numbered, unnumbered section through the \setcounter{secnumdepth}{5} command.

My requirement by default all the section heading should be unnumbered. So i will set default value \setcounter{secnumdepth}{0}. If i give the \numbered command all the head levels should be numbered. So i should create the command \newcommand{\numbered}{\setcounter{secnumdepth}{5}}

Another requirement is section levels should be uppercase. So i am using the command \MakeUppercase command.

But the problem is raised below mentioned cases (BOOKMARK LINK NOT WORKING):

At the same time i am using two commands \setcounter{secnumdepth}{0} and \MakeUppercase, pdfbookmark is not working.

Case 1. \setcounter{secnumdepth}{0} and \MakeUppercase both commands used - pdfbookmark is not working.

Case 2. \setcounter{secnumdepth}{5} and \MakeUppercase both commands used - pdfbookmark is working.

I don't know first case is not worked. second case is worked. Please advice.

Alternatively i am using below mentioned pdfstring command. But this command is not working

\pdfstringdefDisableCommands{%
\let\MakeUppercase\relax%
}


MWE:

\documentclass{article}

\usepackage{hyperref}
\usepackage{lipsum}

\setcounter{secnumdepth}{0}

\makeatletter
\newcommand{\numbered}{\setcounter{secnumdepth}{5}}
\makeatother

\hypersetup{%
citecolor=gray,%
bookmarks=true,
bookmarksopen=true,%
bookmarksnumbered=true,%
pdftitle={\@mytitle},%
pdfauthor={Maria Luisa Di Vona},%
pdfkeywords={},%
pdfsubject={}%
}

\pdfstringdefDisableCommands{%
\let\MakeUppercase\relax%
}

\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-2.8ex}%
{8.25pt}%
{\raggedright\MakeUppercase}}
\makeatother

\begin{document}
\title{This is the sample title}
\author{Steven}

\maketitle

\section{This is the sample section one $\alpha$}

\lipsum[1-5]
\section{Second section}

\lipsum[5-10]

\section{Second section}
\lipsum[4]
\lipsum[5]

\section{This is the sample section two}

\lipsum[1]
\lipsum[1]

\end{document}


\MakeUppercase also converts the name of the destination for the section to uppercase. The result is that the bookmark has the wrong link target. The following example fixes it by moving \MakeUppercase to \Sectionformat, a macro introduced by package nameref, which is usually loaded in \begin{document}. The example loads the package earlier to redefine \SectionFormat. The first argument is the title, the second argument the level number.

Another issues:

• When package bookmark is loaded, the bookmarks are faster updated.
• \alpha is supported by Unicode bookmarks (option unicode or pdfencoding=auto) together with option psdextra to add lots of math symbols.
• The \@mytitle misses \makeatletter. Also it is undefined, therefore the example has directly put the title in \pdftitle. Also a macro can be defined, which is later used in option pdftitle and command \title.

Full example:

\documentclass{article}

\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{lipsum}

\setcounter{secnumdepth}{0}

\hypersetup{%
citecolor=gray,%
bookmarksopen=true,%
bookmarksnumbered=true,%
pdfencoding=auto,%
psdextra,%
pdftitle={This is a sample title},%
pdfauthor={Maria Luisa Di Vona},%
pdfkeywords={},%
pdfsubject={}%
}

\pdfstringdefDisableCommands{%
\let\MakeUppercase\relax%
}

\usepackage{nameref}
\makeatletter
\renewcommand{\Sectionformat}[2]{%
\ifnum#2=1 %
\expandafter\MakeUppercase
\else
\expandafter\@firstofone
\fi
{#1}%
}
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-2.8ex}%
{8.25pt}%
{\raggedright}}
\makeatother

\begin{document}
\title{This is the sample title}
\author{Steven}

\maketitle

\section{This is the sample section one $\alpha$}

\lipsum[1-5]
\section{Second section}

\lipsum[5-10]

\section{Second section}
\lipsum[4]
\lipsum[5]

\section{This is the sample section two}

\lipsum[1]
\lipsum[1]
\end{document}

• Shouldn't \MakeUppercase be made into \@firstofone rather than \relax in \pdfstringdefDisableCommands? – egreg Oct 8 '15 at 20:39
• @egreg Maybe a bit faster as \@firstofone, but the curly braces will be removed anyway. – Heiko Oberdiek Oct 8 '15 at 20:58
• @HeikoOberdiek Great Job. I am happy about your solution. I have an doubt for this solution. What is the usage of \tracingmacros=1, \tracingmacros=1 and \expandafter commands? – Vetri Oct 9 '15 at 6:21
• @Vetti The \tracingmacros settings are just leftovers from testing. If \tracingmacros is larger than zero, then the macro expansions are written to the .log file. – Heiko Oberdiek Oct 9 '15 at 16:03
• @Vetri The \expandafter remove the \else branch and the \fi token, thus that \MakeUppercase and \@firstofone will see the intended argument as their argument {#1}. – Heiko Oberdiek Oct 9 '15 at 16:04