[Tex/LaTex] How to fix moderncv’s ToC generation to get nesting in the PDF ToC

hyperrefmoderncvsectioningtable of contents

I am preparing a (somewhat lengthly) application using the moderncv class. For this I would like to provide for some additional structure in the PDFs ToC, that is, use something like \addcontentsline{toc}{part}{<entry>} for grouping sections. Also I would like to have the subsections to appear nested below the sections in the PDF ToC.

However, the following fails: All bookmarks appear on the part-level, \subsection does not create a bookmark:

\documentclass[11pt,a4paper,sans]{moderncv} 
\moderncvstyle{casual}                        % style options are 'casual' (default) and 'classic' 
\moderncvcolor{blue}                          % color options 'blue' (default), 'orange', 'green', 'red', 'purple', 'grey' and 'black'
\usepackage[utf8]{inputenc}                   % replace by the encoding you are using
\usepackage[scale=0.75]{geometry}
\firstname{John}
\familyname{Doe}
\begin{document}
\maketitle

\phantomsection{}
\addcontentsline{toc}{part}{Academic Career}

\section{Education}
\cventry{year--year}{Degree}{Institution}{City}{\textit{Grade}}{Description}  % arguments 3 to 6 can be left empty
\cventry{year--year}{Degree}{Institution}{City}{\textit{Grade}}{Description}

\subsection{Master thesis}
\cvitem{title}{\emph{Title}}
\cvitem{supervisors}{Supervisors}
\cvitem{description}{Short thesis abstract}

\subsection{Dissertation}
\cvitem{title}{\emph{Title}}
\cvitem{supervisors}{Supervisors}
\cvitem{description}{Short thesis abstract}

\phantomsection{}
\addcontentsline{toc}{part}{Professional Career}

\section{Experience}
\subsection{Vocational}
\cventry{year--year}{Job title}{Employer}{City}{}{General description no longer than 1--2 lines.\newline{}%
Detailed achievements:%
\begin{itemize}%
\item Achievement 1;
\item Achievement 3.
\end{itemize}}
\cventry{year--year}{Job title}{Employer}{City}{}{Description line 1\newline{}Description line 2}
\subsection{Miscellaneous}
\cventry{year--year}{Job title}{Employer}{City}{}{Description}

\section{Languages}
\cvitemwithcomment{Language 1}{Skill level}{Comment}
\cvitemwithcomment{Language 2}{Skill level}{Comment}
\cvitemwithcomment{Language 3}{Skill level}{Comment}

\end{document}

screen shot

The culprit can be found in the moderncv implementation, more precisely in the definitions of the \section and \subsection commands in moderncvstyleclassic.sty:

\ProvidesPackage{moderncvstyleclassic}[2012/01/25 v0.17 modern curriculum vitae style scheme: classic]
...
\renewcommand*{\section}[1]{%
  \vspace*{2.5ex}%
  \parbox[t]{\hintscolumnwidth}{\strut\raggedleft\raisebox{\baseletterheight}{\color{color1}\rule{\hintscolumnwidth}{0.95ex}}}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{part}{#1}%
  \hspace{\separatorcolumnwidth}%
  \parbox[t]{\maincolumnwidth}{\strut\sectionstyle{#1}}%
  \par\nobreak\vskip 1ex\@afterheading}% to avoid a pagebreak after the heading

\renewcommand*{\subsection}[1]{%
  \begin{tabular}{@{}p{\hintscolumnwidth}@{\hspace{\separatorcolumnwidth}}p{\maincolumnwidth}@{}}%
    \raggedleft\hintfont{} &{\subsectionstyle{#1}}%
  \end{tabular}%
  \par\nobreak\vskip 0.5ex\@afterheading}% to avoid a pagebreak after the heading
%  \phantomsection{}% reset the anchor for hyperrefs
%  \addcontentsline{toc}{chapter}{#1}% does not work, the bookmark is placed at the same level as sections (placed themselves at part level to be visible, as hyperref does not allow sections without parents...)

The comment below the \subsection implementation probably explains the problem: sections are placed on part-level, subsections are not placed at all in the ToC because of issues with hyperref. I played a bit around with various \addcontentsline-settings, but without success.

So how can this be fixed?

Best Answer

Well I would redefine \section and \subsection so that they use the correct levels (no idea why moderncv doesn't do this):

\documentclass[11pt,a4paper,sans]{moderncv}
\moderncvstyle{casual}                        % style options are 'casual' (default) and 'classic'
\moderncvcolor{blue}                          % color options 'blue' (default), 'orange', 'green', 'red', 'purple', 'grey' and 'black'
\usepackage[utf8]{inputenc}                   % replace by the encoding you are using
\usepackage[scale=0.75]{geometry}
\firstname{John}
\familyname{Doe}

\makeatletter
\AtEndPreamble{\hypersetup{bookmarksdepth=2}}% to get sections and subsection in toc

\renewcommand*{\section}[1]{%
  \vspace*{2.5ex}%
  \parbox[t]{\hintscolumnwidth}{\strut\raggedleft\raisebox{\baseletterheight}{\color{color1}\rule{\hintscolumnwidth}{0.95ex}}}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{section}{#1}% changed
  \hspace{\separatorcolumnwidth}%
  \parbox[t]{\maincolumnwidth}{\strut\sectionstyle{#1}}%
  \par\nobreak\vskip 1ex\@afterheading}% to avoid a pagebreak after the heading

\renewcommand*{\subsection}[1]{%
  \phantomsection %added
  \addcontentsline{toc}{subsection}{#1}%added
  \begin{tabular}{@{}p{\hintscolumnwidth}@{\hspace{\separatorcolumnwidth}}p{\maincolumnwidth}@{}}%
      \raggedleft\hintfont{} &{\subsectionstyle{#1}}%
  \end{tabular}%
  \par\nobreak\vskip 0.5ex\@afterheading}

\makeatother  
\begin{document}
.......