[Tex/LaTex] Coloring the subsections and subsubsections with different colors in toc

colortable of contents

Is there any possibility to color the subsections and subsubsection with any other two colors rather than blue, red and green say ?!.
enter image description here

I use

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage[svgnames]{xcolor}
\geometry{footskip=15pt}
\usepackage{amsmath}
\hypersetup{
colorlinks=true,
linkcolor=blue,
filecolor=magenta,      
urlcolor=red,  
}
\usepackage{tocloft}

\begin{document}
\tableofcontents

\end{document}

Best Answer

In your example blue is the main link colour (hyperref option linkcolor). You can change the link colour for the ToC entries of different levels:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{hyperref}
\usepackage[svgnames]{xcolor}
\geometry{footskip=15pt}
\usepackage{amsmath}

\usepackage{tocloft}
\renewcommand{\cftsubsecfont}{\hypersetup{linkcolor=green}}% you can replace green by the defined colour you want (see `xcolor` manual)
\renewcommand{\cftsubsubsecfont}{\hypersetup{linkcolor=red}}% you can replace red by the defined colour you want (see `xcolor` manual)

\hypersetup{%
    colorlinks=true,% switch on coloured instead of framed links
    linkcolor=blue,% main link color (e.g. for the ToC)
    filecolor=magenta,% color of links to external files
    urlcolor=red,% color to external URLs
}                           %

\usepackage{mwe}% for the example only
\begin{document}
\tableofcontents

\blinddocument

\end{document}

To be true: This is a hack. But it works:

Table of Contents with different link colours

Related Question