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:
It also gives the following output for a chapter
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:
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,
red
by deftoccolor@chapter{red}
, andblack
by deftoccolor@section{black}
and deftoccolor@subsection{black}
, respectively.To apply this for other types of toc entries,
listoftables
uses aux file .lot
..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.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}
Correct answer by muzimuzhi Z on October 4, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP