0

Is there a simple way to make the table of contents without line breaks between names of sections; so it would look like this:

ToC with sections in one line

I produced it using command \addtocontents for each section, but it would be better to find an automatic solution.

Postscript. This solution works perfectly --- thanks to leandriis.

3
1

The following is an elementary implementation that gobbles everything of the \section within the ToC except the title, setting it within an appropriate adjustwidth environment (with the typical/expected left/right indentations).

enter image description here

\documentclass{book}

\usepackage{lipsum}
\usepackage{etoolbox,changepage}

\setcounter{secnumdepth}{0}% Only number up to section level 0 (chapter)

\makeatletter
\renewcommand{\l@section}[2]{#1. }%
\patchcmd{\@chapter}% <cmd>
  {\addtocontents}% <search>
  {\ifnum\value{chapter}<1\else
    \addtocontents{toc}{\begin{adjustwidth}{1.5em}{\@pnumwidth}}%
  \fi
  \addtocontents}% <replace>
  {}{}% <success><failure>
\pretocmd{\chapter}{%
  %\addtocontents{toc}{\protect\par}
  \ifnum\value{chapter}>0
    \addtocontents{toc}{\end{adjustwidth}\par}
  \fi
}
{}{}% <success><failure>
\AtEndDocument{\addtocontents{toc}{\end{adjustwidth}}}% Close off final adjustwidth environment
\makeatother

\begin{document}

\tableofcontents

\sloppy
\chapter{First chapter}\lipsum[1-50]
\section{First section}\lipsum[1-50]
\section{Second section}\lipsum[1-50]
\section{Third section}\lipsum[1-50]
\section{Fourth section}\lipsum[1-50]
\section{Fifth section}\lipsum[1-50]
\section{Sixth section}\lipsum[1-50]
\section{Seventh section}\lipsum[1-50]
\section{Last section}\lipsum[1-50]

\chapter{Second chapter}\lipsum[1-50]
\section{First section}\lipsum[1-50]
\section{Second section}\lipsum[1-50]
\section{Third section}\lipsum[1-50]
\section{Last section}\lipsum[1-50]

\chapter{Third chapter}\lipsum[1-50]
\section{First section}\lipsum[1-50]
\section{Second section}\lipsum[1-50]
\section{Third section}\lipsum[1-50]
\section{Last section}\lipsum[1-50]

\chapter{Last chapter}\lipsum[1-50]
\section{First section}\lipsum[1-50]
\section{Second section}\lipsum[1-50]
\section{Third section}\lipsum[1-50]
\section{Last section}\lipsum[1-50]

\end{document}

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.