Page LayoutUrgent help with widows/orphans in Table of Contents

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Urgent help with widows/orphans in Table of Contents

Post by LavaTyper »

Greetings.
I am in the process of writing my dissertation, and I need to fix widows and orphans in my table of contents.

For my dissertation, I also need to meet other strict formatting requirements, which I have already included in the below example. (I was given a template and I tried to modify it.)


Here is an illustration: in the below example, Section 4.1 is on a line by itself; Both Chapter 4 and Section 4.1 have to be moved to the subsequent page so that Section 4.1 is together with Section 4.2 (and Section 4.3) to avoid widow/orphan problems.


How do I modify the example to do this for sections and subsections, and appendices?

Also how do I modify the list of tables and list of figures similarly?


This is fairly urgent so I need help soon from a LaTeX guru. Let me know if I can be of assistance in illustrating what I mean further.

(Also I have provided a screenshot illustrating the problem.)

Code: Select all

\documentclass[12pt,letterpaper]{report}

\usepackage[document]{ragged2e}

\usepackage[none]{hyphenat}
\usepackage{times}
\usepackage{ifthen}
\usepackage{calc}
\usepackage{fancyhdr}
\usepackage{setspace}
\usepackage[indentafter,newlinetospace]{titlesec}
\usepackage[dotinlabels]{titletoc}
\usepackage{ccaption}
\usepackage{lipsum}  % not for use in the actual document


\renewcommand{\contentsname}{TABLE OF CONTENTS}
\renewcommand{\listtablename}{LIST OF TABLES}
\renewcommand{\listfigurename}{LIST OF FIGURES}
\renewcommand{\bibname}{REFERENCES}



%% Margins
\setlength{\textheight}{9in}
\setlength{\topmargin}{-0.5in}
\setlength{\headheight}{16pt}
\setlength{\headsep}{0.5in-\headheight}
\setlength{\textwidth}{5.8in}
\setlength{\parindent}{0.5in}

\newcommand{\relaxedspacing}{\doublespacing}  
\doublespacing

\raggedbottom

\clubpenalty =10000
\widowpenalty =10000
\brokenpenalty=10000
\doublehyphendemerits=40000
\finalhyphendemerits =10000
\righthyphenmin=3
\lefthyphenmin=4
\renewcommand{\topfraction}{0.5}
\setlength{\emergencystretch}{2em}



\titleformat{\chapter}[display]{\normalsize\bfseries\filcenter}{Chapter~\thechapter}{0in}{}
\titlespacing{\chapter}{0pt}{-.4in}{.25in}

\titleformat{\section}[hang]{\normalsize\bfseries\raggedright}{\thesection\hspace{0.1in}}{0in}{}
\titlespacing{\section}{0pt}{.23in}{.13in}

\titleformat{\subsection}[hang]{\normalsize\bfseries\raggedright}{\thesubsection\hspace{0.1in}}{0in}{}
\titlespacing{\subsection}{0pt}{.2in}{.09in}


% MAKE SURE TEXT IN THE TOC DOES NOT EXCEED
% ABOUT 5 DOTS:
\contentsmargin[3em]{0in}	
% This doesn't always work, keep reading.




\titlecontents{chapter}[0in]    
    { \ifthenelse{\equal{\thecontentslabel}{1}}{
        \vspace{1.3\baselineskip}Chapter\\ \normalsize
      }{
        \normalsize\vspace{.2\baselineskip}\raggedright
      }
    }
    {\contentspush{\thecontentslabel.~}}
    {}
    {\titlerule*[.3em]{.}\thecontentspage}
    {}

\titlecontents{section}[.3in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}
\titlecontents{subsection}[.6in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}
\titlecontents{subsubsection}[.9in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}
\titlecontents{paragraph}[1.2in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}
\titlecontents{subparagraph}[1.5in]{}
              {\contentspush{\thecontentslabel.~}}{}
              {\titlerule*[.3em]{.}\thecontentspage}{}


\titlecontents{table}[0in]{}
    {\contentspush{Table~\thecontentslabel.~}}{} 
    {\titlerule*[.3em]{.}\thecontentspage}{}

\titlecontents{figure}[0in]{}
    {\contentspush{Figure~\thecontentslabel.~}}{}
    {\titlerule*[.3em]{.}\thecontentspage}{}


\setcounter{secnumdepth}{10}
\setcounter{tocdepth}{10}
\newlength{\contentslength}


\let\Oldlistoffigures\listoffigures
\renewcommand{\listoffigures}{
  \cleardoublepage
%  \phantomsection
  \addcontentsline{toc}{chapter}{\listfigurename}
  {\relaxedspacing \Oldlistoffigures }
}

\let\Oldlistoftables\listoftables
\renewcommand{\listoftables}{
  \cleardoublepage
%  \phantomsection
  \addcontentsline{toc}{chapter}{\listtablename}
  {\relaxedspacing \Oldlistoftables }
}

\let\Oldtableofcontents\tableofcontents
\renewcommand{\tableofcontents}{
  \cleardoublepage
%  \phantomsection
%  \pdfbookmark[0]{\contentsname}{my_toc}
  {\relaxedspacing \Oldtableofcontents} 
  \setcounter{tocdepth}{10}
}

\renewcommand{\appendixname}{Appendix}
\let\oldAppendix\appendix
\renewcommand{\appendix}{
  \oldAppendix
  \cleardoublepage
%  \phantomsection
%  \pdfbookmark[0]{APPENDICES}{my_apps}
  \addtocontents{toc}{\protect \setcounter{tocdepth}{0} \par}
  \titleformat{\chapter}[display]{\normalsize\bfseries\filcenter}{\appendixname~\thechapter}{0in}{}
  \titlespacing{\chapter}{0pt}{-.4in}{.25in}
  \titlecontents{chapter}[0in]{\setlength{\emergencystretch}{4.5em}\normalsize\vspace{.2\baselineskip}}
    {\contentspush{Appendix~\thecontentslabel.~}}{}
    {\titlerule*[.3em]{.}\thecontentspage}{}
}





\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                       \if@mainmatter
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter}#1}%
                       \else
                         \addcontentsline{toc}{chapter}{#1}%
                       \fi
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi
                    \chaptermark{#1}%
%                    \addtocontents{lof}{\protect\addvspace{10\p@}}% NEW
%                    \addtocontents{lot}{\protect\addvspace{10\p@}}% NEW
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\makeatother







% FINALLY HERE WE ARE.

\begin{document}
\raggedright
\pagenumbering{roman}


\tableofcontents % Looks great except for the Appendix


% Because I added ``Table" and ``Figure" I have to redefine the
% emergency stretch
\setlength{\emergencystretch}{3.5em}
% before I add the \listoftables and \listoffigures




\listoftables
\listoffigures

% Now I can go back to what I had before.

\setlength{\emergencystretch}{2em}


\clearpage

\pagenumbering{arabic}


\setlength{\parindent}{0.5in}

\chapter{HERE IS A CHAPTER}

\section{First section first section first section}

Stuff

\section{Second section second section second section}

Stuff

\section{Third section third section third section}

\subsection{First subsection first subsection first subsection}

Stuff

\subsection{Second subsection second subsection second subsection}

Stuff


\chapter{SECOND CHAPTER}

\section{First section first section first section}

Stuff

\section{Second section second section second section}

Stuff

\section{Third section third section third section}

\subsection{First subsection first subsection first subsection}

Stuff

\subsection{Second subsection second subsection second subsection}

Stuff



\chapter{THIRD CHAPTER}

\section{First section first section first section}

Stuff

\section{I decided to have a table}

\begin{table}[h]
\centering
\begin{tabular}{l}
My table content \\
More stuff
\end{tabular}
\caption{\label{myfirsttable}My first table}
\end{table}

\section{Third section third section third section}

\subsection{First subsection first subsection first subsection}

Stuff

\subsection{Second subsection second subsection second subsection}

Stuff



\chapter{FOURTH CHAPTER}

\section{First section first section first section}

Stuff

\section{Second section second section second section}

Stuff

\section{Third section third section third section}

\subsection{First subsection first subsection first subsection}

Stuff

\subsection{This subsection has a table}

Stuff

\begin{table}[h]
\centering
\begin{tabular}{l}
My table content \\
More stuff
\end{tabular}
\caption{\label{mysecondtable}My second table}
\end{table}


\appendix{An appendix}

Stuff


%\stepcounter{chapter}
%\addtocontents{toc}{\protect\LTXcontentsline {chapter}{{Appendix \thechapter}. TITLE FOR A SPECIAL APPENDIX}{TEXT} }
\end{document}
Attachments
Screenshot of problem
Screenshot of problem
ScreenshotTOCwo.png (96.35 KiB) Viewed 5748 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

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

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Urgent help with widows/orphans in Table of Contents

Post by kaiserkarl13 »

This is a hack:

Code: Select all

    \addtocontents{toc}{\protect\pagebreak[4]}
    \chapter{FOURTH CHAPTER}
I say it's a hack because this manually inserts a page break into the table of contents: it won't work correctly if something above that point changes and suddenly you don't want the page to break at that point anymore. There is a way to give TeX a lower penalty for breaking the page before a chapter heading, which might be more robust, but I don't know off the top of my head how to change that.
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Re: Urgent help with widows/orphans in Table of Contents

Post by LavaTyper »

True, if all else fails, I can do the manual page breaks in the TOC... though I was just considering the possibility of an automatic routine.
kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Urgent help with widows/orphans in Table of Contents

Post by kaiserkarl13 »

This works in your example, but may require some tweaking:

Code: Select all

\let\newchapter\chapter
\long\def\chapter{\addtocontents{toc}{\penalty-3000}\newchapter}
Set the penalty to something more negative (like -4000) if it breaks the chapters in the wrong place. Obviously, setting it to -10000 will break the page before EVERY chapter entry....
Post Reply