TransWikia.com

Multipage TOC spills over page border

TeX - LaTeX Asked on July 14, 2021

tableofcontents makes multipage TOC spill over the page border, as can be seen from the picture below.

enter image description here

% Created 2020-10-18 Sun 19:08
% Intended LaTeX compiler: xelatex

documentclass[12pt,a4paper]{article}
%documentclass[12pt,a4paper]{ctexart}

usepackage{xeCJK}
usepackage{zhnumber} % package for Chinese formatting of date time (use /zhtoday)
usepackage[yyyymmdd]{datetime} % set date time to numeric

% Dummy Text for Testing

usepackage{lipsum}
usepackage{zhlipsum}

% Set default indentation

setlengthparindent{24pt}

% Set Paper Size, Page Layout (another variable is 'bindingoffset')
usepackage[margin = 1.5in, paper = a4paper, inner = 2.5cm,
outer = 2.5cm, top = 3cm, bottom = 2.5cm]{geometry}

% Keep paragraph indentation while having a line break in between paragraphs.
edefrestoreparindent{parindent=theparindentrelax}
usepackage{parskip}
restoreparindent

% Indent first paragraph.
usepackage{indentfirst}

usepackage{titlesec}
usepackage{titling}
usepackage{fontspec} % packages for title and section-heading font setting.
usepackage{newunicodechar} % custom fallback font for certain unicode characters.
usepackage{tocloft} % adding the tocloft package for toc customization
usepackage{titletoc} % custom toc indentation and label width.

% Set Header and Numbering Depth
setcounter{tocdepth}{5}
setcounter{secnumdepth}{5}

% Set Font.
setsansfont{Calibri}
setmainfont{Times New Roman} % Set serifed font to Calibri. Originally set to 'Times New Roman', but it cannot display certain characters such as ①②③.
setCJKmainfont{Songti TC}
setCJKsansfont{Kaiti TC} % Set Chinese font. NOTE: Remember to append CJK before of the font class. CJK HAS to be there for the font to show.
setCJKmonofont{PingFang TC}


% WHEN documentclass is set to {article},
% zhnum[style={Traditional,Financial}] doesn't work with the section counter,
% so we define our own counter and increase it every time in thesection.

newcounter{mysec}[section]
renewcommandthesection{%
    addtocounter{mysec}{1}%
    zhnum[style={Traditional,Financial}]{mysec}、} % 大標題序號:壹、貳、參、…
renewcommandthesubsection{zhnum{subsection}、} % added a 、小標題序號:一、二、三、…
renewcommandthesubsubsection{(zhnum{subsubsection})} % added parentheses
% (full-width, don't know if that's what you want) 副標題序號:(一)(二)(三)
renewcommandtheparagraph{arabic{paragraph}} % arabic numbering for paragraph
renewcommandthesubparagraph{} % no subparagraph numbering


% Use dottedcontents from the titletoc package to adjust TOC indentation and label width.
 dottedcontents{section}[3em]{}{3.5em}{1pc} % larger label width (3.5em) to accommodate double digits.
 dottedcontents{subsection}[5.5em]{}{4em}{1pc}
 dottedcontents{subsubsection}[7.5em]{}{2.5em}{1pc}
 dottedcontents{paragraph}[9.5em]{}{2.5em}{1pc}
 dottedcontents{subparagraph}[11.5em]{}{2.5em}{1pc}

% Set formats for each heading level. 'sffamily' will point to the sans-serif font. In this case, 「楷體」.

% here we need to make sure the normal section counter is accessed
titleformat{section}{LARGEbfseriessffamilyfilcenter}
    {zhnum[style={Traditional,Financial}]{section}、}{.5em}{}

titleformat*{subsection}{Largebfseriessffamily}
titleformat*{subsubsection}{Largebfseriessffamily} % Set formats for each heading level. 'sffamily' will point to the sans-serif font. In this case, 「楷體」.

% The `titlesec` package is used over here to make use of `paragraph` and `subparagraph` as headings. Up to five levels of headings can be implemented this way.

% no extra version for numberless is necessary since no numbers are used anyways
% also you get newlines from omitting the [display] in titleformat already
titleformat{paragraph}[block]
    {Largebfseriessffamily}{theparagraph}{0.5em}{}
titleformat{subparagraph}[block]
    {largemdseriessffamily}{thesubparagraph}{0.5em}{}
% we need the following so that they don't indent (second argument, 0em);
% you'll have to adjust the spacing though since this is not display style anymore:
titlespacing*{paragraph}{0em}{3.25ex plus 1ex minus .2ex}{.75ex plus .1ex}
titlespacing*{subparagraph}{0em}{3.25ex plus 1ex minus .2ex}{.75ex plus .1ex}

% Set title font.
renewcommand{maketitlehooka}{sffamily}

% For text-boxes

usepackage{mdframed}
BeforeBeginEnvironment{minted}{begin{mdframed}}
AfterEndEnvironment{minted}{end{mdframed}}

% For tables

usepackage{float}
restylefloat{table}

% [FIXME] ox-latex 的設計不良導致 hypersetup 必須在這裡插入
usepackage{hyperref}
hypersetup{
  colorlinks=true, %把紅框框移掉改用字體顏色不同來顯示連結
  linkcolor=[rgb]{0,0.37,0.53},
  citecolor=[rgb]{0,0.47,0.68},
  filecolor=[rgb]{0,0.37,0.53},
  urlcolor=[rgb]{0,0.37,0.53},
  pagebackref=true,
  linktoc=all,}

usepackage{graphicx}
usepackage{longtable}
usepackage{wrapfig}
usepackage{rotating}
usepackage{amssymb}
usepackage[normalem]{ulem}
usepackage{amsmath}
usepackage{textcomp}
date{today}
title{TOC Issue}
begin{document}

maketitle
clearpage
tableofcontents
% begin{center}text{bfseriesLARGE 目hspace{0.5cm} 錄}end{center}
% vspace{4baselineskip}
% makeatletter
%   @starttoc{toc}%
% makeatother
clearpage
section{Dummy Content}
label{sec:org43d1f54}
subsection{Dummy Section}
label{sec:org3841f64}
lipsum
subsection{Dummy Section}
label{sec:org1050e8e}
lipsum
subsection{Dummy Section}
label{sec:org6bc00c2}
lipsum
subsection{Dummy Section}
label{sec:org8492aa7}
lipsum
subsection{Dummy Section}
label{sec:orgcb5256e}
lipsum
subsection{Dummy Section}
label{sec:orgd9b94b1}
lipsum
subsection{Dummy Section}
label{sec:org4c46c2e}
lipsum
subsection{Dummy Section}
label{sec:orgd61f263}
lipsum
subsection{Dummy Section}
label{sec:org35e467e}
lipsum
subsection{Dummy Section}
label{sec:orgc942561}
lipsum
subsection{Dummy Section}
label{sec:org2747b94}
lipsum
subsection{Dummy Section}
label{sec:orgbec6ff4}
lipsum
subsection{Dummy Section}
label{sec:org2ed2ee6}
lipsum
subsection{Dummy Section}
label{sec:org4188f5f}
lipsum
subsection{Dummy Section}
label{sec:orgb4b25db}
lipsum
subsection{Dummy Section}
label{sec:orgfe9cf5f}
lipsum
subsection{Dummy Section}
label{sec:org64709c6}
lipsum
subsection{Dummy Section}
label{sec:orgecdccfb}
lipsum
subsection{Dummy Section}
label{sec:org55606d9}
lipsum
subsection{Dummy Section}
label{sec:org1f9d81a}
lipsum
subsection{Dummy Section}
label{sec:org502f10c}
lipsum
subsection{Dummy Section}
label{sec:orgc83b51f}
lipsum
subsection{Dummy Section}
label{sec:org775c615}
lipsum
subsection{Dummy Section}
label{sec:orgf237db5}
lipsum
subsection{Dummy Section}
label{sec:orgb17eefe}
lipsum
subsection{Dummy Section}
label{sec:orgffcbede}
lipsum
subsection{Dummy Section}
label{sec:orgc653c67}
lipsum
subsection{Dummy Section}
label{sec:orga5bb311}
lipsum
subsection{Dummy Section}
label{sec:org61ae5ae}
lipsum
subsection{Dummy Section}
label{sec:org178c967}
lipsum
subsection{Dummy Section}
label{sec:org057fa3e}
lipsum
subsection{Dummy Section}
label{sec:orgd104b11}
lipsum
subsection{Dummy Section}
label{sec:orge177e95}
lipsum
subsection{Dummy Section}
label{sec:org6474abe}
lipsum
subsection{Dummy Section}
label{sec:org8d03b86}
lipsum
subsection{Dummy Section}
label{sec:org57c5529}
lipsum
subsection{Dummy Section}
label{sec:orgfbe5a0f}
lipsum
subsection{Dummy Section}
label{sec:org398b88f}
lipsum
subsection{Dummy Section}
label{sec:org8fe4b5b}
lipsum
subsection{Dummy Section}
label{sec:org1b11404}
lipsum
subsection{Dummy Section}
label{sec:org52661fb}
lipsum
subsection{Dummy Section}
label{sec:org9a455b1}
lipsum
subsection{Dummy Section}
label{sec:orgdde86ed}
lipsum
subsection{Dummy Section}
label{sec:org583a3d3}
lipsum
subsection{Dummy Section}
label{sec:org9c71061}
lipsum
subsection{Dummy Section}
label{sec:org41ea3a9}
lipsum
subsection{Dummy Section}
label{sec:org82e47fb}
lipsum
subsection{Dummy Section}
label{sec:org31ea7c1}
lipsum
subsection{Dummy Section}
label{sec:org5f39310}
lipsum
subsection{Dummy Section}
label{sec:org33a3deb}
lipsum
subsection{Dummy Section}
label{sec:orge4c6f8c}
lipsum
subsection{Dummy Section}
label{sec:org2339500}
lipsum
end{document}

The issue is resolved by falling back on @starttoc{toc}%, which, I have learnt, is called by tableofcontents internally to do the work.

enter image description here

% Created 2020-10-18 Sun 19:08
% Intended LaTeX compiler: xelatex

documentclass[12pt,a4paper]{article}
%documentclass[12pt,a4paper]{ctexart}

usepackage{xeCJK}
usepackage{zhnumber} % package for Chinese formatting of date time (use /zhtoday)
usepackage[yyyymmdd]{datetime} % set date time to numeric

% Dummy Text for Testing

usepackage{lipsum}
usepackage{zhlipsum}

% Set default indentation

setlengthparindent{24pt}

% Set Paper Size, Page Layout (another variable is 'bindingoffset')
usepackage[margin = 1.5in, paper = a4paper, inner = 2.5cm,
outer = 2.5cm, top = 3cm, bottom = 2.5cm]{geometry}

% Keep paragraph indentation while having a line break in between paragraphs.
edefrestoreparindent{parindent=theparindentrelax}
usepackage{parskip}
restoreparindent

% Indent first paragraph.
usepackage{indentfirst}

usepackage{titlesec}
usepackage{titling}
usepackage{fontspec} % packages for title and section-heading font setting.
usepackage{newunicodechar} % custom fallback font for certain unicode characters.
usepackage{tocloft} % adding the tocloft package for toc customization
usepackage{titletoc} % custom toc indentation and label width.

% Set Header and Numbering Depth
setcounter{tocdepth}{5}
setcounter{secnumdepth}{5}

% Set Font.
setsansfont{Calibri}
setmainfont{Times New Roman} % Set serifed font to Calibri. Originally set to 'Times New Roman', but it cannot display certain characters such as ①②③.
setCJKmainfont{Songti TC}
setCJKsansfont{Kaiti TC} % Set Chinese font. NOTE: Remember to append CJK before of the font class. CJK HAS to be there for the font to show.
setCJKmonofont{PingFang TC}


% WHEN documentclass is set to {article},
% zhnum[style={Traditional,Financial}] doesn't work with the section counter,
% so we define our own counter and increase it every time in thesection.

newcounter{mysec}[section]
renewcommandthesection{%
    addtocounter{mysec}{1}%
    zhnum[style={Traditional,Financial}]{mysec}、} % 大標題序號:壹、貳、參、…
renewcommandthesubsection{zhnum{subsection}、} % added a 、小標題序號:一、二、三、…
renewcommandthesubsubsection{(zhnum{subsubsection})} % added parentheses
% (full-width, don't know if that's what you want) 副標題序號:(一)(二)(三)
renewcommandtheparagraph{arabic{paragraph}} % arabic numbering for paragraph
renewcommandthesubparagraph{} % no subparagraph numbering


% Use dottedcontents from the titletoc package to adjust TOC indentation and label width.
 dottedcontents{section}[3em]{}{3.5em}{1pc} % larger label width (3.5em) to accommodate double digits.
 dottedcontents{subsection}[5.5em]{}{4em}{1pc}
 dottedcontents{subsubsection}[7.5em]{}{2.5em}{1pc}
 dottedcontents{paragraph}[9.5em]{}{2.5em}{1pc}
 dottedcontents{subparagraph}[11.5em]{}{2.5em}{1pc}

% Set formats for each heading level. 'sffamily' will point to the sans-serif font. In this case, 「楷體」.

% here we need to make sure the normal section counter is accessed
titleformat{section}{LARGEbfseriessffamilyfilcenter}
    {zhnum[style={Traditional,Financial}]{section}、}{.5em}{}

titleformat*{subsection}{Largebfseriessffamily}
titleformat*{subsubsection}{Largebfseriessffamily} % Set formats for each heading level. 'sffamily' will point to the sans-serif font. In this case, 「楷體」.

% The `titlesec` package is used over here to make use of `paragraph` and `subparagraph` as headings. Up to five levels of headings can be implemented this way.

% no extra version for numberless is necessary since no numbers are used anyways
% also you get newlines from omitting the [display] in titleformat already
titleformat{paragraph}[block]
    {Largebfseriessffamily}{theparagraph}{0.5em}{}
titleformat{subparagraph}[block]
    {largemdseriessffamily}{thesubparagraph}{0.5em}{}
% we need the following so that they don't indent (second argument, 0em);
% you'll have to adjust the spacing though since this is not display style anymore:
titlespacing*{paragraph}{0em}{3.25ex plus 1ex minus .2ex}{.75ex plus .1ex}
titlespacing*{subparagraph}{0em}{3.25ex plus 1ex minus .2ex}{.75ex plus .1ex}

% Set title font.
renewcommand{maketitlehooka}{sffamily}

% For text-boxes

usepackage{mdframed}
BeforeBeginEnvironment{minted}{begin{mdframed}}
AfterEndEnvironment{minted}{end{mdframed}}

% For tables

usepackage{float}
restylefloat{table}

% [FIXME] ox-latex 的設計不良導致 hypersetup 必須在這裡插入
usepackage{hyperref}
hypersetup{
  colorlinks=true, %把紅框框移掉改用字體顏色不同來顯示連結
  linkcolor=[rgb]{0,0.37,0.53},
  citecolor=[rgb]{0,0.47,0.68},
  filecolor=[rgb]{0,0.37,0.53},
  urlcolor=[rgb]{0,0.37,0.53},
  pagebackref=true,
  linktoc=all,}

usepackage{graphicx}
usepackage{longtable}
usepackage{wrapfig}
usepackage{rotating}
usepackage{amssymb}
usepackage[normalem]{ulem}
usepackage{amsmath}
usepackage{textcomp}
date{today}
title{TOC Issue}
begin{document}

maketitle
clearpage
% tableofcontents
begin{center}text{bfseriesLARGE 目hspace{0.5cm} 錄}end{center}
vspace{4baselineskip}
makeatletter
  @starttoc{toc}%
makeatother
clearpage
section{Dummy Content}
label{sec:org43d1f54}
subsection{Dummy Section}
label{sec:org3841f64}
lipsum
subsection{Dummy Section}
label{sec:org1050e8e}
lipsum
subsection{Dummy Section}
label{sec:org6bc00c2}
lipsum
subsection{Dummy Section}
label{sec:org8492aa7}
lipsum
subsection{Dummy Section}
label{sec:orgcb5256e}
lipsum
subsection{Dummy Section}
label{sec:orgd9b94b1}
lipsum
subsection{Dummy Section}
label{sec:org4c46c2e}
lipsum
subsection{Dummy Section}
label{sec:orgd61f263}
lipsum
subsection{Dummy Section}
label{sec:org35e467e}
lipsum
subsection{Dummy Section}
label{sec:orgc942561}
lipsum
subsection{Dummy Section}
label{sec:org2747b94}
lipsum
subsection{Dummy Section}
label{sec:orgbec6ff4}
lipsum
subsection{Dummy Section}
label{sec:org2ed2ee6}
lipsum
subsection{Dummy Section}
label{sec:org4188f5f}
lipsum
subsection{Dummy Section}
label{sec:orgb4b25db}
lipsum
subsection{Dummy Section}
label{sec:orgfe9cf5f}
lipsum
subsection{Dummy Section}
label{sec:org64709c6}
lipsum
subsection{Dummy Section}
label{sec:orgecdccfb}
lipsum
subsection{Dummy Section}
label{sec:org55606d9}
lipsum
subsection{Dummy Section}
label{sec:org1f9d81a}
lipsum
subsection{Dummy Section}
label{sec:org502f10c}
lipsum
subsection{Dummy Section}
label{sec:orgc83b51f}
lipsum
subsection{Dummy Section}
label{sec:org775c615}
lipsum
subsection{Dummy Section}
label{sec:orgf237db5}
lipsum
subsection{Dummy Section}
label{sec:orgb17eefe}
lipsum
subsection{Dummy Section}
label{sec:orgffcbede}
lipsum
subsection{Dummy Section}
label{sec:orgc653c67}
lipsum
subsection{Dummy Section}
label{sec:orga5bb311}
lipsum
subsection{Dummy Section}
label{sec:org61ae5ae}
lipsum
subsection{Dummy Section}
label{sec:org178c967}
lipsum
subsection{Dummy Section}
label{sec:org057fa3e}
lipsum
subsection{Dummy Section}
label{sec:orgd104b11}
lipsum
subsection{Dummy Section}
label{sec:orge177e95}
lipsum
subsection{Dummy Section}
label{sec:org6474abe}
lipsum
subsection{Dummy Section}
label{sec:org8d03b86}
lipsum
subsection{Dummy Section}
label{sec:org57c5529}
lipsum
subsection{Dummy Section}
label{sec:orgfbe5a0f}
lipsum
subsection{Dummy Section}
label{sec:org398b88f}
lipsum
subsection{Dummy Section}
label{sec:org8fe4b5b}
lipsum
subsection{Dummy Section}
label{sec:org1b11404}
lipsum
subsection{Dummy Section}
label{sec:org52661fb}
lipsum
subsection{Dummy Section}
label{sec:org9a455b1}
lipsum
subsection{Dummy Section}
label{sec:orgdde86ed}
lipsum
subsection{Dummy Section}
label{sec:org583a3d3}
lipsum
subsection{Dummy Section}
label{sec:org9c71061}
lipsum
subsection{Dummy Section}
label{sec:org41ea3a9}
lipsum
subsection{Dummy Section}
label{sec:org82e47fb}
lipsum
subsection{Dummy Section}
label{sec:org31ea7c1}
lipsum
subsection{Dummy Section}
label{sec:org5f39310}
lipsum
subsection{Dummy Section}
label{sec:org33a3deb}
lipsum
subsection{Dummy Section}
label{sec:orge4c6f8c}
lipsum
subsection{Dummy Section}
label{sec:org2339500}
lipsum
end{document}

I would like to know:

  1. Why this happens and
  2. What other ways are there to prevent this from happening?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP