[Tex/LaTex] Hyperref table of contents

hyperrefnumberingtable of contents

I have a problem with the numbering of my appendix. This is my MWE:

\documentclass[oneside, openright, 12pt]{book}
\sloppy

\usepackage[plainpages=false,pdfpagelabels]{hyperref}
\hypersetup{
    pdftitle={title},
    pdfsubject={subject},
    pdfauthor={author},
    pdfborder={0 0 0}
}

\usepackage[titles]{tocloft}
\newcommand\tocentry[1]{\addcontentsline{toc}{chapter}{#1}}

\begin{document}
    \pagenumbering{Alph}

    \pagenumbering{roman}
    \setcounter{page}{1}
    \tableofcontents

    \listoffigures
    \tocentry{Figures}

    \pagenumbering{arabic}
    \chapter{chapter1}
    dsfdsfdsfsdf
    \section{section1} 
    dsafdsaffdasfdasfffadsfffadsff
    \section{section2} 
    gfsdcsdfdsf
    \section{section3}
    dsfdsfdsf

    \tocentry{Appendix}
    \chapter*{appendix}
    \section{appendix1}
    \section{appendix2}

    \bibliographystyle{alpha}
    \bibliography{mwe/lit} 
    \tocentry{Literature}

\end{document}

As you can see in the following screenshot the numbering in my appendix simply continues from the last chapter.
wrong numbering
I want it to be A, B,… I figured out that \chapter*{} does not increment the necessary counters – there are many similar questions on stackoverflow… I fixed my problem like this:

\setcounter{chapter}{\thechapter+1}
\setcounter{section}{0}
\renewcommand{\thesection}{\Alph{section}}  
\chapter*{appendix}

It solved my problem.
correct numbering
But now, I've observed another problem with my Hyperref table of contents within the pdf viewer: My appendix and literature are placed as a subsection of chapter1->section3.
wrong numbering in pdf contents

Does anyone know how to solve this problem? Thanks in advance 🙂

Best Answer

If you really want to add your appendices as sections instead chapters (I do not recommend this) you can use:

\usepackage{xpatch}
\xapptocmd\appendix
  {\chapter*{Appendix}\addcontentsline{toc}{chapter}{Appendix}\renewcommand\thesection{\Alph{section}.}}
  {}{\PatchFailed}

and

\appendix
\section{appendix1}
\section{appendix2}

Additional remarks:

Use package tocbibind for the ToC entrys of the lists and the bibliography to ensure the correct page numbers in the table of contents.

Use \cleardoublepage before \pagenumbering{...}.

\documentclass[oneside, openright, 12pt]{book}
\usepackage[nottoc]{tocbibind}% add a ToC entry for lists, index and bibliography

\usepackage{xpatch}
\xapptocmd\appendix
  {\chapter*{Appendix}\addcontentsline{toc}{chapter}{Appendix}\renewcommand\thesection{\Alph{section}.}}
  {}{\PatchFailed}

\usepackage[plainpages=false,pdfpagelabels]{hyperref}
\hypersetup{
    pdftitle={title},
    pdfsubject={subject},
    pdfauthor={author},
    pdfborder={0 0 0}
}

\begin{document}
\pagenumbering{roman}
\tableofcontents
\listoffigures

\cleardoublepage% <-must be added before \pagenumbering
\pagenumbering{arabic}
\chapter{chapter1}
Text
\section{section1} 
Text
\section{section2} 
Text
\section{section3}
Text

\appendix
\section{appendix1}
\section{appendix2}

\bibliographystyle{alpha}
\bibliography{mwe/lit} 
\end{document}

Result:

enter image description here

enter image description here