TransWikia.com

Incorrectly ordered contents with tocloft

TeX - LaTeX Asked on February 25, 2021

I am trying to add some horizontal bars to the contents of my thesis, to split it up a little. However, I am finding that the horizontal bars do not appear where I set them. As an MWE I have written the following document:

documentclass{report}

%Horizontal line in toc
usepackage{tocloft}
newcommandtocrule{cftaddtitleline{toc}{chapter}{rule{textwidth}{0.4pt}}{}}

begin{document}
tableofcontents
include{chapter1}
tocrule
include{chapter2}
end{document}

The file chapter1 is simply chapter{My chapter 1} and chapter2 similarly include{chapter2}. When I make the document, the horizontal bar is below chapter 2 in the contents. Something strange is going on with the interaction between cftaddtitleline and include. If the chapters are in the main document then the contents page renders fine. Obviously, for my thesis, I don’t want everything in one file.

One Answer

cftaddtitleline{toc}{chapter}{rule{textwidth}{0.4pt}}{} writes @writefile{toc}{contentsline {chapter}{rule {textwidth }{0.4pt}}{}} to the main aux file, when the current page is shipped out.

include writes immediatley an @input command for the own aux file of the included tex file to the main aux file. So, if the cftaddtitleline command appears on the same page as include the @input for the special aux goes the main aux before the @writefile{toc} … .

Therefore the resulting main aux file of your example is:

...
@input{chapter1.aux}
@input{chapter2.aux}
@writefile{toc}{contentsline {chapter}{rule  {textwidth }{0.4pt}}{}}
...

AFAIK this behaviour of include can not be changed.

But here are at least three possible solutions:

  1. You could use input instead include:
begin{filecontents}[overwrite]{chapter1.tex}
chapter{Foo}
end{filecontents}
begin{filecontents}[overwrite]{chapter2.tex}
chapter{Bar}
end{filecontents}

documentclass{report}
%Horizontal line in toc
usepackage{tocloft}
newcommandtocrule{cftaddtitleline{toc}{chapter}{rule{textwidth}{0.4pt}}{}}

begin{document}
tableofcontents
input{chapter1}
tocrule
input{chapter2}
end{document}

enter image description here

  1. Move tocrule in file chapter2.tex:
begin{filecontents}[overwrite]{chapter1.tex}
chapter{Foo}
end{filecontents}
begin{filecontents}[overwrite]{chapter2.tex}
tocrule
chapter{Bar}
end{filecontents}

documentclass{report}
%Horizontal line in toc
usepackage{tocloft}
newcommandtocrule{cftaddtitleline{toc}{chapter}{rule{textwidth}{0.4pt}}{}}

begin{document}
tableofcontents
include{chapter1}
include{chapter2}
end{document}

The result is the same as above.

  1. You could load package scrlfile and use BeforeFile{chapter2.tex}{tocrule} before include{chapter2}:
begin{filecontents}[overwrite]{chapter1.tex}
chapter{Foo}
end{filecontents}
begin{filecontents}[overwrite]{chapter2.tex}
chapter{Bar}
end{filecontents}

documentclass{report}
%Horizontal line in toc
usepackage{tocloft}
newcommandtocrule{cftaddtitleline{toc}{chapter}{rule{textwidth}{0.4pt}}{}}

usepackage{scrlfile}

begin{document}
tableofcontents
include{chapter1}
BeforeFile{chapter2.tex}{tocrule}
include{chapter2}
end{document}

or

begin{filecontents}[overwrite]{chapter1.tex}
chapter{Foo}
end{filecontents}
begin{filecontents}[overwrite]{chapter2.tex}
chapter{Bar}
end{filecontents}

documentclass{report}
%Horizontal line in toc
usepackage{tocloft}
newcommandtocrule{cftaddtitleline{toc}{chapter}{rule{textwidth}{0.4pt}}{}}

usepackage{scrlfile}
BeforeFile{chapter2.tex}{tocrule}

begin{document}
tableofcontents
include{chapter1}
include{chapter2}
end{document}

The result is the same as above.

Correct answer by esdd on February 25, 2021

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