TransWikia.com

Create a table of contents with links in all text and different colours for sections and page number using pdflatex

TeX - LaTeX Asked on October 4, 2021

Introduction

I would like to create a table of contents with links in all text and page number but with different colours for the text and page numbers and it should compile with pdflatex.

Work done

I have the (minimal) working code below.

documentclass[11pt, letterpaper, openany, oneside]{book}

usepackage{sectsty}
usepackage{titlesec}
usepackage{color}

usepackage[bookmarks,hypertexnames=false,debug,linktocpage=true,hidelinks]{hyperref}

hypersetup{
    colorlinks,
    linktoc=all,
    linkcolor={blue},
    citecolor={blue},
    urlcolor={blue}
}



begin{document}

titleformat{chapter}{color{red}normalfontHugebfseries}{thechapter}{1em}{}[]
sectionfont{color{red}}
subsectionfont{color{red}}

tableofcontents

chapter{Chapter One}
section{A Section}
Text goes here.
section{Another Section}
Text goes here.
subsection{A Sub Section}
Text goes here.
section{One more section Section}
Text goes here.

chapter{Chapter One}
Text goes here.
section{A Section}
Text goes here.
section{Another Section}
Text goes here.

end{document}

This gives the following output for the table of contents:

enter image description here

It also gives the following output for a chapter

enter image description here

Desired outcome

I would like to have a table of contents where all text (chapter headings, section headings, subsection headings, page number etc.) are hyperlinks to the relevant section but have customized colours for the section headings, subsection headings and page numbers in the table of contents.

It should also compile with pdflatex (not xelatex, lualatex etc.).

Here is a summary of the desired output:

enter image description here

One Answer

Here I patch the contentsline command to locally change the link color used in title part of toc entry (compared to the page number part), per toc type.

In the following example,

  • the title part of chapter toc entry is changed to red by deftoccolor@chapter{red}, and
  • the title part of section and subsection toc entries are changed to black by deftoccolor@section{black} and deftoccolor@subsection{black}, respectively.

To apply this for other types of toc entries,

  1. find the corresponding auxiliary file. For example, listoftables uses aux file .lot.
  2. find the type of toc entry this list uses. For example, in .lot file you can see lines like contentsline {table}{numberline {1}{ignorespaces Title}}{1}%, then table (the first argument of contentsline) is the type name.
  3. provide corresponding toccolor@<type> command. In the previous example, with deftoccolor@table{<color>}, the color of title part of toc entries table will typeset in <color>.
documentclass[11pt, letterpaper, openany, oneside]{book}

usepackage{sectsty}
usepackage{titlesec}
usepackage{color}
usepackage{xpatch}

usepackage[bookmarks,hypertexnames=false,debug,linktocpage=true,hidelinks]{hyperref}

hypersetup{
    colorlinks,
    linktoc=all,
    linkcolor={blue},
    citecolor={blue},
    urlcolor={blue}
}


titleformat{chapter}
  {color{red}normalfontHugebfseries}
  {thechapter}{1em}
  {}[]
sectionfont{color{red}}
subsectionfont{color{red}}


makeatletter
% redefine contentsline, locally change linkcolor
xpatchcmdcontentsline
  {%
    csname l@#1endcsname{%
      hyper@linkstart{link}{Hy@tocdestname}{#2}hyper@linkend
    }{%
      hyper@linkstart{link}{Hy@tocdestname}{#3}hyper@linkend
    }%
  }
  {%
    csname l@#1endcsname{%
      begingroup
        Hy@toccolor{#1}%
        hyper@linkstart{link}{Hy@tocdestname}{#2}hyper@linkend
      endgroup
    }{%
      hyper@linkstart{link}{Hy@tocdestname}{#3}hyper@linkend
    }%
  }
  {}{fail}

% choose toc linkcolor, #1 is first arg of contetnsline, 
% eg "chapter", "section"
defHy@toccolor#1{%
  ifcsname toccolor@#1endcsname
    edef@linkcolor{csname toccolor@#1endcsname}%
  fi
}

deftoccolor@chapter{red}
deftoccolor@section{black}
deftoccolor@subsection{black}
makeatother

begin{document}
tableofcontents

chapter{Chapter One}
section{A Section}
Text goes here.
section{Another Section}
Text goes here.
subsection{A Sub Section}
Text goes here.
section{One more section Section}
Text goes here.

chapter{Chapter One}
Text goes here.
section{A Section}
Text goes here.
section{Another Section}
Text goes here.

end{document}

enter image description here

Correct answer by muzimuzhi Z on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP