TransWikia.com

How can I include unnumbered chapters in a custom toc?

TeX - LaTeX Asked by THEWOLF SNOW on August 9, 2021

I’m trying to implement a custom toc in my Latex file and I would like to include the unnumbered chapter Third Chapter.

By the way, in the unnumbered chapter I would like to have the same structure but instead of having chapter 1 or 2 I would like to have just an empty rectangle with the same colour.

enter image description here

documentclass[10pt,a4paper,openany]{book}

usepackage[most]{tcolorbox}

usepackage{tocloft}

definecolor{darkmidnightblue}{rgb}{0.0, 0.2, 0.4}

newtcbox{chapternumberbox}[1][]{%
  colback=darkmidnightblue!50!darkmidnightblue,
  colupper={white},
  colframe={white},
  fontupper={bfserieslarge},
  valign=center,
  size=small,
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

newtcolorbox{chapternamebox}[1][]{%
  enhanced jigsaw,
  valign=center,
  interior style={left color={darkmidnightblue!30!darkmidnightblue},right color={darkmidnightblue!90!darkmidnightblue}},
  colframe={white},
  boxrule={1pt},
  colupper={white},
  size=small,
  fontupper={bfserieslarge},
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

newcommand{mychapternamebox}[3][]{%
  begin{chapternamebox}[#1]
    #2hfill#3%
  end{chapternamebox}%
}

usepackage{xpatch}

makeatletter
xpatchcmd{@chapter}{%
  addcontentsline{toc}{chapter}%
  {protectnumberline{thechapter}#1}%
}{%
  addtocontents{toc}{protectcftpagenumbersoff{chapter}}
  addcontentsline{toc}{chapter}{protectchapternumberbox{protectchaptername{} thechapter}protectmychapternamebox[width={9.8cm}]{#1}{thepage}}%%
  addtocontents{toc}{protectcftpagenumberson{chapter}}%
}{}{}

makeatother

begin{document}
tableofcontents

chapter{First Chapter}

section{First Section}
section{Second Section}

chapter{Second Chapter}
section{First Section}
section{Second Section}

chapter*{Third Chapter}
%cleardoublepage
addcontentsline{toc}{chapter}{protectnumberline{Second Chapter}}

end{document}

One Answer

documentclass[10pt,a4paper,openany]{book}

usepackage[most]{tcolorbox}

usepackage{tocloft}

definecolor{darkmidnightblue}{rgb}{0.0, 0.2, 0.4}

newtcbox{chapternumberbox}[1][]{%
  colback=darkmidnightblue!50!darkmidnightblue,
  colupper={white},
  colframe={white},
  fontupper={bfserieslarge},
  valign=center,
  size=small,
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

newtcolorbox{chapternamebox}[1][]{%
  enhanced jigsaw,
  valign=center,
  interior style={left color={darkmidnightblue!30!darkmidnightblue},right color={darkmidnightblue!90!darkmidnightblue}},
  colframe={white},
  boxrule={1pt},
  colupper={white},
  size=small,
  fontupper={bfserieslarge},
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

newcommand{mychapternamebox}[3][]{%
  begin{chapternamebox}[#1]
    #2hfill#3%
  end{chapternamebox}%
}

usepackage{xpatch}

makeatletter
xpatchcmd{@chapter}{%
  addcontentsline{toc}{chapter}%
  {protectnumberline{thechapter}#1}%
}{%
  addtocontents{toc}{protectcftpagenumbersoff{chapter}}
  addcontentsline{toc}{chapter}{protectchapternumberbox{protectchaptername{} thechapter}protectmychapternamebox[width={9.8cm}]{#1}{thepage}}%%
  addtocontents{toc}{protectcftpagenumberson{chapter}}%
}{}{}

makeatother

begin{document}
tableofcontents

chapter{First Chapter}

section{First Section}
section{Second Section}

chapter{Second Chapter}
section{First Section}
section{Second Section}

chapter*{Third Chapter}
%cleardoublepage
  addtocontents{toc}{protectcftpagenumbersoff{chapter}}
  addcontentsline{toc}{chapter}{protectchapternumberbox{hphantom{Chapter 2}}protectmychapternamebox[width={9.8cm}]{Third Chapter}{thepage}}%%
  addtocontents{toc}{protectcftpagenumberson{chapter}}%

end{document}

enter image description here

Correct answer by samcarter_is_at_topanswers.xyz on August 9, 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