Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How do I suppress pagebreaks in the toc "paragraphs" produced by titlesec's \titlecontents* command without stopping linebreaks between subsections?

The follwoing suppresses linebreaks within subsection, but doesn't deal with pagebreaks. titlesec does internally use \nobreak and \addpenalty in the definition of \ttl@outnoblock, but I don't know where I would have to add them.

\documentclass{book}
\usepackage{titletoc}
\titlecontents*{subsection}[3.8em]{\filright\itshape\contentsmargin{2em}}%
  {\thecontentslabel.\nobreakspace\mbox}{\mbox}{,\nobreakspace\thecontentspage}[][ -- ][.]

\setcounter{tocdepth}{2}
\setcounter{secnumdepth}{1}

\usepackage[linktocpage]{hyperref}
\usepackage{bookmark}

\begin{document}

\frontmatter

\phantomsection
\addcontentsline{toc}{chapter}{\contentsname}
\tableofcontents

\mainmatter

\chapter{my first chapter}\label{ch:first}
\section{my first section}\label{sec:first}
\addtocontents{toc}{\vspace{11cm}} %for the example
\chapter{my second chapter}
\section{my second section}\label{sec:second}
\subsection{first subsection}
\subsection{second subsection}
\subsection{third subsection}
\subsection{4th subsection}
\subsection{5th subsection}
\subsection{6th subsection}
\subsection{7th subsection}

\end{document}

It might in general be better just to increase the penalty than absolutely forbid the pagebreak, but in my particular case forbidding it might be sufficient.

The pagebreak functionalities of titletoc should be retained if possible:

Pages are never broken between entries if the first one is of an higher level than the second one as, for instance, between a section and a subsection. If both of them are of the same level, the break is allowed, and if the first is lower than the second, it is considered a good place for a page break.

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

I don't know if this is completely foolproof, but it seems to work with your example:

\usepackage{titletoc}
\titlecontents{section}[2.3em]
  {}
  {\filbreak\contentslabel{2.3em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1pc]{.}\contentspage\nobreak}

\titlecontents*{subsection}[3.8em]
  {\filright\itshape\contentsmargin{2em}}
  {\thecontentslabel.\nobreakspace\mbox}{\mbox}
  {,\nobreakspace\thecontentspage}[][ -- ][.]

With \filbreak we roughly say to break unless what comes before the following \filbreak can stay in the page.

For the first section after a chapter we mustn't issue the \filbreak: here's a sketch, one needs probably to redefine the chapter entries.

\newif\iffilchapter
\titlecontents{chapter}[1cm]
  {}
  {\filbreak\global\filchaptertrue\bfseries\contentslabel{1cm}}
  {\hspace*{-1cm}}
  {\titlerule*[1pc]{.}\contentspage\nobreak}

\titlecontents{section}[2.3em]
  {}
  {\iffilchapter\global\filchapterfalse\else\filbreak\fi\contentslabel{2.3em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1pc]{.}\contentspage\nobreak}

\titlecontents*{subsection}[3.8em]
  {\filright\itshape\contentsmargin{2em}}
  {\thecontentslabel.\nobreakspace\mbox}
  {\mbox}
  {,\nobreakspace\thecontentspage}[][ -- ][.]
share|improve this answer
    
When I introduce a chapter before this section the pagebreak will occur between chapter and section, which normally titletoc forbidds. Can u improve your answer to restore this benefit (see my edited question)? –  Markus Schmassmann May 16 '12 at 16:30
    
\filbreak seems to introduce an additional newline, adding \vspace{-\baselineskip} before each \filbreak gets the vertical spacing correct. Is there something less hacked? –  Markus Schmassmann May 16 '12 at 17:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.