[Tex/LaTex] How to add section/subsection numbers to pdf bookmarks

bookmarkshyperrefnumberingsectioning

I'm writing up solutions to some exercises, and having problems with the bookmarks produced in the resulting pdf file. In the document I'm using \section (slightly modified) and \exercise (based on \subsection) – I'd like these to appear in the bookmarks in exactly the same way they appear in the document, including the numbering. Without the numbering, \exercise appears as a blank bookmark…

I know I could use the optional parameter for the \section and \exercise commands to manually specify the text used in the bookmarks, but I'd like to avoid it!

Code – main file

\documentclass[answers]{exam}

\addtolength{\textwidth}{1cm}
\addtolength{\hoffset}{-0.5cm}
\addtolength{\textheight}{1cm}
\addtolength{\voffset}{-0.5cm}

\usepackage{xcolor} % Use colour!
\usepackage{titlesec} % Allow creation of new sectioning commands
\usepackage{amsmath} % Needed for splitting equations over multiple lines
\usepackage{ulem} % Underlining effects
\usepackage{environ} % Tinkering with environments
\usepackage{gensymb} % Degree symbol
\usepackage[pdfstartview=FitH,bookmarks=true]{hyperref} 
\usepackage{bookmark} %PDF Bookmarks

\titleformat{\section}{\normalfont\large\bfseries}{Chapter }{0em}{\thesection: }

\titleclass{\exercise}{straight}[\section]  % Create new 'section' command for exercises - this replaces \subsection
\newcounter{exercise}
\titleformat{\exercise}{\normalfont\large\bfseries}{Exercise }{0em}{\theexercise}
\titlespacing*{\exercise}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\exerciseautorefname}{exercise}
\makeatletter
\providecommand*{\toclevel@exercise}{2} % Make sure exercises appear below sections in bookmarks
\makeatother

\pagestyle{headandfoot} % Let's have both
\header{\oddeven{Exercise \theexercise}{Chapter \thesection: \rightmark}}{Core 1}{\oddeven{Chapter \thesection: \rightmark}{Exercise \theexercise}}
\headrule
\footer{\oddeven{}{\thepage}}{}{\oddeven{\thepage}{}}
\footrule

\renewcommand\sectionmark[1]{\markright{#1}} % Set up \rightmark

\begin{document}

\unframedsolutions % Print solutions as plain text
\SolutionEmphasis{\color{blue}} % Print solutions in blue
\renewcommand{\thepartno}{\roman{partno}} % Format part numbers as lower-case roman numerals
\renewcommand\theexercise{\thesection\Alph{exercise}} % Print exercise numbers as 1A, etc...
\renewcommand{\solutiontitle}{\noindent} % Format solution - just print solution as entered
\newcommand{\ans}[1]{\uuline{#1}} % Double underline final answer

\NewEnviron{sol}{ % Align solutions sensibly
    \begin{solution}
        $\begin{aligned}
            \BODY
        \end{aligned}$
    \end{solution}
}

\include{Chapter1}

\end{document}

Code – Chapter1.tex

\section{Basic Algebra}

\exercise{}
Some text...

Best Answer

Bookmark are numbered with option numbered of package bookmark:

\usepackage{bookmark}
\bookmarksetup{numbered}

Of without bookmark with hyperref only:

\hypersetup{bookmarksnumbered}

Adding prefixes Chapter and Exercise can be done via a trick:

\renewcommand*{\thesection}{%
  \texorpdfstring{}{Chapter }%
  \arabic{section}%
  \texorpdfstring{}{:}%
}

\renewcommand*{\theexercise}{%
  \texorpdfstring{}{Exercise }%
  \arabic{section}%
  \Alph{exercise}%
}