TransWikia.com

Including "Chapter" before each section number in the ToC? (Article Class)

TeX - LaTeX Asked on January 30, 2021

Basically, here’s my MWE, and I need to include “Chapter” before every section title in the ToC (without changing anything else).

   documentclass[12pt]{article}
usepackage[utf8]{inputenc} 
usepackage[english]{babel}
usepackage[titles]{tocloft}
usepackage{titlesec}
usepackage{titletoc}

addtocaptionsenglish{ renewcommand*contentsname{centerline{TABLE OF CONTENTS}}}

%renewcommand{thesection}{CHAPTER}
%renewcommand{cftsecpresnum}{Chapter thesection}
titleformat{section}[display]%
{nullvskip1emsffamilybfseriesfilcenter}{Chapter thesection}{0em}{}[]
titlespacing*{section}{0em}{1em}{1em}

titlecontents{section}
[1.5em] % ie, 1.5em (chapter) + 2.3em
{}
{contentslabel{1em}}
{hspace*{-1.5em}}
{titlerule*[0.5pc]{.}contentspage}

begin{document}
newpage
tableofcontents
newpage

section{Section One}
section{Section Two}
Content
end{document}

The commented out code is what I’ve already tried. The first one just changes the number to “CHAPTER” and doesn’t move the text in the ToC (so it just overlays on top of the name of the section), and if I include “thesection” after “CHAPTER” it creates an infinite loop definition error. The second one causes no differences, and the code still compiles.

Thanks!

2 Answers

Here is a solution. I didn't load tocloft, since I use titletoc. Also I added a formatting for thoe unnumbered sections which should appear in the table of contents:

 documentclass[12pt]{article}
usepackage[utf8]{inputenc}
usepackage[english]{babel}
usepackage[showframe]{geometry}
usepackage{titlesec}
usepackage{titletoc}

addtocaptionsenglish{ renewcommand*contentsname{centerline{TABLE OF CONTENTS}}}

titleformat{section}[display]%
{nullvskip1emsffamilybfseriesfilcenter}{Chapter thesection}{0em}{}[]
titleformat{name = section, numberless}[block]%
{nullvskip1emsffamilybfseriesfilcenter}{}{0em}{}
titlespacing*{section}{0em}{1em}{1em}

titlecontents{section}
[7em] % ie, width of contentslabel + 0.5em
{medskip}
{contentslabel[MakeUppercasechaptername~thecontentslabel]{6.5em}}%thecontentslabel
{hspace*{-6.5em}}
{titlerule*[0.5pc]{.}contentspage}

begin{document}

tableofcontents
newpage

section*{An Unnumbered Section}
addcontentsline{toc}{section}{An Unnumbered Section}
section{Section One}
subsection{A first subsection}
section{Section Two}
Content

end{document} 

enter image description here

Answered by Bernard on January 30, 2021

I am editing a book using tstextbook, which includes use of package titletoc.

Solutions given in this thread did not helped me. Because of package titlesec introduce too many error in my compilation, resulting in a bigger problems that that I was trying to solve. The same case when I tried to test solutions that use package tocloft.

After some hours researching and testing, my solution is simple: just by modifying the of the titlecontents command for "chapter".

My original entry:

{color{tssteelblue}contentslabel[Largethecontentslabel]{1.25cm}} %ORIGINAL

A working solution:

{chaptername thecontentslabel:quad}% <numbered-entry-format> %It compiled within my code without errors!!!

My final solution, that solved my requirement:

{color{tssteelblue}Largechaptername thecontentslabel.space} %Final solution

*Please consider that my book is in Spanish.

My final complete code for chapter:

%titlecontents{ <section> }[ <left> ]
%{ <above-code> }
%{ <numbered-entry-format> }{ <numberless-entry-format> }
%{ <filler-page-format> }[ <below-code> ]

titlecontents{chapter}[0cm]
{addvspace{14pt}largesffamilybfseries}
{color{tssteelblue}Largechaptername thecontentslabel.space} %Final solution
{}
{color{tssteelblue}normalsizesffamilybfseries;titlerule*[.5pc]{.};thecontentspage}

TOC fragments: before and after the solution

My solution given from here

Answered by Gengis Toledo Ramírez on January 30, 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