Page LayoutProblem with table of contents

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

Problem with table of contents

Post by ciema7 »

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 19385 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problem with table of contents

Post by Johannes_B »

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: 22
Joined: Sat Nov 10, 2018 2:31 am

Problem with table of contents

Post by ciema7 »

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 ?):

Code: Select all

\documentclass[fontsize=12pt]{article}
\usepackage[table,xcdraw]{xcolor}
\usepackage{listings}
\usepackage{inputenc}
\usepackage{graphicx}
\usepackage{polski}
\usepackage{geometry} 
\usepackage{textcomp} 
\usepackage{gensymb}
\usepackage{parskip}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{enumitem}
\usepackage{booktabs}
\usepackage{array}
\usepackage{caption} 
\usepackage{amsmath}
\usepackage{scrextend}
\usepackage{mwe} 
\usepackage[export]{adjustbox}  % do wycentrowania obrazka w tabeli w pionie
\usepackage{setspace}
\usepackage{stanli}
\usepackage{tikz} 
\usepackage{wrapfig} 
\usepackage{hyperref}
\usepackage[numbers,sort&compress]{natbib}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{pdfpages}
%\usepackage{titlesec}
\usepackage{placeins} 
\usepackage{bm} 
\usepackage{float} 
%\usepackage{newtxmath} 
% \usepackage{fontspec}
% \setmainfont{Times New Roman} 
%\usepackage[none]{hyphenat}
%\titlelabel{\thetitle.\quad} 

\pagenumbering{arabic} 
\usepackage{leading}
\leading{18pt} 



\tolerance=1 
\emergencystretch=\maxdimen
\hyphenpenalty=10000
\hbadness=10000

 
\captionsetup[figure]{font=footnotesize,labelfont=footnotesize} 
 

\lstset{language=matlab,basicstyle=\footnotesize,showstringspaces=false,style = Matlab-editor,basicstyle=\mlttfamily,
	escapechar= ",mlshowsectionrules = true}



\graphicspath{ {figures/} } 
\begin{document}

\newgeometry{tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm} 
	

	
\renewcommand{\thesection}{\arabic{section}.} % tworzy lub usuwa kropki przy rozdzialach (reguluje)
\numberwithin{equation}{section} % tworzy lub usuwa kropki przy rozdzialach (reguluje)
\renewcommand{\theequation}{\thesection\arabic{equation}}
\renewcommand{\thesubsection}{\thesection\arabic{subsection}} 

	\vspace{0.6cm}
	\begin{centering}
		\tableofcontents
	\end{centering}


\newpage
	\addtocontents{toc}{\protect\addvspace{20mm}}
\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}

\vspace{1cm}\hspace{1cm}These are my few words in the section.


	\end{document}
Last edited by Stefan Kottwitz on Wed May 08, 2019 4:15 pm, edited 1 time in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Problem with table of contents

Post by Stefan Kottwitz »

Hello,

that centering here causes it:

Code: Select all

\begin{centering}
\tableofcontents
\end{centering}
Just change to

Code: Select all

\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
LaTeX.org admin
ciema7
Posts: 22
Joined: Sat Nov 10, 2018 2:31 am

Problem with table of contents

Post by ciema7 »

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: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Problem with table of contents

Post by Stefan Kottwitz »

Here is a way:

Code: Select all

\usepackage{tocstyle}
\usetocstyle{standard}
\settocfeature{raggedhook}{\raggedright}

Stefan
LaTeX.org admin
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Problem with table of contents

Post by Ijon Tichy »

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

Code: Select all

\usepackage{tocbasic}% One more KOMA-Script package added.
% Using tocbasic to make the table of contents:
\renewcommand*{\tableofcontents}{\listoftoc[\contentsname]{toc}}
\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:

Code: Select all

\documentclass[12pt]{article}% fontsize=12pt is a KOMA-Script option. To have
                             % the same with article you need to use 12pt.
\usepackage[table,xcdraw]{xcolor}
\usepackage{listings}
%\usepackage{inputenc}% inputenc without option doesn't make much sense and if
                      % you are using an up-to-date TeX installation and UTF8
                      % encoded input you even do not need this package.
\usepackage{graphicx}
\usepackage{polski}
\usepackage{geometry}
\usepackage{textcomp} 
\usepackage{gensymb}
\usepackage{parskip}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{enumitem}
\usepackage{booktabs}
%\usepackage{array}% Already loaded by tabularx
\usepackage{caption} 
\usepackage{amsmath}
\usepackage{scrextend}% Wouldn't it be a choice to use scrartcl instead of
                      % article, if you want KOMA-Script features?
\usepackage{mwe} 
\usepackage[export]{adjustbox}  % do wycentrowania obrazka w tabeli w pionie
\usepackage{setspace}% It does not make much sense to use this package
                     % together with package leading!
\usepackage{stanli}
%\usepackage{tikz}% Already loaded by stanli
\usepackage{wrapfig} 
\usepackage[numbers,sort&compress]{natbib}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{pdfpages}
%\usepackage{titlesec}
\usepackage{placeins} 
\usepackage{bm} 
\usepackage{float} 
%\usepackage{newtxmath} 
% \usepackage{fontspec}
% \setmainfont{Times New Roman} 
%\usepackage[none]{hyphenat}
%\titlelabel{\thetitle.\quad} 
\usepackage{leading}

\usepackage{tocbasic}% One more KOMA-Script package added.

\usepackage{hyperref}% Must be loaded after float!!!!
                     % Generally you should load hyperref after all other
                     % packages but those for which the manual of either the
                     % package or hyperref tells you, that you should load it
                     % after hyperref.
\usepackage{bookmark}% It is recommended to use this with hyperref, because it
                     % improves generation of bookmarks.

% Using tocbasic to make the table of contents:
\renewcommand*{\tableofcontents}{\listoftoc[\contentsname]{toc}}
\DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}

%\pagenumbering{arabic}% Default
\leading{18pt} 

\tolerance=1 
\emergencystretch=\maxdimen
\hyphenpenalty=10000% Really? Do not allow hyphenation?
\hbadness=10000

\captionsetup[figure]{font=footnotesize,labelfont=footnotesize} 

\lstset{language=matlab,basicstyle=\footnotesize,showstringspaces=false,style = Matlab-editor,basicstyle=\mlttfamily,
    escapechar= ",mlshowsectionrules = true}

\graphicspath{{figures/}}% Avoid whitespaces in \graphicspath, because the
                         % manual does not specify them as tollerated (however
                         % currently they are).


% All global settings should be done in the document preamble!
\geometry{tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm}

\renewcommand{\thesection}{\arabic{section}.} % tworzy lub usuwa kropki przy rozdzialach (reguluje)
\numberwithin{equation}{section}% I would recommend to use \counterwithin
                                % instead of \numberwithin, because since
                                % LaTeX 2018/04/01 it is part of the LaTeX
                                % kernel. With prior LaTeX kernels (e.g. TeX
                                % Live 2017 from Overlead) you can load
                                % package chngcntr to get \counterwithin.
\renewcommand{\theequation}{\thesection\arabic{equation}}
\renewcommand{\thesubsection}{\thesection\arabic{subsection}} 

\begin{document}

\tableofcontents

\newpage

%\addtocontents{toc}{\protect\addvspace{20mm}}% Should not be needed.
\section{Introduction is placed good in the table of contents, where the longtext-entry
  is raggedright using \textsf{tocbasic}}

\lipsum[1] % provided by package lipsum loaded by package mwe (which you are
           % already using to get dummy text)

\end{document}
BTW: A more minimal but working example for the problem would be:

Code: Select all

\documentclass[12pt]{article}% fontsize=12pt is a KOMA-Script option. To have
                             % the same with article you need to use 12pt.
\usepackage{mwe}% useful for minimal but working examples

\usepackage{tocbasic}% One more KOMA-Script package added.

\usepackage{hyperref}% Must be loaded after float!!!!
                     % Generally you should load hyperref after all other
                     % packages but those for which the manual of either the
                     % package or hyperref tells you, that you should load it
                     % after hyperref.
\usepackage{bookmark}% It is recommended to use this with hyperref, because it
                     % improves generation of bookmarks.

% Using tocbasic to make the table of contents:
\renewcommand*{\tableofcontents}{\listoftoc[\contentsname]{toc}}
\DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}

\begin{document}

\tableofcontents

\section[Introduction in the table of contents, where even this verylongtext-entry
  is raggedright using \textsf{tocbasic}]{Introduction}

\lipsum[1] % provided by package lipsum loaded by package mwe (which you are
           % already using to get dummy text)

\end{document}
or using KOMA-Script class scrartcl:

Code: Select all

\documentclass[fonsize=12pt]{scrartcl}
\usepackage{mwe}% useful for minimal but working examples

\usepackage{hyperref}% Must be loaded after float!!!!
                     % Generally you should load hyperref after all other
                     % packages but those for which the manual of either the
                     % package or hyperref tells you, that you should load it
                     % after hyperref.
\usepackage{bookmark}% It is recommended to use this with hyperref, because it
                     % improves generation of bookmarks.

% Make the text of all ToC entries ragged-right
\DeclareTOCStyleEntries[raggedentrytext]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}

\begin{document}

\tableofcontents

\section[Introduction in the table of contents, where even all these strange
  verylongtextentry are raggedright using \textsf{tocbasic}]{Introduction}

\lipsum[1] % provided by package lipsum loaded by package mwe (which you are
           % already using to get dummy text)

\end{document}
Post Reply