[Tex/LaTex] Adding ToC line manually within appendix

table of contents

I'm trying to manually add the chapters of my appendix to the ToC, but the result is horrible. The page number is placed directly next to the chapter name, instead of using the regular format where the page number aligns right.

My code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{geometry}
\usepackage[toc,page,title]{appendix}

\begin{document}
\tableofcontents
\section{Week 1 - Intro \textcolor{blue}{\textbf{R}}}
\begin{appendices}
\addcontentsline{toc}{chapter}{Appendix A} 
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\chapter{\large{\textbf{Appendix A - \textcolor{blue}{R} commando's}}} \\
\chapter{\large{\textbf{Appendix B - Begrippenlijst}}}
\end{appendices}
\end{document}

Screenshot of the code:

Screenshot

So what I want is to have the "regular" ToC format when adding these chapters to the ToC. All manuals I can find offer no explanation as to how the \addcontentsline command works, but I'm almost certain the solution lies in that command, specifically in the "entry" part of the command (third argument).

Best Answer

You are using article class and using \chapter which doesn't look correct. Use \section instead.

\documentclass{article}
\usepackage{xcolor}
%\usepackage{geometry}
\usepackage[toc,page,title]{appendix}

\begin{document}
\tableofcontents
\section{Week 1 - Intro \textcolor{blue}{\textbf{R}}}
\begin{appendices}
%\addcontentsline{toc}{section}{Appendix A}
%\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\section{Appendix A - \protect\textcolor{blue}{R} commando's}
\section{Appendix B - Begrippenlijst}
\end{appendices}
\end{document}

enter image description here

I have removed extra commands from the section arguments and modified. Note the \protect since \textcolor is fragile..

Further, you need not add Appendix A in the section title. Use titletoc option for appendix

\documentclass{article}
\usepackage{xcolor}
%\usepackage{geometry}
\usepackage[titletoc,page,title]{appendix}   %% note titletoc

\begin{document}
\tableofcontents
\section{Week 1 - Intro \textcolor{blue}{\textbf{R}}}
\begin{appendices}
%\addcontentsline{toc}{section}{Appendix A}
%\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\section{\protect\textcolor{blue}{R} commando's}
\section{Begrippenlijst}
\end{appendices}
\end{document}

enter image description here