3

I want to add more indent of section and subsection in table of contents, align with the name of the chapter,

enter image description here into this enter image description here

This is my code

\documentclass[a5paper,11pt]{book}
\usepackage{newtxtext,newtxmath}
\usepackage[margin=1.5cm, inner=2.5cm]{geometry}
\usepackage{tocbasic}
\usepackage[bookmarks=true]{hyperref}% load as last package

\DeclareTOCStyleEntry[
linefill=\bfseries\TOCLineLeaderFill,
beforeskip=0pt,
entrynumberformat=\chapterprefixintoc,
dynnumwidth
]{tocline}{chapter}

\newcommand*\chapterprefixintoc[1]
{\MakeUppercase{\chaptername}~#1\enskip}


\begin{document}
    \cleardoublepage
    \phantomsection\addcontentsline{toc}{chapter}{CONTENTS}
    \tableofcontents

    \chapter{PENDAHULUAN}
    contents...
    \section{Latar Belakang}
    contents...
    \subsection{ABCD}
    contents
    \subsection{EFGH}
    ...
    \section{Rumusan Masalah}
    \vspace{0.3cm}
    blablabla
    \section{Tujuan Penulisan}
    \vspace{0.25cm}
    blablabla..

    \chapter{DASAR TEORI}
    contents...
    \chapter{PEMBAHASAN}
    contents...
    \chapter{PENUTUP}
    contents...
    \chapter*{BIBLIOGRAPHY}
    \addcontentsline{toc}{chapter}{BIBLIOGRAPHY}
    contents
\end{document}

How to do that?

  • 1
    What should happen at Chapter 10 or 100 or 100000? – TeXnician Aug 8 '18 at 13:03
1

You could use a fixed value for chapter numwidth and section indent in ToC:

\documentclass[a5paper,11pt]{book}
\usepackage{newtxtext,newtxmath}
\usepackage[margin=1.5cm, inner=2.5cm]{geometry}
\usepackage{tocbasic}
\usepackage[bookmarks=true]{hyperref}% load as last package

\newcommand*\chapternumwidthintoc{7em}
\newcommand*\chapterprefixintoc[1]
  {\MakeUppercase{\chaptername}~#1\enskip}
\DeclareTOCStyleEntry[
  linefill=\bfseries\TOCLineLeaderFill,
  beforeskip=0pt,
  entrynumberformat=\chapterprefixintoc,
  numwidth=\chapternumwidthintoc,
]{tocline}{chapter}
\DeclareTOCStyleEntry[
  indent=\chapternumwidthintoc
]{tocline}{section}
\DeclareTOCStyleEntry{tocline}{subsection}

\begin{document}
  \cleardoublepage
  \phantomsection\addcontentsline{toc}{chapter}{CONTENTS}
  \tableofcontents

  \chapter{PENDAHULUAN}
  contents...
  \section{Latar Belakang}
  contents...
  \subsection{ABCD}
  contents
  \subsection{EFGH}
  ...
  \section{Rumusan Masalah}
  \vspace{0.3cm}
  blablabla
  \section{Tujuan Penulisan}
  \vspace{0.25cm}
  blablabla..

  \chapter{DASAR TEORI}
  contents...
  \chapter{PEMBAHASAN}
  contents
  \chapter{PENUTUP}
  contents
  \chapter*{BIBLIOGRAPHY}
  \addcontentsline{toc}{chapter}{BIBLIOGRAPHY}
  contents
\end{document}

Result:

enter image description here


If you still want to use dynnumwidth for the chapter entry:

Warning: The following suggestion uses an internal command that can be changed or removed in the future. So the code can break in the future.

\documentclass[a5paper,11pt]{book}
\usepackage{newtxtext,newtxmath}
\usepackage[margin=1.5cm, inner=2.5cm]{geometry}
\usepackage{tocbasic}
\usepackage[bookmarks=true]{hyperref}% load as last package

\DeclareTOCStyleEntry[
linefill=\bfseries\TOCLineLeaderFill,
beforeskip=0pt,
entrynumberformat=\chapterprefixintoc,
dynnumwidth
]{tocline}{chapter}

\newcommand*\chapterprefixintoc[1]
{\MakeUppercase{\chaptername}~#1\enskip}

\makeatletter
\AtBeginDocument{%
  \DeclareTOCStyleEntry[%
    indent=\ifundefinedorrelax{scr@dte@chapter@lastmaxnumwidth}
      {1.5em}
      {\scr@dte@chapter@lastmaxnumwidth}%
  ]{tocline}{section}
  \DeclareTOCStyleEntry{tocline}{subsection}
}
\makeatother


\begin{document}
  \cleardoublepage
  \phantomsection\addcontentsline{toc}{chapter}{CONTENTS}
  \tableofcontents

  \chapter{PENDAHULUAN}
  contents...
  \section{Latar Belakang}
  contents...
  \subsection{ABCD}
  contents
  \subsection{EFGH}
  ...
  \section{Rumusan Masalah}
  \vspace{0.3cm}
  blablabla
  \section{Tujuan Penulisan}
  \vspace{0.25cm}
  blablabla..

  \chapter{DASAR TEORI}
  contents...
  \chapter{PEMBAHASAN}
  contents
  \chapter{PENUTUP}
  contents
  \chapter*{BIBLIOGRAPHY}
  \addcontentsline{toc}{chapter}{BIBLIOGRAPHY}
  contents
\end{document}

Run three times to get

enter image description here

Example with a greater chapter number:

enter image description here

2

The following uses tocloft for achieving the desired alignment (rather than tocbasic). However, the format has been kept the same and should be clear from the context.

enter image description here

\documentclass{book}

\usepackage{tocloft,lipsum}
\usepackage[bookmarks=true]{hyperref}% load as last package

\setlength{\cftbeforechapskip}{0pt}
\renewcommand{\cftchappresnum}{CHAPTER~}
\settowidth{\cftchapnumwidth}{\cftchapfont CHAPTER~XX}
\renewcommand{\cftchapleader}{\cftchapfont \cftdotfill{\cftsecdotsep}}
\setlength{\cftsecindent}{\cftchapnumwidth}
\setlength{\cftsubsecindent}{\dimexpr\cftsecindent+\cftsecnumwidth}

\begin{document}

\phantomsection\addcontentsline{toc}{chapter}{CONTENTS}
\tableofcontents

\chapter{PENDAHULUAN}\lipsum[1]
\section{Latar Belakang}\lipsum[2]
\subsection{ABCD}\lipsum[3]
\subsection{EFGH}\lipsum[4]
\section{Rumusan Masalah}\lipsum[5]
\section{Tujuan Penulisan}\lipsum[6]

\chapter{DASAR TEORI}\lipsum[7]
\chapter{PEMBAHASAN}\lipsum[8]
\chapter{PENUTUP}\lipsum[9]
\chapter*{BIBLIOGRAPHY}
\addcontentsline{toc}{chapter}{BIBLIOGRAPHY}\lipsum[10]

\end{document}
  • It's usually a good idea to load tocloft with the titles option. – egreg Aug 8 '18 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.