3

I can use \settocdepth to change the depth of the ToC min-document. This works as expected in the MWE below when I comment out the \include statement. However, when the \settocdepth command is followed by an include statement which includes a file containing the different \chapter and \section statements, the toc file ends up in the wrong order.

My MWE is the following:

\documentclass{memoir}    
\begin{document}       

\frontmatter    
\settocdepth{chapter}    
\tableofcontents*    
\chapter{Preface}    
\chapter{Introduction}    
\section{Section 1}    
\section{Section 2}    
\section{Section 3}    

\mainmatter    
\settocdepth{section}    
\include{first-chapter}
%\chapter{First chapter}    
%\section{A section}    
%\section{Another section}    
\chapter{Second chapter}    
\section{A section}    
\section{Another section}    

\end{document}

The ToC file:

\changetocdepth {0}    
\contentsline {chapter}{Preface}{iii}{}%
\contentsline {chapter}{Introduction}{v}{}%
\contentsline {section}{Section 1}{v}{}%
\contentsline {section}{Section 2}{v}{}%
\contentsline {section}{Section 3}{v}{}%
\contentsline {chapter}{\chapternumberline {1}First chapter}{1}{}%
\contentsline {section}{\numberline {1.1}A section}{1}{}%
\contentsline {section}{\numberline {1.2}Another section}{1}{}%
\changetocdepth {1}
\contentsline {chapter}{\chapternumberline {2}Second chapter}{3}{}%
\contentsline {section}{\numberline {2.1}A section}{3}{}%
\contentsline {section}{\numberline {2.2}Another section}{3}{}%

You can see that \changetocdepth is only added after the contentslines from the included file.

How can I fix this? What am I doing wrong?

7
  • 1
    move the command inside the included files. Feb 12 at 12:10
  • Thanks, @UlrikeFischer, for this work-around. However, I rather solve the problem in a different way as I do not believe the \settocdepth command belongs intisde the included file containing the chapter contents. Especially should I decide to change the order of the chapters, I would have to remember moving that command around as well.
    – Tommiie
    Feb 12 at 12:13
  • 1
    @UlrikeFischer that gives the document a bad structure and easily gets overlooked later on.
    – daleif
    Feb 12 at 12:13
  • 1
    I do not want to change the counter "for some chapter". I want the value to be zero for everything in the frontmatter and the value to be one for everything in the mainmatter. The command thus does not belong inside an included chapter file.
    – Tommiie
    Feb 12 at 12:42
  • 1
    @Tommiie I've added a solution
    – daleif
    Feb 12 at 13:10

3 Answers 3

2

You need to switch the value before \mainmatter does a \clearpage and flushes the pending writes.

$ cat aa105.toc
\changetocdepth {0}
\contentsline {chapter}{Preface}{iii}{}%
\contentsline {chapter}{Introduction}{v}{}%
\contentsline {section}{Section 1}{v}{}%
\contentsline {section}{Section 2}{v}{}%
\contentsline {section}{Section 3}{v}{}%
\changetocdepth {1}
\contentsline {chapter}{\chapternumberline {1}First chapter}{1}{}%
\contentsline {section}{\numberline {1.1}A section}{1}{}%
\contentsline {section}{\numberline {1.2}Another section}{1}{}%
\contentsline {chapter}{\chapternumberline {2}Second chapter}{3}{}%
\contentsline {section}{\numberline {2.1}A section}{3}{}%
\contentsline {section}{\numberline {2.2}Another section}{3}{}%

is produced from

\documentclass{memoir}    
\begin{document}       

\frontmatter    
\settocdepth{chapter}    
\tableofcontents*    
\chapter{Preface}    
\chapter{Introduction}    
\section{Section 1}    
\section{Section 2}    
\section{Section 3}    



\settocdepth{section}
\mainmatter    
\include{first-chapter}
%\chapter{First chapter}    
%\section{A section}    
%\section{Another section}    
\chapter{Second chapter}    
\section{A section}    
\section{Another section}    

\end{document}
2
  • I am very certain I tried this and it did not work. However, I tried it again just now and it does work now. I must have done something wrong the previous time. Thank you for your answer!
    – Tommiie
    Feb 12 at 18:15
  • I've added tihs as a comment to the memoir manual.
    – daleif
    Feb 13 at 12:05
3

The main reason to use \include is that it allows you to use \includeonly which can compile only some chapters but still refer to numbers and labels of the other chapters.

To get this to work, every \include writes its own .aux file (and records its name in the main file) and also record states of counters and similar.

This only works well if every included file is basically self-contained and that imposes some restrictions on your document: One is that you need to do some settings inside the file, mostly the ones which work by writing to the aux-files and other files like your command.

Getting such commands working outside the \include by making them immediate can work, but typically lead to other problems as they now are often issued too early so I wouldn't recommend this.

Options

  • If you don't need \includeonly use \input instead \include

  • If you need \includeonly and want to have such settings in the main file, consider to add a hook at the begin of every file and then use the hook commands to add commands to it (this can naturally be hidden in some shorter command):

  • if your include-files always start with a \chapter you can also use the cmd/chapter/before hook.

\documentclass[a4paper]{book}
\NewHook{myhook/settings}

\begin{filecontents}[overwrite]{foobar1.tex}
\UseHook{myhook/settings}
\chapter{Foobar 1 chapter}

test

\section{Foobar section}
\end{filecontents}

\begin{filecontents}[overwrite]{foobar2.tex}
\UseHook{myhook/settings}
\chapter{Foobar 2 chapter}

test

\section{Foobar section}
\end{filecontents}

\begin{document}

\frontmatter

\tableofcontents

\AddToHookNext{myhook/settings}
 {\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}}
\include{foobar1}


\mainmatter

\AddToHookNext{myhook/settings}
 {\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}}
\include{foobar2}

\end{document}

enter image description here

3

The next memoir release will have a solution for this problem. A starred version will be added that automatically either adds a \clearpage like in Davids answer, or uses an immediate write if it is safe.


This actually isn't a memoir problem per se. Your code is equivalent to the article example at the end, which shows the same problem (\settocdepth is a wrapper around \addtocontents). Since writing stuff to the toc file mostly involves writing page numbers to the file, its writing is handled in the output routine and thus things can get out of sequence.

However something can be done.

We can define an immediate version of \settocdepth.

BUT USE WITH EXTREME CARE, YOU CAN END UP WITH THE OPOSITE PROPBLEM IF YOU USE SAY \section AT THE SAME LEVEL

\makeatletter

% we need an immediate version of \addtocontents.
% Note this can be dangerous and give wrong results, so we remove \thepage
\long\def\immediateaddtocontents#1#2{%
\immediate@protected@write\@auxout% <-- \immediate@protected@write is defined in memoir
{\let\label\@gobble \let\index\@gobble \let\glossary\@gobble \let\thepage\relax}%
{\string\@writefile{#1}{#2}}}

% it is a lot easier to define \settocdepth using expl3, here is a helper
\ExplSyntaxOn
\newcommand*{\settocdepth@}[2]{%
  \str_case_e:nnTF{#2}{
    {none}{          #1{toc}{\changetocdepth{-10} }}
    {book}{          #1{toc}{\changetocdepth{-2} }}
    {part}{          #1{toc}{\changetocdepth{-1} }}
    {chapter}{       #1{toc}{\changetocdepth{0} }}
    {section}{       #1{toc}{\changetocdepth{1} }}
    {subsection}{    #1{toc}{\changetocdepth{2} }}
    {subsubsection}{ #1{toc}{\changetocdepth{3} }}
    {paragraph}{     #1{toc}{\changetocdepth{4} }}
    {subparagraph}{  #1{toc}{\changetocdepth{5} }}
    {all}{           #1{toc}{\changetocdepth{50} }}
  }{
    \@ifundefined{toclevel@#2}{%
      \@memwarn{Unknown~toclevel~for~#2}%
    }{%
      \setcounter{tocdepth}{\@nameuse{toclevel@#2}}%
    }
  }{
    \@memerror{%
      Unknown~document~division~name~(#2)
    }{%
     I'll~ignore~ it.~
     Type~<return>~and~I'll~continue.}%
  }
}

\newcommand*\immediatesettocdepth[1]{%
  \settocdepth@{\immediateaddtocontents}{#1}%
}
\ExplSyntaxOff
\makeatother

Then use \immediatesettocdepth in your code instead of \settocdepth.

I might add this to memoir in the future.


Here is the example showing that this is not a memoir issue.

\documentclass[a4paper]{book}
\begin{filecontents}[overwrite]{foobar.tex}
\chapter{Foobar chapter}

test

\section{Foobar section}
\end{filecontents}
\begin{document}

\frontmatter
\tableofcontents

\addtocontents{toc}{\protect\setcounter{tocdepth}{0}}
\include{foobar}


\mainmatter

\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\include{foobar}

\end{document}
2
  • Then use \immediatesettocdepth in your code instead of \settocdepth. (your statement contains a typo but since it is only one character, I cannot edit your post - an edit requires at least six modified characters).
    – Tommiie
    Feb 12 at 13:15
  • @Tommiie fixed.
    – daleif
    Feb 12 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .