LaTeX forum ⇒ Page LayoutProblem with table of contents Topic is solved

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
ciema7
Posts: 21
Joined: Sat Nov 10, 2018 2:31 am

Problem with table of contents

Postby ciema7 » Thu May 02, 2019 6:15 pm

Hello,

I have some problems with my ToC. Is there a simply way to align better long sections names ? I add photo, which ilustrates my problem (section 2).
Attachments
question.png
question.png (26.77 KiB) Viewed 841 times

User avatar
Johannes_B
Site Moderator
Posts: 4044
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Thu May 02, 2019 9:19 pm

This completely depends on the class you are using and maybe packages used to customize chapter headings. Can you show a minimal working example?
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

ciema7
Posts: 21
Joined: Sat Nov 10, 2018 2:31 am

Postby ciema7 » Wed May 08, 2019 1:44 pm

Thanks for reply! I present my code below (it is long because its my master thesis code, maybe the problem is with these 4 rows started with \tolerance=1 ?):

  1. \documentclass[fontsize=12pt]{article}
  2. \usepackage[table,xcdraw]{xcolor}
  3. \usepackage{listings}
  4. \usepackage{inputenc}
  5. \usepackage{graphicx}
  6. \usepackage{polski}
  7. \usepackage{geometry}
  8. \usepackage{textcomp}
  9. \usepackage{gensymb}
  10. \usepackage{parskip}
  11. \usepackage{multirow}
  12. \usepackage{tabularx}
  13. \usepackage{enumitem}
  14. \usepackage{booktabs}
  15. \usepackage{array}
  16. \usepackage{caption}
  17. \usepackage{amsmath}
  18. \usepackage{scrextend}
  19. \usepackage{mwe}
  20. \usepackage[export]{adjustbox} % do wycentrowania obrazka w tabeli w pionie
  21. \usepackage{setspace}
  22. \usepackage{stanli}
  23. \usepackage{tikz}
  24. \usepackage{wrapfig}
  25. \usepackage{hyperref}
  26. \usepackage[numbers,sort&compress]{natbib}
  27. \usepackage[numbered,framed]{matlab-prettifier}
  28. \usepackage{pdfpages}
  29. %\usepackage{titlesec}
  30. \usepackage{placeins}
  31. \usepackage{bm}
  32. \usepackage{float}
  33. %\usepackage{newtxmath}
  34. % \usepackage{fontspec}
  35. % \setmainfont{Times New Roman}
  36. %\usepackage[none]{hyphenat}
  37. %\titlelabel{\thetitle.\quad}
  38.  
  39. \pagenumbering{arabic}
  40. \usepackage{leading}
  41. \leading{18pt}
  42.  
  43.  
  44.  
  45. \tolerance=1
  46. \emergencystretch=\maxdimen
  47. \hyphenpenalty=10000
  48. \hbadness=10000
  49.  
  50.  
  51. \captionsetup[figure]{font=footnotesize,labelfont=footnotesize}
  52.  
  53.  
  54. \lstset{language=matlab,basicstyle=\footnotesize,showstringspaces=false,style = Matlab-editor,basicstyle=\mlttfamily,
  55. escapechar= ",mlshowsectionrules = true}
  56.  
  57.  
  58.  
  59. \graphicspath{ {figures/} }
  60. \begin{document}
  61.  
  62. \newgeometry{tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm}
  63.  
  64.  
  65.  
  66. \renewcommand{\thesection}{\arabic{section}.} % tworzy lub usuwa kropki przy rozdzialach (reguluje)
  67. \numberwithin{equation}{section} % tworzy lub usuwa kropki przy rozdzialach (reguluje)
  68. \renewcommand{\theequation}{\thesection\arabic{equation}}
  69. \renewcommand{\thesubsection}{\thesection\arabic{subsection}}
  70.  
  71. \vspace{0.6cm}
  72. \begin{centering}
  73. \tableofcontents
  74. \end{centering}
  75.  
  76.  
  77. \newpage
  78. \addtocontents{toc}{\protect\addvspace{20mm}}
  79. \section{Introduction is placed not so good in the table of contents, it is too long, it gets centered and some words go to the lower row}
  80.  
  81. \vspace{1cm}\hspace{1cm}These are my few words in the section.
  82.  
  83.  
  84. \end{document}
Last edited by Stefan Kottwitz on Wed May 08, 2019 4:15 pm, edited 1 time in total.
Reason: code marked

User avatar
Stefan Kottwitz
Site Admin
Posts: 9439
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Wed May 08, 2019 4:01 pm

Hello,

that centering here causes it:

  1. \begin{centering}
  2. \tableofcontents
  3. \end{centering}


Just change to
  1. \tableofcontents


and test again. Compile two times to update the table of contents.

I guess all your section headings are left aligned. In that case I would be consistent and let the contents (section) heading left aligned too. If you really would like to have this single heading of the contents to be centered, let us know, there would be a different way.

Stefan
Site admin

ciema7
Posts: 21
Joined: Sat Nov 10, 2018 2:31 am

Postby ciema7 » Wed May 08, 2019 4:20 pm

Thank you very much, it works, but here is my another question :) . Is there any simple way to not divide my last word in the any row in table of contest and not to throwing the part of the word to the next row (below), but whole this (divided) word throw to the next row?

PS. Stefan you can edit your previous post and delete this " } " from the second code, you provided :) Just formality. Thanks again for help!!!

User avatar
Stefan Kottwitz
Site Admin
Posts: 9439
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Wed May 08, 2019 4:39 pm

Here is a way:

  1. \usepackage{tocstyle}
  2. \usetocstyle{standard}
  3. \settocfeature{raggedhook}{\raggedright}



Stefan
Site admin

User avatar
Ijon Tichy
Posts: 91
Joined: Mon Dec 24, 2018 10:12 am

Postby Ijon Tichy » Sat May 11, 2019 2:00 pm

Usage of tocstyle is deprecated and not recommended. But you can get a similar result using:

  1. \usepackage{tocbasic}% One more KOMA-Script package added.
  2. % Using tocbasic to make the table of contents:
  3. \renewcommand*{\tableofcontents}{\listoftoc[\contentsname]{toc}}
  4. \DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}


Here added to the original code. Please have a look to my additional comments in this code:
  1. \documentclass[12pt]{article}% fontsize=12pt is a KOMA-Script option. To have
  2. % the same with article you need to use 12pt.
  3. \usepackage[table,xcdraw]{xcolor}
  4. \usepackage{listings}
  5. %\usepackage{inputenc}% inputenc without option doesn't make much sense and if
  6. % you are using an up-to-date TeX installation and UTF8
  7. % encoded input you even do not need this package.
  8. \usepackage{graphicx}
  9. \usepackage{polski}
  10. \usepackage{geometry}
  11. \usepackage{textcomp}
  12. \usepackage{gensymb}
  13. \usepackage{parskip}
  14. \usepackage{multirow}
  15. \usepackage{tabularx}
  16. \usepackage{enumitem}
  17. \usepackage{booktabs}
  18. %\usepackage{array}% Already loaded by tabularx
  19. \usepackage{caption}
  20. \usepackage{amsmath}
  21. \usepackage{scrextend}% Wouldn't it be a choice to use scrartcl instead of
  22. % article, if you want KOMA-Script features?
  23. \usepackage{mwe}
  24. \usepackage[export]{adjustbox} % do wycentrowania obrazka w tabeli w pionie
  25. \usepackage{setspace}% It does not make much sense to use this package
  26. % together with package leading!
  27. \usepackage{stanli}
  28. %\usepackage{tikz}% Already loaded by stanli
  29. \usepackage{wrapfig}
  30. \usepackage[numbers,sort&compress]{natbib}
  31. \usepackage[numbered,framed]{matlab-prettifier}
  32. \usepackage{pdfpages}
  33. %\usepackage{titlesec}
  34. \usepackage{placeins}
  35. \usepackage{bm}
  36. \usepackage{float}
  37. %\usepackage{newtxmath}
  38. % \usepackage{fontspec}
  39. % \setmainfont{Times New Roman}
  40. %\usepackage[none]{hyphenat}
  41. %\titlelabel{\thetitle.\quad}
  42. \usepackage{leading}
  43.  
  44. \usepackage{tocbasic}% One more KOMA-Script package added.
  45.  
  46. \usepackage{hyperref}% Must be loaded after float!!!!
  47. % Generally you should load hyperref after all other
  48. % packages but those for which the manual of either the
  49. % package or hyperref tells you, that you should load it
  50. % after hyperref.
  51. \usepackage{bookmark}% It is recommended to use this with hyperref, because it
  52. % improves generation of bookmarks.
  53.  
  54. % Using tocbasic to make the table of contents:
  55. \renewcommand*{\tableofcontents}{\listoftoc[\contentsname]{toc}}
  56. \DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}
  57.  
  58. %\pagenumbering{arabic}% Default
  59. \leading{18pt}
  60.  
  61. \tolerance=1
  62. \emergencystretch=\maxdimen
  63. \hyphenpenalty=10000% Really? Do not allow hyphenation?
  64. \hbadness=10000
  65.  
  66. \captionsetup[figure]{font=footnotesize,labelfont=footnotesize}
  67.  
  68. \lstset{language=matlab,basicstyle=\footnotesize,showstringspaces=false,style = Matlab-editor,basicstyle=\mlttfamily,
  69. escapechar= ",mlshowsectionrules = true}
  70.  
  71. \graphicspath{{figures/}}% Avoid whitespaces in \graphicspath, because the
  72. % manual does not specify them as tollerated (however
  73. % currently they are).
  74.  
  75.  
  76. % All global settings should be done in the document preamble!
  77. \geometry{tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm}
  78.  
  79. \renewcommand{\thesection}{\arabic{section}.} % tworzy lub usuwa kropki przy rozdzialach (reguluje)
  80. \numberwithin{equation}{section}% I would recommend to use \counterwithin
  81. % instead of \numberwithin, because since
  82. % LaTeX 2018/04/01 it is part of the LaTeX
  83. % kernel. With prior LaTeX kernels (e.g. TeX
  84. % Live 2017 from Overlead) you can load
  85. % package chngcntr to get \counterwithin.
  86. \renewcommand{\theequation}{\thesection\arabic{equation}}
  87. \renewcommand{\thesubsection}{\thesection\arabic{subsection}}
  88.  
  89. \begin{document}
  90.  
  91. \tableofcontents
  92.  
  93. \newpage
  94.  
  95. %\addtocontents{toc}{\protect\addvspace{20mm}}% Should not be needed.
  96. \section{Introduction is placed good in the table of contents, where the longtext-entry
  97. is raggedright using \textsf{tocbasic}}
  98.  
  99. \lipsum[1] % provided by package lipsum loaded by package mwe (which you are
  100. % already using to get dummy text)
  101.  
  102. \end{document}


BTW: A more minimal but working example for the problem would be:
  1. \documentclass[12pt]{article}% fontsize=12pt is a KOMA-Script option. To have
  2. % the same with article you need to use 12pt.
  3. \usepackage{mwe}% useful for minimal but working examples
  4.  
  5. \usepackage{tocbasic}% One more KOMA-Script package added.
  6.  
  7. \usepackage{hyperref}% Must be loaded after float!!!!
  8. % Generally you should load hyperref after all other
  9. % packages but those for which the manual of either the
  10. % package or hyperref tells you, that you should load it
  11. % after hyperref.
  12. \usepackage{bookmark}% It is recommended to use this with hyperref, because it
  13. % improves generation of bookmarks.
  14.  
  15. % Using tocbasic to make the table of contents:
  16. \renewcommand*{\tableofcontents}{\listoftoc[\contentsname]{toc}}
  17. \DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}
  18.  
  19. \begin{document}
  20.  
  21. \tableofcontents
  22.  
  23. \section[Introduction in the table of contents, where even this verylongtext-entry
  24. is raggedright using \textsf{tocbasic}]{Introduction}
  25.  
  26. \lipsum[1] % provided by package lipsum loaded by package mwe (which you are
  27. % already using to get dummy text)
  28.  
  29. \end{document}

or using KOMA-Script class scrartcl:
  1. \documentclass[fonsize=12pt]{scrartcl}
  2. \usepackage{mwe}% useful for minimal but working examples
  3.  
  4. \usepackage{hyperref}% Must be loaded after float!!!!
  5. % Generally you should load hyperref after all other
  6. % packages but those for which the manual of either the
  7. % package or hyperref tells you, that you should load it
  8. % after hyperref.
  9. \usepackage{bookmark}% It is recommended to use this with hyperref, because it
  10. % improves generation of bookmarks.
  11.  
  12. % Make the text of all ToC entries ragged-right
  13. \DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}
  14.  
  15. \begin{document}
  16.  
  17. \tableofcontents
  18.  
  19. \section[Introduction in the table of contents, where even all these strange
  20. verylongtextentry are raggedright using \textsf{tocbasic}]{Introduction}
  21.  
  22. \lipsum[1] % provided by package lipsum loaded by package mwe (which you are
  23. % already using to get dummy text)
  24.  
  25. \end{document}


Return to “Page Layout”

Who is online

Users browsing this forum: No registered users and 4 guests