6

I am experiencing a trouble when compiling my PDF file. I am using MiKTeX 2.9 and Texmaker (PDFLaTeX) to get the file.

I have the following MWE :

\documentclass[11pt]{article}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm]{geometry}
\usepackage[frenchb, american]{babel}
\usepackage[
colorlinks=true,
linkcolor=black,
filecolor=black,
urlcolor=black,
citecolor=black,
bookmarks=true,
bookmarksopen=true,
bookmarksnumbered=true]{hyperref}

\begin{document}
\tableofcontents

\section{Introduction}
\section{blabla}

\appendix
\renewcommand\appendixname{Annexes}

\section*{Annexes}
\phantomsection
\addcontentsline{toc}{section}{\appendixname}
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

\makeatletter
\def\@seccntformat#1{Annexe~\csname the#1\endcsname~-~}
\makeatother

\renewcommand{\thesection}{\arabic{section}}

\section{blabla}
\section{blabla}
\section{blabla}
\section{blabla}
\section{blabla}

\end{document}

When compiling the file, the TOC is perfect but the bookmarks in the PDF are dropped. I know that this issue is coming from that line :

\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}

It seems that this line is dropping all entries for bookmarks because when I run this code, the .out file is empty. However, when I drop this line, the .out file is all right but subsequent sections after Annexes are shown both in the TOC and bookmarks

I tried several things but I can't figure out a code that could work. My objective is to have only the entry Annexes in both the TOC and bookmarks, without having the subsequent sections (I need those numbered sections in my text).

Does someone is able to help me?

Thanks

2
  • 1
    Welcome to TeX.SX!. With other words: The regular sections should appear, Annexes should appear, but not the blabla sections?
    – user31729
    Jun 17, 2016 at 15:09
  • Thanks! Yes, this is exactly what I'm looking for Jun 17, 2016 at 15:12

2 Answers 2

5

The tocdepth must be set again to 1, lately at the end of the document. This can be done either manually or in the \AtEndDocument hook. This must be written to the .toc.

In addition the tocdepth counter must be set to 0 before using the various blabla sections.

\documentclass[11pt]{article}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[frenchb, american]{babel}
\usepackage[
colorlinks=true,
linkcolor=black,
filecolor=black,
urlcolor=black,
citecolor=black,
bookmarks=true,
bookmarksopen=true,
bookmarksnumbered=true]{hyperref}
%\usepackage{bookmark}

\AtEndDocument{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
}

\begin{document}
\tableofcontents

\section{Introduction}
\section{blabla}

\appendix
\renewcommand\appendixname{Annexes}


\phantomsection
\addcontentsline{toc}{section}{\appendixname}
\section*{Annexes}



\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
\makeatletter

\def\@seccntformat#1{Annexe~\csname the#1\endcsname~-~}

\makeatother


\setcounter{tocdepth}{0}

\renewcommand{\thesection}{\arabic{section}}

\section{blabla}
\section{blabla}
\section{blabla}
\section{blabla}
\section{blabla}



\end{document}

enter image description here

2
  • That's really perfect! Thanks for this quick answer! Jun 17, 2016 at 15:24
  • @LucasGuichard: You're welcome. Happy TeXing!
    – user31729
    Jun 17, 2016 at 15:28
0

Just to include a simpler variation of the solution. A \setcounter{tocdepth}{1} can be included just after the line \addtocontents{toc}{\protect\setcounter{tocdepth}{0}}.

That is,

...    
\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
    
\setcounter{tocdepth}{1} % New line
    
\makeatletter
...

produces the same result as above, with the bookmarks created in the PDF.

You must log in to answer this question.

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