Page Layout ⇒ Problem with table of contents
Problem with table of contents
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 (26.77 KiB) Viewed 20238 times
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Problem with table of contents
Problem with table of contents
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}
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
Problem with table of contents
that centering here causes it:
Code: Select all
\begin{centering}
\tableofcontents
\end{centering}
Code: Select all
\tableofcontents
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
Problem with table of contents

PS. Stefan you can edit your previous post and delete this " } " from the second code, you provided

- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
Problem with table of contents
Code: Select all
\usepackage{tocstyle}
\usetocstyle{standard}
\settocfeature{raggedhook}{\raggedright}
Stefan
- Ijon Tichy
- Posts: 640
- Joined: Mon Dec 24, 2018 10:12 am
Problem with table of contents
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}
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}
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}
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}