1

The problem of having ragged right toc is described in Table of contents: section titles ragged right for articles. The etoolbox-option seem not to work with the book class, and I am not too comfortable using tocstyle due to it being alpha-version (for example, I get some underfull \vbox in my toc using this package). Any other solutions? Here is a MWE:

\documentclass[b5paper, twoside, 11pt]{book}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@dottedtocline}
  {\rightskip\@tocrmarg}
  {\rightskip\@tocrmarg plus 4em \hyphenpenalty\@M}
  {}{}
\makeatother


\begin{document}

\tableofcontents

\part{A very very long title that is hyphenated and not raggedright even though etoolbox is used}
\part{A very very very long title that is hyphenated and not raggedright even though etoolbox is used}
\part{A very very very very long title that is hyphenated and not raggedright even though etoolbox is used}

\end{document}

A more comprehensive TOC is obtained by the following

\documentclass[b5paper, twoside, 11pt]{book}

%\usepackage{etoolbox}
%\makeatletter
%\patchcmd{\@dottedtocline}
%  {\rightskip\@tocrmarg}
%  {\rightskip\@tocrmarg plus 4em \hyphenpenalty\@M}
%  {}{}
%\makeatother


%\usepackage{tocloft}
%\renewcommand{\cftpnumalign}{l}
%\renewcommand{\cftpartleader}{ }    % change \part setting
%\renewcommand{\cftpartafterpnum}{\cftparfillskip}
%\renewcommand{\cftchapleader}{ }    % change \chapter setting
%\renewcommand{\cftchapafterpnum}{\cftparfillskip}
%\renewcommand{\cftsecleader}{ }     % change \section setting
%\renewcommand{\cftsubsecafterpnum}{\cftparfillskip}
%\renewcommand{\cftsubsecleader}{ }     % change \subsection setting
%\renewcommand{\cftsecafterpnum}{\cftparfillskip}


\usepackage{tocbasic}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=0pt,
  raggedentrytext
]{tocline}{part}

\DeclareTOCStyleEntries[
  raggedentrytext
]{tocline}{chapter,section,subsection,subsubsection,paragraph,subparagraph}

\newcommand{\tocseparator}{\vspace{0.5\normalbaselineskip}} % Command used to have slightly larger separation between sectionslist and appendiceslist and referenceslist in toc.
\usepackage{blindtext}
\begin{document}

\tableofcontents

\chapter{Abbreviations}
\Blindtext[10][1]
\chapter{Notation}
\Blindtext[10][1]
\part{Introduction}
\blindtext[10][1]
\section{Background}
\Blindtext[10][1]
\section{Isogeometric analysis}
\Blindtext[10][1]
\subsection{B-splines}
\Blindtext[10][1]
\subsection{B-spline knot insertion}
\Blindtext[10][1]
\subsection{B-spline degree elevation}
\Blindtext[10][1]
\subsection{Spline volumes}
\Blindtext[10][1]
\subsection{NURBS}
\Blindtext[10][1]
\subsection{NURBS knot insertion}
\Blindtext[10][1]
\subsection{The weak form and Galerkin’s method}
\Blindtext[10][1]
\subsection{Assembly}
\Blindtext[10][1]
\subsection{Error analysis}
\Blindtext[10][1]
\section{Helmholtz problems}
\Blindtext[10][1]
\subsection{Far field pattern}
\Blindtext[10][1]
\section{Spectral methods}
\Blindtext[10][1]
\subsection{Spectral element method}
\Blindtext[10][1]
\subsection{Method of fundamental solutions}
\Blindtext[10][1]
\section{Summary of Papers}
\Blindtext[10][1]
\subsection{Paper I: Exact 3D scattering solutions for spherical symmetric scatterers}
\Blindtext[10][1]
\subsection{Paper II: Isogeometric Analysis of Acoustic Scattering using Infinite Elements}
\Blindtext[10][1]
\subsection{Paper III: Isogeometric boundary element method}
\Blindtext[10][1]
\subsection{Paper IV: Isogeometric Kirchhoff approximation using numerical steepest descent}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper I – Exact 3D Scattering Solutions for Spherical Symmetric Scatterers}
\section{Introduction}
\Blindtext[10][1]
\section{Governing equations}
\Blindtext[10][1]
\subsection{Governing equations in the time domain}
\Blindtext[10][1]
\subsection{Governing equations in the frequency domain}
\Blindtext[10][1]
\section{General solution in the solid domain}
\Blindtext[10][1]
\subsection{Lam{\'e} solution}
\Blindtext[10][1]
\subsection{Series representation using separation of variables}
\Blindtext[10][1]
\subsection{Expressions for the displacement and stress field}
\Blindtext[10][1]
\subsection{Validation of the displacement and stress formulas}
\Blindtext[10][1]
\section{Establishing constraints from boundary conditions}
\Blindtext[10][1]
\subsection{Notation for the solution in layered domains}
\Blindtext[10][1]
\subsection{Tangential traction conditions}
\Blindtext[10][1]
\subsection{Displacement and pressure condition in intermediate fluid layers}
\Blindtext[10][1]
\subsection{Displacement and pressure condition in the outermost fluid}
\Blindtext[10][1]
\subsection{Displacement and pressure condition in the innermost fluid}
\Blindtext[10][1]
\section{Assembling the linear system of equations}
\Blindtext[10][1]
\subsection{The default case with Neumann-to-Neumann conditions}
\Blindtext[10][1]
\subsection{Alternative boundary conditions}
\Blindtext[10][1]
\subsection{Summary of solution formulas}
\Blindtext[10][1]
\section{Computational aspects}
\Blindtext[10][1]
\subsection{Matrix manipulations}
\Blindtext[10][1]
\subsection{Series evaluation}
\Blindtext[10][1]
\subsection{Round-off errors}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Chang benchmark problem}
\Blindtext[10][1]
\subsection{Ihlenburg benchmark problem}
\Blindtext[10][1]
\subsection{Fender benchmark problem}
\Blindtext[10][1]
\subsection{Benchmark problems}
\Blindtext[10][1]
\subsection{Benchmark problems in the time domain}
\Blindtext[10][1]
\section{Conclusions}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{The spherical coordinate system}
\Blindtext[10][1]
\section{Linear elasticity}
\Blindtext[10][1]
\section{Fundamental functions}
\Blindtext[10][1]
\subsection{Legendre polynomials}
\Blindtext[10][1]
\subsection{Spherical Bessel and Hankel functions}
\Blindtext[10][1]
\section{The incident wave}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper II – Isogeometric Analysis of Acoustic Scattering using Infinite Elements}
\section{Introduction}
\Blindtext[10][1]
\section{Exterior Helmholtz problems}
\Blindtext[10][1]
\subsection{Weak formulation for the Helmholtz equation}
\Blindtext[10][1]
\subsection{Infinite elements}
\Blindtext[10][1]
\subsection{Far field pattern}
\Blindtext[10][1]
\section{Acoustic-structure interaction}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Simpson benchmark}
\Blindtext[10][1]
\subsection{Ihlenburg benchmark}
\Blindtext[10][1]
\subsection{Radial pulsation from a mock shell}
\Blindtext[10][1]
\subsection{Stripped BeTSSi submarine}
\section{Conclusions}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{Derivation of bilinear form in infinite elements}
\Blindtext[10][1]
\subsection{The prolate spheroidal coordinate system}
\Blindtext[10][1]
\subsection{Bilinear form for unconjugated Petrov–Galerkin formulation}
\Blindtext[10][1]
\section{Evaluation of radial integrals}
\Blindtext[10][1]
\section{The stripped BeTSSi submarine model}
\Blindtext[10][1]
\section{Approximating NURBS parametrizations with B-spline parametrizations}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper III – Isogeometric Boundary Element Method for Acoustic Scattering by a Submarine}
\section{Introduction}
\Blindtext[10][1]
\section{Helmholtz problems}
\Blindtext[10][1]
\subsection{Far field pattern}
\Blindtext[10][1]
\section{Boundary integral equations}
\Blindtext[10][1]
\subsection{Regularization techniques}
\Blindtext[10][1]
\subsection{Rigid scattering problems}
\Blindtext[10][1]
\section{Collocation and Galerkin formulations}
\Blindtext[10][1]
\section{Numerical evaluation of the boundary integrals}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Pulsating sphere}
\Blindtext[10][1]
\subsection{Rigid scattering on a sphere}
\Blindtext[10][1]
\subsection{Torus interior acoustic problem}
\Blindtext[10][1]
\subsection{Manufactured solutions for complex geometries}
\Blindtext[10][1]
\subsection{Manufactured solution with a cube}
\Blindtext[10][1]
\subsection{Manufactured solution with the BeTSSi submarine}
\Blindtext[10][1]
\subsection{Rigid scattering on the BeTSSi submarine}
\Blindtext[10][1]
\section{Conclusions}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{NURBS parametrization of the sphere}
\Blindtext[10][1]
\subsection{Parametrization 1}
\Blindtext[10][1]
\subsection{Parametrization 2}
\Blindtext[10][1]
\section{NURBS parametrization of the torus}
\Blindtext[10][1]
\section{The BeTSSi submarine model}
\Blindtext[10][1]
\subsection{Main body}
\Blindtext[10][1]
\subsection{NACA profiles}
\Blindtext[10][1]
\subsection{Sail}
\Blindtext[10][1]
\subsection{Main rudders}
\Blindtext[10][1]
\subsection{Depth rudders}
\Blindtext[10][1]
\section{An analysis suitable BeTSSi submarine}
\Blindtext[10][1]
\section{Triangulation of the BeTSSi submarine}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper IV – Isogeometric Kirchhoff Approximation using Numerical Steepest Descent}
\section{Introduction}
\Blindtext[10][1]
\section{Kirchhoff approximation}
\Blindtext[10][1]
\section{Numerical steepest descent}
\Blindtext[10][1]
\subsection{Starting value for Newton iterations}
\Blindtext[10][1]
\subsection{The case of no critical points or resonance points in 2D}
\Blindtext[10][1]
\subsection{Resonance points}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Scattering on a rigid sphere}
\Blindtext[10][1]
\section{Conclusion}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Addendum – Spectral Element Analysis of Acoustic Scattering using Infinite Elements}
\section{Introduction}
\Blindtext[10][1]
\section{Lagrange basis functions and GLL nodes}
\Blindtext[10][1]
\section{Geometry approximation}
\Blindtext[10][1]
\section{Exterior Helmholtz problems}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Poisson 1D problem}
\Blindtext[10][1]
\subsection{Rigid scattering on a sphere}
\Blindtext[10][1]
\section{Conclusions}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{A note on the infinite element method}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Appendices}
\section{Derivation of bilinear form for the infinite elements}
\Blindtext[10][1]
\subsection{Numerical examples}
\Blindtext[10][1]
\section{Ray/beam tracing in the isogeometric framework}
\Blindtext[10][1]
\subsection{Beam tracing using linear shape functions}
\Blindtext[10][1]
\subsection{Ray tracing in the isogeometric framework}
\Blindtext[10][1]
\subsection{Computation of reflection and transmission coefficients}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\end{document}
  • tocstyle is no longer developed. It is therefore not recommended to use this package. – esdd Oct 8 at 18:35
1

You could use package tocbasic:

\documentclass[b5paper, twoside, 11pt]{book}

\usepackage{tocbasic}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=0pt,
  raggedentrytext
]{tocline}{part}

\DeclareTOCStyleEntries[
  raggedentrytext
]{tocline}{chapter,section,subsection,subsubsection,paragraph,subparagraph}

\begin{document}

\tableofcontents

\part{A very very long title that is hyphenated and not raggedright even though etoolbox is used}
\chapter{A chapter with a long unhyphenated title that hopefully is set raggedright}
\section{A section with a long unhyphenated title that hopefully is set raggedright}
\subsection{A subsection with a long unhyphenated title that hopefully is set raggedright}
\subsection{A subsection with a short title}
\part{A very very very long title that is hyphenated and not raggedright even though etoolbox is used}
\part{A very very very very long title that is hyphenated and not raggedright even though etoolbox is used}

\end{document}

Result:

enter image description here


Update regarding the new example in the question using larger page numbers

You can add option pagenumberbox={\mbox} for the part entries in TOC. Maybe the same is needed for chapters. (At the moment there are no chapters after the first part title).

Example:

\documentclass[b5paper, twoside, 11pt]{book}
\usepackage[paper=b5paper]{geometry}% <- added to get b5paper

%%%% to remove the overfull boxes outside the ToC:
\raggedbottom\pagestyle{plain}
\usepackage[raggedright]{titlesec}% <- raggedright section etc. in document
%%%%

\usepackage{tocbasic}
\DeclareTOCStyleEntry[
  indent=0pt,
  numwidth=0pt,
  pagenumberbox={\mbox},
  raggedentrytext
]{tocline}{part}
\DeclareTOCStyleEntry[
  pagenumberbox={\mbox},
  raggedentrytext
]{tocline}{chapter}

\DeclareTOCStyleEntries[
  raggedentrytext
]{tocline}{section,subsection,subsubsection,paragraph,subparagraph}

\newcommand{\tocseparator}{\vspace{0.5\normalbaselineskip}} % Command used to have slightly larger separation between sectionslist and appendiceslist and referenceslist in toc.
\usepackage{blindtext}
\begin{document}

\tableofcontents

\chapter{Abbreviations}
\Blindtext[10][1]
\chapter{Notation}
\Blindtext[10][1]
\part{Introduction}
\blindtext[10][1]
\section{Background}
\Blindtext[10][1]
\section{Isogeometric analysis}
\Blindtext[10][1]
\subsection{B-splines}
\Blindtext[10][1]
\subsection{B-spline knot insertion}
\Blindtext[10][1]
\subsection{B-spline degree elevation}
\Blindtext[10][1]
\subsection{Spline volumes}
\Blindtext[10][1]
\subsection{NURBS}
\Blindtext[10][1]
\subsection{NURBS knot insertion}
\Blindtext[10][1]
\subsection{The weak form and Galerkin’s method}
\Blindtext[10][1]
\subsection{Assembly}
\Blindtext[10][1]
\subsection{Error analysis}
\Blindtext[10][1]
\section{Helmholtz problems}
\Blindtext[10][1]
\subsection{Far field pattern}
\Blindtext[10][1]
\section{Spectral methods}
\Blindtext[10][1]
\subsection{Spectral element method}
\Blindtext[10][1]
\subsection{Method of fundamental solutions}
\Blindtext[10][1]
\section{Summary of Papers}
\Blindtext[10][1]
\subsection{Paper I: Exact 3D scattering solutions for spherical symmetric scatterers}
\Blindtext[10][1]
\subsection{Paper II: Isogeometric Analysis of Acoustic Scattering using Infinite Elements}
\Blindtext[10][1]
\subsection{Paper III: Isogeometric boundary element method}
\Blindtext[10][1]
\subsection{Paper IV: Isogeometric Kirchhoff approximation using numerical steepest descent}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper I – Exact 3D Scattering Solutions for Spherical Symmetric Scatterers}
\section{Introduction}
\Blindtext[10][1]
\section{Governing equations}
\Blindtext[10][1]
\subsection{Governing equations in the time domain}
\Blindtext[10][1]
\subsection{Governing equations in the frequency domain}
\Blindtext[10][1]
\section{General solution in the solid domain}
\Blindtext[10][1]
\subsection{Lam{\'e} solution}
\Blindtext[10][1]
\subsection{Series representation using separation of variables}
\Blindtext[10][1]
\subsection{Expressions for the displacement and stress field}
\Blindtext[10][1]
\subsection{Validation of the displacement and stress formulas}
\Blindtext[10][1]
\section{Establishing constraints from boundary conditions}
\Blindtext[10][1]
\subsection{Notation for the solution in layered domains}
\Blindtext[10][1]
\subsection{Tangential traction conditions}
\Blindtext[10][1]
\subsection{Displacement and pressure condition in intermediate fluid layers}
\Blindtext[10][1]
\subsection{Displacement and pressure condition in the outermost fluid}
\Blindtext[10][1]
\subsection{Displacement and pressure condition in the innermost fluid}
\Blindtext[10][1]
\section{Assembling the linear system of equations}
\Blindtext[10][1]
\subsection{The default case with Neumann-to-Neumann conditions}
\Blindtext[10][1]
\subsection{Alternative boundary conditions}
\Blindtext[10][1]
\subsection{Summary of solution formulas}
\Blindtext[10][1]
\section{Computational aspects}
\Blindtext[10][1]
\subsection{Matrix manipulations}
\Blindtext[10][1]
\subsection{Series evaluation}
\Blindtext[10][1]
\subsection{Round-off errors}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Chang benchmark problem}
\Blindtext[10][1]
\subsection{Ihlenburg benchmark problem}
\Blindtext[10][1]
\subsection{Fender benchmark problem}
\Blindtext[10][1]
\subsection{Benchmark problems}
\Blindtext[10][1]
\subsection{Benchmark problems in the time domain}
\Blindtext[10][1]
\section{Conclusions}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{The spherical coordinate system}
\Blindtext[10][1]
\section{Linear elasticity}
\Blindtext[10][1]
\section{Fundamental functions}
\Blindtext[10][1]
\subsection{Legendre polynomials}
\Blindtext[10][1]
\subsection{Spherical Bessel and Hankel functions}
\Blindtext[10][1]
\section{The incident wave}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper II – Isogeometric Analysis of Acoustic Scattering using Infinite Elements}
\section{Introduction}
\Blindtext[10][1]
\section{Exterior Helmholtz problems}
\Blindtext[10][1]
\subsection{Weak formulation for the Helmholtz equation}
\Blindtext[10][1]
\subsection{Infinite elements}
\Blindtext[10][1]
\subsection{Far field pattern}
\Blindtext[10][1]
\section{Acoustic-structure interaction}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Simpson benchmark}
\Blindtext[10][1]
\subsection{Ihlenburg benchmark}
\Blindtext[10][1]
\subsection{Radial pulsation from a mock shell}
\Blindtext[10][1]
\subsection{Stripped BeTSSi submarine}
\section{Conclusions}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{Derivation of bilinear form in infinite elements}
\Blindtext[10][1]
\subsection{The prolate spheroidal coordinate system}
\Blindtext[10][1]
\subsection{Bilinear form for unconjugated Petrov–Galerkin formulation}
\Blindtext[10][1]
\section{Evaluation of radial integrals}
\Blindtext[10][1]
\section{The stripped BeTSSi submarine model}
\Blindtext[10][1]
\section{Approximating NURBS parametrizations with B-spline parametrizations}
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper III – Isogeometric Boundary Element Method for Acoustic Scattering by a Submarine}
\section{Introduction}
\Blindtext[10][1]
\section{Helmholtz problems}
\Blindtext[10][1]
\subsection{Far field pattern}
\Blindtext[10][1]
\section{Boundary integral equations}
\Blindtext[10][1]
\subsection{Regularization techniques}
\Blindtext[10][1]
\subsection{Rigid scattering problems}
\Blindtext[10][1]
\section{Collocation and Galerkin formulations}
\Blindtext[10][1]
\section{Numerical evaluation of the boundary integrals}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Pulsating sphere}
\Blindtext[10][1]
\subsection{Rigid scattering on a sphere}
\Blindtext[10][1]
\subsection{Torus interior acoustic problem}
\Blindtext[10][1]
\subsection{Manufactured solutions for complex geometries}
\Blindtext[10][1]
\subsection{Manufactured solution with a cube}
\Blindtext[10][1]
\subsection{Manufactured solution with the BeTSSi submarine}
\Blindtext[10][1]
\subsection{Rigid scattering on the BeTSSi submarine}
\Blindtext[10][1]
\section{Conclusions}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{NURBS parametrization of the sphere}
\Blindtext[10][1]
\subsection{Parametrization 1}
\Blindtext[10][1]
\subsection{Parametrization 2}
\Blindtext[10][1]
\section{NURBS parametrization of the torus}
\Blindtext[10][1]
\section{The BeTSSi submarine model}
\Blindtext[10][1]
\subsection{Main body}
\Blindtext[10][1]
\subsection{NACA profiles}
\Blindtext[10][1]
\subsection{Sail}
\Blindtext[10][1]
\subsection{Main rudders}
\Blindtext[10][1]
\subsection{Depth rudders}
\Blindtext[10][1]
\section{An analysis suitable BeTSSi submarine}
\Blindtext[10][1]
\section{Triangulation of the BeTSSi submarine}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Paper IV – Isogeometric Kirchhoff Approximation using Numerical Steepest Descent}
\section{Introduction}
\Blindtext[10][1]
\section{Kirchhoff approximation}
\Blindtext[10][1]
\section{Numerical steepest descent}
\Blindtext[10][1]
\subsection{Starting value for Newton iterations}
\Blindtext[10][1]
\subsection{The case of no critical points or resonance points in 2D}
\Blindtext[10][1]
\subsection{Resonance points}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Scattering on a rigid sphere}
\Blindtext[10][1]
\section{Conclusion}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Addendum – Spectral Element Analysis of Acoustic Scattering using Infinite Elements}
\section{Introduction}
\Blindtext[10][1]
\section{Lagrange basis functions and GLL nodes}
\Blindtext[10][1]
\section{Geometry approximation}
\Blindtext[10][1]
\section{Exterior Helmholtz problems}
\Blindtext[10][1]
\section{Numerical examples}
\Blindtext[10][1]
\subsection{Poisson 1D problem}
\Blindtext[10][1]
\subsection{Rigid scattering on a sphere}
\Blindtext[10][1]
\section{Conclusions}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{A note on the infinite element method}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]

\part{Appendices}
\section{Derivation of bilinear form for the infinite elements}
\Blindtext[10][1]
\subsection{Numerical examples}
\Blindtext[10][1]
\section{Ray/beam tracing in the isogeometric framework}
\Blindtext[10][1]
\subsection{Beam tracing using linear shape functions}
\Blindtext[10][1]
\subsection{Ray tracing in the isogeometric framework}
\Blindtext[10][1]
\subsection{Computation of reflection and transmission coefficients}
\Blindtext[10][1]
\addtocontents{toc}{\tocseparator} % add an empty line before references in toc
\section{References}
\Blindtext[10][1]
\end{document}

Result:

enter image description here

  • This gives me the results I want for the MWE. However, as for the tocstyle, I get several overfull boxes for my full document. I have added a more comprehensive example to show this. It seems that large page numbers for "part" gives overfull \hbox in this example. – user253249 Oct 9 at 6:49
  • See my updated answer – esdd Oct 9 at 7:40
  • Perfect! Thanks a lot :) – user253249 Oct 9 at 15:41
0

I'm not sure what you mean by a raggedright ToC. However the tocloft package could come to your aid. Below is a modified version of your MWE.

% tocraggedprob.tex  SE 511452  set ToC raggedright

\documentclass[b5paper, twoside, 11pt]{book}
\usepackage{comment}

\begin{comment}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@dottedtocline}
  {\rightskip\@tocrmarg}
  {\rightskip\@tocrmarg plus 4em \hyphenpenalty\@M}
  {}{}
\makeatother
\end{comment}

\usepackage{tocloft}
\renewcommand{\cftpnumalign}{l}
\renewcommand{\cftpartleader}{ }    % change \part setting
\renewcommand{\cftpartafterpnum}{\cftparfillskip}
\renewcommand{\cftchapleader}{ }    % change \chapter setting
\renewcommand{\cftchapafterpnum}{\cftparfillskip}
\renewcommand{\cftsecleader}{ }     % change \section setting
\renewcommand{\cftsubsecafterpnum}{\cftparfillskip}
\renewcommand{\cftsubsecleader}{ }     % change \subsection setting
\renewcommand{\cftsecafterpnum}{\cftparfillskip}
% and similarly for \subsection (subsec), \subsubsection (subsubsec),
% \paragraph (para) and \subparagraph (subpara) entries 

\begin{document}

\tableofcontents

\part{A very very long title that is hyphenated and not raggedright even though etoolbox is used}
\chapter{A chapter with a long unhyphenated title that hopefully is set raggedright}
\section{A section with a long unhyphenated title that hopefully is set raggedright}
\subsection{A subsection with a long unhyphenated title that hopefully is set raggedright}
\subsection{A subsection with a short title}
\part{A very very very long title that is hyphenated and not raggedright even though etoolbox is used}
\part{A very very very very long title that is hyphenated and not raggedright even though etoolbox is used}

\end{document}

This results in the page numbers being set just at the end of an entry with no dotted lines. To me this looks raggedright.

Read the documentation (> texdoc tocloft) for more information.

  • I guess the tocloft package comes with options of having dots inserted and page number in the end. However, as for the tocstyle, I get several overfull boxes for my full document. I have added a more comprehensive example to show this. Moreover, I get some hyphenation also for this case... – user253249 Oct 9 at 6:10
  • @user253249 tocloft enables you to control the dotted lines and many more things that you might want to do. Read the documentation. – Peter Wilson Oct 10 at 17:18

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.