LaTeX forum ⇒ Text FormattingCustomizing the ToC with toclof

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
User avatar
Cham
Posts: 926
Joined: Sat Apr 02, 2011 4:06 pm

Customizing the ToC with toclof

Postby Cham » Tue Nov 26, 2019 10:18 pm

I'm having lots of troubles customizing my very large ToC, using the toclof package. Here's a MWE example that shows what I'm trying to achieve. Currently, the spacings are done by eyes only, and I don't understand well the commands used in that code, even after trial and mistakes:
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \RequirePackage[l2tabu, orthodox]{nag}
  2. \documentclass[11pt,letterpaper,twoside]{book}
  3. \usepackage[T1]{fontenc}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage[french]{babel}
  6. \usepackage{lmodern}
  7. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  8.  
  9. \usepackage{tocloft}
  10. \renewcommand{\cftdotsep}{2} % Points spacement ...
  11. \makeatletter % To fix a small offset with page number.
  12. \renewcommand{\@pnumwidth}{2.3em} % {1.75em}
  13. %\renewcommand{\@tocrmarg}{2.75em} % Useless command?
  14. \makeatother
  15. \setcounter{tocdepth}{3} % Add sub-sub-sections to ToC.
  16.  
  17. % Chapter spacement:
  18. \renewcommand{\cftchappresnum}{\chaptername\ }
  19. \renewcommand{\cftchapaftersnumb}{\newline}
  20. \setlength{\cftchapnumwidth}{0pt} % Spacement to left of chapter number.
  21.  
  22. % Sections spacement:
  23. %\renewcommand{\cftsecpresnum}{\hfill} %
  24. %\renewcommand{\cftsecaftersnum}{\hspace*{10pt}} %
  25. %\setlength{\cftsecindent}{18pt} %
  26. %\setlength{\cftsecnumwidth}{36pt} %
  27. \addtolength{\cftsecnumwidth}{10pt} %
  28.  
  29. % Sub-sections spacement:
  30. %\renewcommand{\cftsubsecpresnum}{\hfill} %
  31. %\renewcommand{\cftsubsecaftersnum}{\hspace*{10pt}} %
  32. %\setlength{\cftsubsecnumwidth}{44pt} %
  33. \setlength{\cftsubsecindent}{54pt} %
  34. \addtolength{\cftsubsecnumwidth}{12pt} %
  35.  
  36. % Sub-sub-sections spacement:
  37. \renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize} % Subsubsection style.
  38. \setlength{\cftsubsubsecindent}{98pt} %
  39. %\setlength{\cftsubsubsecnumwidth}{0pt} %
  40.  
  41. \begin{document}
  42.  
  43. \tableofcontents
  44.  
  45. \mainmatter
  46. \setcounter{chapter}{16}
  47.  
  48. \chapter{Chapter title}
  49. \setcounter{section}{22}
  50. \section{Section title}
  51. \setcounter{subsection}{32}
  52. \subsection{Subsection title}
  53. \subsubsection{Title of a subsubsection}
  54. \subsection{Subsection title}
  55. \subsection{Subsection title}
  56. \subsection{Subsection title}
  57. \subsection{Subsection title}
  58. \subsection{Subsection title}
  59. \setcounter{chapter}{9}
  60. \setcounter{section}{8}
  61. \section{Section title}
  62. \subsection{Subsection title}
  63. \subsubsection{Title of a subsubsection}
  64. \subsection{Subsection title}
  65. \subsection{Subsection title}
  66. \subsection{Subsection title}
  67. \subsection{Subsection title}
  68. \subsection{Subsection title}
  69.  
  70. \end{document}


Here's what it gives:
ToC.jpg
ToC.jpg (88.27 KiB) Viewed 142 times


I would like to get the subsubsections titles to be exactly aligned with their subsection title. Currently, the spacement shown above is done by eye and isn't exact.

Also, the section number should have a constant space with its title. Currently, I'm unable to get this.

And lastly, I would like to have the subsection number to be left aligned with its section title, and have a constant spacing on its right and its title.

How can I achieve that?

User avatar
Cham
Posts: 926
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Tue Nov 26, 2019 10:53 pm

I think I got it, but I need a confirmation that's a proper way of doing this:
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \RequirePackage[l2tabu, orthodox]{nag}
  2. \documentclass[11pt,letterpaper,twoside]{book}
  3. \usepackage[T1]{fontenc}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage[french]{babel}
  6. \usepackage{lmodern}
  7. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  8.  
  9. \usepackage{tocloft}
  10. \renewcommand{\cftdotsep}{2} % Points spacement ...
  11. \makeatletter % To fix a small offset with page number.
  12. \renewcommand{\@pnumwidth}{2.3em} % {1.75em}
  13. %\renewcommand{\@tocrmarg}{2.75em} % Useless command?
  14. \makeatother
  15. \setcounter{tocdepth}{3} % Add sub-sub-sections to ToC.
  16.  
  17. % Chapter spacement:
  18. \renewcommand{\cftchappresnum}{\chaptername\ }
  19. \renewcommand{\cftchapaftersnumb}{\newline}
  20. \setlength{\cftchapindent}{0pt} % Spacement to left of chapter number.
  21. \setlength{\cftchapnumwidth}{0pt} %
  22.  
  23. % Sections spacement:
  24. %\renewcommand{\cftsecpresnum}{\hfill} %
  25. %\renewcommand{\cftsecaftersnum}{\hfill} %
  26. \setlength{\cftsecindent}{24pt} % Spacement to left of section number.
  27. \setlength{\cftsecnumwidth}{32pt} %
  28. %\addtolength{\cftsecnumwidth}{12pt} %
  29.  
  30. % Sub-sections spacement:
  31. %\renewcommand{\cftsubsecpresnum}{\hfill} %
  32. %\renewcommand{\cftsubsecaftersnum}{\hfill} %
  33. \setlength{\cftsubsecindent}{56pt} % Spacement to left of subsection number.
  34. \setlength{\cftsubsecnumwidth}{48pt} %
  35. %\addtolength{\cftsubsecnumwidth}{12pt} %
  36.  
  37. % Sub-sub-sections spacement:
  38. \renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize} % Subsubsection style.
  39. \setlength{\cftsubsubsecindent}{104pt} % Spacement to left of subsubsection number.
  40.  
  41. \begin{document}
  42.  
  43. \tableofcontents
  44.  
  45. \mainmatter
  46. \setcounter{chapter}{16}
  47.  
  48. \chapter{Chapter title}
  49. \setcounter{section}{22}
  50. \section{Section title}
  51. \setcounter{subsection}{32}
  52. \subsection{Subsection title}
  53. \subsubsection{Title of a subsubsection}
  54. \subsection{Subsection title}
  55. \subsection{Subsection title}
  56. \subsection{Subsection title}
  57. \subsection{Subsection title}
  58. \subsection{Subsection title}
  59. \setcounter{chapter}{9}
  60. \setcounter{section}{8}
  61. \section{Section title}
  62. \subsection{Subsection title}
  63. \subsubsection{Title of a subsubsection}
  64. \subsection{Subsection title}
  65. \subsection{Subsection title}
  66. \subsection{Subsection title}
  67. \subsection{Subsection title}
  68. \subsection{Subsection title}
  69.  
  70. \end{document}


Here's what it gives:
ToC2.jpg
ToC2.jpg (85.7 KiB) Viewed 137 times


Would it be better to move all the numbers at a constant space from its title instead? Currently, I don't knbow how to do that! :oops:

rais
Posts: 250
Joined: Sun Nov 16, 2014 8:51 pm

Postby rais » Sat Nov 30, 2019 1:04 am

Well, if I would try s.th. like that, I'd use the parent's values to create the indent for the current entry, e.g., \cftsecindent+\cftsecnumwidth for \cftsubsecindent. Should you later decide on, say, a different indent for section entries, you'll just need to change it in one place (and not several places, as in your original).
I modified your sample code for fixed distance number/entry; if that's better or worse I leave up to you. :D
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \RequirePackage[l2tabu, orthodox]{nag}
  2. \documentclass[11pt,letterpaper,twoside]{book}
  3. \usepackage[T1]{fontenc}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage[french]{babel}
  6. \usepackage{lmodern}
  7. \usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
  8. \usepackage{xcolor}
  9. \usepackage{tocloft}
  10. \renewcommand{\cftdotsep}{2} % Points spacement ...
  11. \makeatletter % To fix a small offset with page number.
  12. \renewcommand{\@pnumwidth}{2.3em} % {1.75em}
  13. %\renewcommand{\@tocrmarg}{2.75em} % Useless command? <-- see \cftsetrmarg in tocloft's doc
  14.  
  15. %\renewcommand{\numberline}[1]{% tocloft's definition
  16. % \hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
  17. \renewcommand{\numberline}[1]{%<-- modified to allow for \raggedleft in \cft*presnum
  18. \ifdim\@tempdima>0pt
  19. \parbox{\@tempdima}{\@cftbsnum #1\@cftasnum}%
  20. \else
  21. \makebox[0pt][l]{\@cftbsnum #1\@cftasnum}% hide the overflow of the number's box from LaTeX
  22. % please note that this may lead to overprinting (title over number), if \cft*aftersnumb doesn't contain
  23. % enough space---or a line break
  24. \fi
  25. \@cftasnumb
  26. }
  27. \makeatother
  28. \newlength\ntsepwidth
  29. \setlength\ntsepwidth{10pt}
  30. \newcommand*\ntsep{\rule{\ntsepwidth}{0pt}}%<-- number/title separator
  31.  
  32. \setcounter{tocdepth}{3} % Add sub-sub-sections to ToC.
  33.  
  34. % Chapter spacement:
  35. \renewcommand{\cftchappresnum}{\chaptername\ }
  36. \renewcommand{\cftchapaftersnumb}{\newline}
  37. \setlength{\cftchapindent}{0pt} % Spacement to left of chapter number.
  38. \setlength{\cftchapnumwidth}{0pt}%
  39.  
  40. % Sections spacement:
  41. \renewcommand{\cftsecpresnum}{\raggedleft}%
  42. \renewcommand{\cftsecaftersnum}{\ntsep} %
  43. \setlength{\cftsecindent}{24pt} % Spacement to left of section number.
  44. %\setlength{\cftsecnumwidth}{32pt} %
  45. \settowidth\cftsecnumwidth{12.34\ntsep}
  46.  
  47. %\addtolength{\cftsecnumwidth}{12pt} %
  48.  
  49. % Sub-sections spacement:
  50. \renewcommand{\cftsubsecpresnum}{\raggedleft} %
  51. \renewcommand{\cftsubsecaftersnum}{\ntsep} %
  52. \setlength{\cftsubsecindent}{\dimexpr\cftsecindent+\cftsecnumwidth} % Spacement to left of subsection number.
  53. %\setlength{\cftsubsecnumwidth}{48pt} %
  54. \settowidth\cftsubsecnumwidth{12.34.56\ntsep}
  55. %\addtolength{\cftsubsecnumwidth}{12pt} %
  56.  
  57. % Sub-sub-sections spacement:
  58. \renewcommand{\cftsubsubsecfont}{\normalfont\sffamily\footnotesize} % Subsubsection style.
  59. \setlength{\cftsubsubsecindent}{\dimexpr\cftsubsecindent+\cftsubsecnumwidth} % Spacement to left of subsubsection number.
  60.  
  61. \begin{document}
  62.  
  63. \tableofcontents
  64.  
  65. \mainmatter
  66.  
  67. \addtocontents{toc}{\makebox[0pt][l]{\raisebox{0pt}[0pt][0pt]{% my `calipers' for the TOC
  68. % omit this block for final output
  69. \rule{\cftsecindent}{0.5pt}%
  70. {\color{blue!30}\rule[-200pt]{0.4pt}{200pt}}%
  71. \rule{\dimexpr\cftsecnumwidth-0.4pt-\ntsepwidth\relax}{0.5pt}%
  72. % \ntsep is set inside the box for the number; the sarting point for this line is shifted
  73. % horizontally already by the thickness of the (prev.) vertical line---the 0.4pt
  74. {\color{blue!30}\rule[-200pt]{0.4pt}{200pt}}%
  75. \rule{\dimexpr\ntsepwidth-0.4pt\relax}{0.5pt}%
  76. {\color{blue!30}\rule[-200pt]{0.4pt}{200pt}}%
  77. \rule{\dimexpr\cftsubsecnumwidth-0.4pt-\ntsepwidth\relax}{0.5pt}%
  78. {\color{red!30}\rule[-200pt]{0.4pt}{200pt}}%
  79. \rule{\dimexpr\ntsepwidth-0.4pt\relax}{0.5pt}%
  80. {\color{red!30}\rule[-200pt]{0.4pt}{200pt}}%
  81. }}}
  82.  
  83. \section{Section title}
  84. \subsection{Subsection title}
  85. \subsubsection{Title of a subsubsection}
  86. \subsection{A somewhat longer subsection title, just to see what happens---or what does not happen}
  87.  
  88. \setcounter{chapter}{16}
  89.  
  90. \chapter{Chapter title}
  91. \setcounter{section}{22}
  92. \section{Section title}
  93. \setcounter{subsection}{32}
  94. \subsection{Subsection title}
  95. \subsubsection{Title of a subsubsection}
  96. \subsection{Subsection title}
  97. \end{document}

KR
Rainer


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 8 guests